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

@gae-labs/better-ms

v2.0.0

Published

Millisecond conversion tool with support for multiple time units in a single input string

Downloads

19

Readme

BetterMsJs

Based on vercel/ms @ v2.1.3

CI

Use this package to easily convert various time formats to milliseconds.

Comparison with ms

BetterMsJs is a fork of the original ms package. BetterMsJs implements the support for multiple time units in a single input string, such as 1d6h30m, and support for multiple units when formatting a human-readable string from milliseconds. This allows for more flexibility in the input format. The original ms package only supports a single time unit in the input string, and rounds the output string to the nearest unit.

Breaking Changes compared to ms

  • This package now returns a ms value on inputs like 1d 2h 3m 4s instead of NaN, this could be a breaking change if you were relying on the previous behavior.
  • This package now returns NaN on inputs that contain multiple decimal points like 1.2.3.4s instead of undefined, this could be a breaking change if you were relying on the previous behavior.
  • This package now accepts inputs greater than 100 characters, this could be a breaking change if you were relying on the previous behavior.

Installation

npm install @gae-labs/better-ms

Usage

const betterMs = require('@gae-labs/better-ms');

betterMs('2 days 6 hours 30 minutes 15 seconds')    // 196215000
betterMs(196215000)                                 // "2d 6h 30m 15s"

Parse to Milliseconds

betterMs('2 days 6 hours 30 minutes 15 seconds')    // 196215000
betterMs('1d 2h 3m 4s')                             // 93784000
betterMs('2 days')                                  // 172800000
betterMs('1d')                                      // 86400000
betterMs('10h')                                     // 36000000
betterMs('2.5 hrs')                                 // 9000000
betterMs('2h')                                      // 7200000
betterMs('1m')                                      // 60000
betterMs('5s')                                      // 5000
betterMs('1y')                                      // 31557600000
betterMs('100')                                     // 100
betterMs('-3 days')                                 // -259200000
betterMs('-1h')                                     // -3600000
betterMs('-200')                                    // -200

Supported Units for Parsing

  • Years: y, yr, yrs, year, years
  • Months: mo, mos, mth, mths, month, months
  • Weeks: w, week, weeks
  • Days: d, day, days
  • Hours: h, hr, hrs, hour, hours
  • Minutes: m, min, mins, minute, minutes
  • Seconds: s, sec, secs, second, seconds
  • Milliseconds: ms, msec, msecs, millisecond, milliseconds

Format to Human-Readable String

betterMs(60000)                                     // "1m"
betterMs(1000 * 60 * 60 * 24)                       // "1d"
betterMs(betterMs('10 hours'))                      // "10h"
betterMs(1000 * 60 * 60 * 36)                       // "1d 12h"
betterMs(1000 * 60 * 60 * 36, { long: true })       // "1 day 12 hours"
betterMs(1000 * 60 * 60 * 36, { compact: true })    // "1d12h"
betterMs(1000 * 60 * 60 * 36, { extraSpace: true }) // "1 d 12 h"
betterMs(1000 * 60 * 60 * 36, { exclude: ['d'] })   // "36h"
betterMs(1000 * 60 * 60 * 36, { plural: false })    // "1 day 12 hour"
betterMs(1000 * 60 * 60 * 36, { short: true })      // "1d"
betterMs(1000 * 60 * 60 * 36, { object: true })     // { d: 1, h: 12 }
betterMs(1000 * 60 * 60 * 48, { minimum: 2 })       // "2d"
betterMs(1000 * 60 * 60 * 47, { minimum: 2 })       // "47h"

Options for Formatting

  • long verbose formatting [false] (Example: 5 hours instead of 5h)
  • compact compact formatting [false] (Example: 5h30m instead of 5h 30m)
  • extraSpace add extra space between value and unit [true with long and false when !long, unless specified] (Example: 5 h instead of 5h)
  • exclude array of units to exclude [] (Acceptable Inputs: [y, M, w, d, h, m, s, ms])
  • plural pluralize unit [true] [only works with long] (Example: 2 years instead of 2 year)
  • short provide only the most significant unit [false] (Example: 5h 30m 10s would be shown as 5h)
  • minimum minimum amount of a unit required to include that unit [1] (Acceptable Inputs: >= 1) (Example: Input of 1h 30m - Minimum 1 = 1h 30m | Minimum 2 = 90m | Minimum 1.5 = 1h 30m)
  • object To return an object with each unit and value [false] (Expected Output: { y: 1, M: 2, w: 3, d: 4, h: 5, m: 6, s: 7, ms: 8 })

Features

  • Works both in Node.js and in the browser
  • If a number is supplied to ms, a string with a unit is returned
  • If a string that contains the number is supplied, it returns it as a number (e.g.: it returns 100 for '100')
  • If you pass a string with a number and a valid unit, the number of equivalent milliseconds is returned

Caught a Bug?

  1. Fork this repository to your own GitHub account and then clone it to your local device
  2. Link the package to the global module directory: npm link
  3. Within the module you want to test your local development instance of ms, just link it to the dependencies: npm link ms. Instead of the default one from npm, Node.js will now use your clone of ms!

As always, you can run the tests using: npm test