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

@caldwell619/ms

v0.0.3

Published

Tiny time formatting utility forked from vercel/ms

Downloads

38

Readme

MS

Easily convert various time formats to milliseconds, and milliseconds back to the formats. Forked from vercel/ms

Installation

# Yarn
yarn add @caldwell619/ms

# NPM
npm install --save @caldwell619/ms

Usage

2 main utilities are exposed:

import { convertTimeToMs, convertMsToTime } from '@caldwell619/ms'

The only required argument is the input to be converted.

Accepted types are string | number

Converting logical units to milliseconds

Full list of supported units

import { convertTimeToMs } from '@caldwell619/ms'

convertTimeToMs('5s') // 5000
convertTimeToMs('1m') // 60000
convertTimeToMs('10h') // 36000000
convertTimeToMs('2 days') // 172800000
convertTimeToMs('1y') // 31557600000
convertTimeToMs('-1h') // -3600000

Number as milliseconds

import { convertMsToTime, convertTimeToMs } from '@caldwell619/ms'

convertMsToTime(60000) // "1m"
convertMsToTime(2 * 60000) // "2m"
convertMsToTime(-3 * 60000) // "-3m"
convertMsToTime(convertTimeToMs('10 hours')) // "10h"

Optional Configuration

The config is optional, as is every key in the config

| Argument | Type | | | --------------- | ------------------------------------------ | ----------------------------------------------------- | | long | boolean | If present, will return the long version of the unit. | | preferredUnit | 'ms', 's', 'm', 'h', 'w', 'd', 'mo', 'y' | The supported unit you'd like the value returned in. |

Long

This will print the value as hours, minutes as opposed to h, m.

convertMsToTime(60000, { long: true }) // "1 minute"
convertMsToTime(2 * 60000, { long: true }) // "2 minutes"
convertMsToTime(-3 * 60000, { long: true }) // "-3 minutes"
convertMsToTime(convertTimeToMs('10 hours'), { long: true }) // "10 hours"
Preferred Unit

This is a working progress. It is implemented, but possibly not the way you'd like. Feel free to submit an issue or PR with how you'd like it done.

If provided, it will attempt to match the value with the given unit.

Example

For example, if your value resolves to 15 days, that is also 2 weeks. The default will return as 2 weeks. This is the largest bucket the time can be put into.

This can be overridden with providing your preferredUnit.

convertMsToTime(7200000) // "2h"
convertMsToTime(7200000, { preferredUnit: 'm' }) // "120m"
convertMsToTime(7200000, { preferredUnit: 'm', long: true }) // "120 minutes"
Edge Cases

If your provided unit doesn't meet the requirement, meaning that the value cannot get to a full count of your unit, it will default to the next closest one.

convertMsToTime(7200000) // "2h"
convertMsToTime(7200000, { preferredUnit: 'd' }) // "2h"
convertMsToTime(7200000, { preferredUnit: 'd', long: true }) // "2 hours"

In this example, 2 hours cannot be represented as a > 1 day. This is where the funny business is, because maybe you'd prefer 0.08d instead of defaulting to 2h.

My preference is the latter, so :man_shrugging:

Supported Units

The following unites are supported for convertTimeToMs

Years

years, year, yrs, yr y

Months

months, month, mon, mo

Weeks

weeks, week, wks, wk, w

Days

days, day, d

Hours

hours, hour, hrs, hr, h

Minutes

minutes, minute, mins, min, m

Seconds

seconds, second, secs, sec, s

Milliseconds

milliseconds, millisecond, msecs, msec, ms