npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typed-duration

v2.0.0

Published

Zero-dependency typed duration library for JavaScript

Downloads

148,884

Readme

Typed Duration

Node.js CI npm version

A Zero-dependency typed duration library for JavaScript/TypeScript. Express and convert time durations with type-safety.

This library uses Value Object Typing to allow you to express time durations in a type-safe way, and perform conversion between different units.

Note: requires TypeScript 3.8 or later

Version 1.x works on Node 10+ Version 2.x requires Node 16+

Installation

Install the library to your project:

npm i typed-duration

Use

Consider the following code:

setTimeout(doSomething, 1000)

It's pretty clear that these are milliseconds, because you know the API. Typically, developers might do something like:

setTimeout(doSomething, 5 * 60 * 1000) // In Five Minutes

With this library, you can do this:

import { Duration } from 'typed-duration'
const { milliseconds, minutes } = Duration

const period = minutes.of(5)

setTimeout(doSomething, milliseconds.from(period)) // Every Five Minutes

Well, that looks like more code. Yes, it is. It is also more semantically expressive of the programmer's intent, which makes it better for maintenance.

The situation is exacerbated when you expose a programming API that takes a time duration as a number. We all know that setTimeout takes milliseconds, but how do you communicate to consumers of your API what the time units are for timeout in your API call?

You should, of course, document it, and put it in JSDoc comments so that they can get hinting in their IDE.

You could call it timeoutSeconds to make it clear that it expects seconds.

Or you could make it take a TimeDuration and allow them to pass in whatever they want, and convert it to the units you need, like this:

import { Duration, TimeDuration } from 'typed-duration'

function executeLater(fn: () => void, delay: TimeDuration) {
    setTimeout(fn, Duration.milliseconds.from(delay))
}

Now, consumers of this function can call it like this:

import { Duration } from 'typed-duration'
const { milliseconds, seconds, minutes, hours, days } = Duration

// After 2.5 seconds
executeLater(doSomething, milliseconds.of(2500))

// After 10 seconds
executeLater(doSomething, seconds.of(10))

// After 15 minutes
executeLater(doSomething, minutes.of(15))

// After 3 hours
executeLater(doSomething, hours.of(3))

// After 6 days
executeLater(doSomething, days.of(6))

#winning

Backward-compatible API

If you have an existing API you want to add this to, you can use the MaybeTimeDuration type, like this:

import { Duration, MaybeTimeDuration } from 'typed-duration'

function executeLater(fn: () => void, period: MaybeTimeDuration) {
    setTimeout(fn, Duration.milliseconds.from(period))
}

// You can pass in a typed duration, and it will convert to a number of milliseconds
executeLater(doSomething, Duration.seconds.from(20))

// a number will be allowed by the MaybeTimeDuration type
// and the milliseconds.from() call will simply pass it through
executeLater(doSomething, 2500)

Logging

You can log times for user information in the format that the user specified them, including units, with Duration.value.of. You can supply an optional default unit to be used for untyped numbers (if you don't, it will just print the number).

For example:

import { Duration, MaybeTimeDuration } from 'typed-duration'

function executeLater(fn: () => void, delay: MaybeTimeDuration) {
    console.log(`Executing in ${Duration.value.of(delay, "ms")}...`)
    setTimeout(fn, Duration.milliseconds.from(delay))
}

executeLater(doSomething, Duration.seconds.from(20))
// Executing in 20s...

executeLater(doSomething, Duration.milliseconds.from(350))
// Executing in 350ms...

executeLater(doSomething, Duration.hours.from(3))
// Executing in 3h...

executeLater(doSomething, 2500)
// Executing in 2500ms...

Feature Requests, Bug Reports

See the GitHub repo.