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 🙏

© 2025 – Pkg Stats / Ryan Hefner

unitspan

v1.0.1

Published

Create differential spans for units of measurement, including time measurements, digital (binary) measurements, and more.

Downloads

154

Readme

UnitSpan

UnitSpan is a suite of conversion tools for easy conversions between different units of measurement.

TimeSpan

Convert between different units of measurement for time, such as nanoseconds to seconds, milliseconds, days, weeks, years, and more!

import { TimeSpan } from "unitspan";

const ts = TimeSpan.fromSeconds(10);

// default precision of 5 digits
console.log(ts.to(m => m.Minutes)); // prints 0.16667
console.log(ts.precision(7).to(m => m.Minutes)); // prints 0.1666667

// or custom functions

// set a timeout that will fire after 10 seconds.
const unsub1 = ts.timeout(() => {
    console.log(`I will print after 10 seconds`);
});

let i = 0;
// set an interval that runs once every 1 second.
const unsub2 = ts.sub(m => m.Seconds(9)).interval(() => {
    if(i++ >= 2) {
        console.log(`but not if I unsubscribe after 3 seconds!`);
        unsub1();
        unsub2();
    }
});

// wait for 10 seconds.
await ts.delay();
console.log(`This will print definitely after 10 seconds.`);

DigiSpan

Convert between different units of measurement for Digital Storage, such as Bits to Bytes, Kilobytes, or even Kibibytes

import { DigiSpan } from "unitspan";

const ds = DigiSpan.fromBits(12);
console.log(ds.to(m => m.Bytes)); // prints 1.5

const buffer = ds.buffer(); // creates a Uint8Array of size 2 (Math.ceil(ds.to(m => m.Bytes)))

// Get the number of digital units that your buffer size is.
const dsFromBuffer = DigiSpan.fromBuffer(new Uint8Array(12));
console.log(ds.to(m => m.Bytes)); // 12
console.log(ds.to(m => m.Bits)); // 96

TempSpan

Convert between the different temperature units of measurement, Kelvin, Celsius, and Fahrenheit.

import { TempSpan } from "unitspan";

const ts = TempSpan.fromKelvin(0);
console.log(ds.to(m => m.Fahrenheit)); // will print -459.67
console.log(ds.to(m => m.Celsius)); // will print -273.15

alternative syntax

to(model => string) vs. to(string)

const ts = TimeSpan.fromSeconds(10);
// to convert to minutes, you can either do:
ts.to(m => m.Minutes);
// or
ts.to("Minutes");

Wrappers

The UnitSpan class syntax can be different from what other languages are used to. For example, C#'s implementation of TimeSpan would look something like this when converting seconds to minutes:

TimeSpan.FromSeconds(10).Minutes;

The above syntax is much more understandable than:

TimeSpan.fromSeconds(10).to(m => m.Minutes);

Therefore, this section is dedicated to creating a Wrapper class from the UnitSpan library to make the library look more human-friendly.

Here is an example of creating a wrapper class that looks like C#'s implementation:

import { TimeSpan as US_TimeSpan } from 'unitspan';

export class TimeSpan {
    /** @type {US_TimeSpan} */
    #timespan;

    /**
     * @param {number} initialQuantity
     */
    static FromSeconds(initialQuantity) {
        return new TimeSpan(US_TimeSpan.fromSeconds(initialQuantity));
    }

    /**
     * @protected
     * @param {US_TimeSpan} timespan
     */
    constructor(timespan) {
        this.#timespan = timespan;
    }

    get Minutes() {
        return this.#timespan.to(m => m.Minutes);
    }

    get Timeout() {
        return this.#timespan.timeout();
    }

    Interval(callback) {
        return this.#timespan.interval(callback);
    }
}

const _12secondsToMinutes = TimeSpan.FromSeconds(12).Minutes;

Helpful information

All UnitSpan functions return a clone of the UnitSpan child object, meaning you can chain a lot of functions (such as .sub or .add) and you can save the state as you make changes to the Span-like object.

e.g.,

const ts_2sec = TimeSpan.fromSeconds(2);
const ts_1sec = ts_2sec.sub(m => m.Seconds(1));

assert(ts_2sec.to(m => m.Seconds) === 2);
assert(ts_1sec.to(m => m.Seconds) === 1);