3.0.1 โ€ข Published 3 years ago

delta-time v3.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

โฒ๏ธ delta-time

npm GitHub Workflow CI Status Codecov GitHub npm

A simple module to compute intervals

  • Simple function to parse user friendly intervals
  • Clear-up your code from messy time computations
  • Comes with a utility to delay a promise
  • Dependency free and typed

๐Ÿ“ฆ Installation

npm install delta-time

๐Ÿ“š Usage

import {calculate as dt, delay} from "delta-time";

// human friendly string conversion
dt("2 days"); // 172800000
dt("1d"); // 86400000
dt("1 micro"); // 0.001
dt("1h1m1s", "sec"); // 3661

// typed objects
dt({hours: 1, minutes: 30}); // 5400000

// pass-through ms numbers
dt(123); // 123
dt(456, "sec"); // 0.456

// delay utility
await delay("1min"); // block and resolve to undefined
const out = await delay({seconds: 1}, {value: "โฒ๏ธ timey wimey"}); // block and resolve to value
await delay({seconds: 30}, {reject: true, value: new Error("๐Ÿ’ฃ timed error")}); // block and throw value

Perfect for timeouts and intervals

setTimeout(function () {
    console.log("foo");
}, dt("500ms"));

setTimeout(function () {
    console.log("bar");
}, dt("5 secs"));

setTimeout(function () {
    console.log("baz");
}, dt("1h30m5s"));

Allows complex specifications

dt("- 1 day"); // -86400000
dt("2 hours - 30 seconds"); // 7170000

Not just for milliseconds

dt("1000 ms", "s"); // 1
dt("2 week", "days"); // 14
dt("300 Years, 5 Months and 2 Hours", "days"); // 109727.28333333334

๐Ÿ“– API

calculate(time: TimeValue, unit?: TimeUnit): number

  • Takes a number, a string or an object describing a time interval
    • A combination of units can be given
    • For better typing, prefer the object
  • Returns the value of the interval in the specified unit (default is milliseconds)

delay<T = undefined>(time: TimeValue, config?: DelayConfig): Promise<T | undefined>

  • Takes a number, a string or an object describing a time interval to block
  • Takes an optional configuration object with:
    • An optional value T field to resolve or error to reject
    • An optional reject boolean field, set it to true to reject (default is false)
  • Returns a Promise that resolves or rejects the optional value given after the time given

๐ŸŒ Language

UnitDurationString unitsObject key
nanosecond10โˆ’9 secondsns, nano(s), nanosecond(s)nanoseconds
microsecond10โˆ’6 secondsฮผs, micro(s), microsecond(s)microseconds
millisecond0.001 secondsms, milli(s), millisecond(s)milliseconds
second1 secondss, sec(s), second(s)seconds
minute60 secondsm, min(s), minute(s)minutes
hour60 minutesh, hr(s), hour(s)hours
day24 hoursd, day(s)days
week7 daysw, wk(s), week(s)weeks
month30.44 daysmo(s), month(s)months
year365.25 daysy, yr(s), year(s)years

๐Ÿงช Environment import

The package is bundled in cjs and esm for bundlers.

Bundler

import dt from "delta-time";
import {calculate as dt, delay} from "delta-time";

NodeJs

const {calculate: dt, delay} = require("delta-time");

NodeJs (module)

import dt from "delta-time";
const {calculate, delay} = dt;
3.0.1

3 years ago

3.0.0

3 years ago

2.0.0

5 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago