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

@anonymort/ms-long

v2.0.0

Published

Extended time conversion utility supporting large values and multiple units

Downloads

6

Readme

ms-long

An extended time conversion utility that builds upon the popular ms library, supporting extremely large time values and multiple units, including those needed for scientific projects.

Features

  • Parse time strings to milliseconds (using BigInt for large values)
  • Format milliseconds to human-readable strings
  • Support for extremely large time values (up to eras - billions of years)
  • Combine multiple time units in a single string (e.g., "1 hour 30 minutes")
  • More precise calculations, accounting for leap years
  • TypeScript support with improved type safety
  • Separate parse and format functions for flexible usage
  • Support for decades, centuries, millennia, epochs, and eras
  • Custom time scales for scientific fields
  • Support for BigInt to handle extremely large numbers
  • Scientific notation support for input values

Installation

npm install @anonymort/ms-long

Usage

import msLong, { parse, format } from '@anonymort/ms-long';

// Parse time strings
console.log(msLong('2 hours 30 minutes')); // 9000000n

// Format milliseconds
console.log(msLong(9000000n)); // '2h 30m'
console.log(msLong(9000000n, { long: true })); // '2 hours 30 minutes'

// Parse complex time strings with large units
console.log(parse('2 epochs 1 era')); // 1002000000000000000000n

// Format large values
console.log(format(1002000000000000000000n, { long: true })); 
// '1 era 2 epochs'

// Use scientific notation
console.log(parse('1.5e9 years')); // 47304000000000000000000n

// Custom time scales
const customUnits = {
  galacticYear: 230000000000n * 365n * 24n * 60n * 60n * 1000n, // ~230 million years
};

console.log(parse('2 galacticYears', customUnits)); // 14515200000000000000000n

console.log(format(14515200000000000000000n, { long: true, customUnits }));
// '2 galacticYears'

API

msLong(value: string | number | bigint, options?: { long?: boolean; customUnits?: Record<string, bigint> }): string | bigint

The main function that can both parse and format time values.

  • When given a string, it parses it to milliseconds (as BigInt).
  • When given a number or BigInt, it formats it to a time string.
  • The long option determines whether to use long format (e.g., "2 hours" vs "2h").
  • The customUnits option allows for defining custom time units.

parse(value: string, customUnits?: Record<string, bigint>): bigint

Parses a time string to milliseconds (as BigInt). Supports multiple units and extremely large time values.

format(ms: bigint | number, options?: { long?: boolean; customUnits?: Record<string, bigint> }): string

Formats milliseconds (as BigInt or number) to a time string. Can handle extremely large values and provides precise output for large durations.

Supported Units

  • Millisecond(s)
  • Second(s)
  • Minute(s)
  • Hour(s)
  • Day(s)
  • Week(s)
  • Month(s)
  • Year(s)
  • Decade(s)
  • Century(ies)
  • Millennium(a)
  • Epoch(s) (millions of years)
  • Era(s) (billions of years)

Custom units can be defined as needed.

Scientific and Large-Scale Time Handling

  • Supports parsing and formatting of time spans up to billions of years
  • Uses BigInt for precise calculations with extremely large numbers
  • Allows scientific notation input (e.g., "1.5e9 years")
  • Custom time scales can be defined for specific scientific applications

Differences from original ms library

  • Supports much larger time values (up to eras - billions of years)
  • Allows combining multiple units in a single string
  • More precise calculations for large time values using BigInt
  • Separate parse and format functions exposed
  • Written in TypeScript for improved type safety
  • Throws more informative errors for invalid inputs
  • Includes 'decade', 'century', 'millennium', 'epoch', and 'era' as time units
  • Supports custom time scales and units

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.