New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

duration-util

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

duration-util

A utility to streamline creating millisecond durations by allowing the use of human readable code

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

duration-util

npm package

Downloads Issues

A comprehensive, flexible, and efficient duration manipulation utility for JavaScript.

Inspired by the Dart programming language's Duration class, duration-util offers a similar experience for JavaScript developers, providing a rich set of functionalities to handle time durations with ease.

Install

npm install duration-util

Usage

import { Duration, setDurationTimeout, setDurationInterval, sleep } from "duration-util";

const duration = new Duration({ hours: 1, minutes: 30 });

console.log(duration.toMinutes()); //=> 90

// Using utility functions
setDurationTimeout(() => {
	console.log("Timeout after 2 minutes");
}, Duration.fromMinutes(2));

setDurationInterval(() => {
	console.log("Interval every 10 seconds");
}, Duration.fromSeconds(10));

(async () => {
	await sleep(Duration.fromSeconds(5));
	console.log("Slept for 5 seconds");
})();

API

Class: Duration

new Duration(options?)
  • options: DurationOptions | number - Duration options or milliseconds.
  • Returns: Duration instance.
Duration.fromMilliseconds(milliseconds), Duration.fromSeconds(seconds), etc.
  • Static methods to create Duration from different time units.
  • milliseconds, seconds, minutes, etc.: number
  • Returns: Duration instance.
toMilliseconds(), toSeconds(), toMinutes(), etc.
  • Conversion methods to get the duration in different time units.
  • Returns: number
add(other), subtract(other), multiply(factor), divide(divisor)
  • Arithmetic methods to manipulate durations.
  • other: Duration, factor, divisor: number
  • Returns: Duration instance.
get days(), get hours(), get minutes(), etc.
  • Getter methods for different time units.
  • Returns: number
format(template)
  • Formats the duration according to a template string.
  • template: string
  • Returns: string
toString()
  • Returns a string representation of the duration.
  • Returns: string

Function: setDurationTimeout(callback, duration)

  • callback: () => void - Function to execute after timeout.
  • duration: Duration - Duration to wait before executing the callback.

Function: setDurationInterval(callback, duration)

  • callback: () => void - Function to execute at each interval.
  • duration: Duration - Interval duration.

Function: sleep(duration)

  • duration: Duration - Duration to sleep.
  • Returns: Promise<void>

FAQs

Package last updated on 14 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc