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

pretty-large-ms

v1.0.2

Published

like pretty-ms but with support for large values

Downloads

15

Readme

pretty-large-ms

Build Status Coverage Status

This module is written on typescript, and contains the .d.ts file. If you write on typescript: just use it in your project and definitions will be automatically uploaded.

npm i -S pretty-large-ms

About

This module converts large milliseconds to a human readable string up to the millenniums. Like pretty-ms but with support for large values.

Exports

export function prettyLargeMs(
  ms: number|string,
  size?: number,
  replacers?: IReplacers,
  space?: string): string;

export function addUncountableReplacers(replacer: string|Array<string>): void;
export function dropUncountableReplacers(): void;

export const compact = 1;
export const short   = 2;
export const long    = 4;
export const verbose = 10;

export const longReplacers  = { /*...*/ };
export const shortReplacers = { /*...*/ };

export const useSpace    = ' ';
export const notUseSpace = '';

Features

const pretty = require('pretty-large-ms')
const assert = require('assert')

// About DEFAULTS args:
//
//   size     = pretty.short
//   replacer = pretty.longReplacers
//   space    = pretty.useSpace
assert.equal(pretty(1e8), '1 day 3 hours')
assert.equal(
  pretty(1e8, pretty.short, pretty.longReplacers, pretty.useSpace),
  pretty(1e8))

// About SIZE options:
//
//   compact = 1
//   short   = 2
//   long    = 4
//   verbose = 10 (ms..millennium)
// also, you can specify some custom size >= 1

// About REPLACERS:
//
//   shortReplacers = { millenniums: 'MI', centuries: 'C', decades: 'D',
//                     years: 'Y',  months: 'M', days: 'd', hours: 'h',
//                     minutes: 'm', seconds: 's', milliseconds: 'ms' }
//   longReplacers  = { millenniums: 'millennium', centuries: 'century',
//                      ... seconds: 'second', milliseconds: 'ms' }
const shortAct = pretty(1e8, pretty.short, pretty.shortReplacers)
const longAct = pretty(1e8, pretty.short, pretty.longReplacers)
assert.equal(shortAct, '1d 3h')
assert.equal(longAct, '1 day 3 hours')

// also you can specify some custom map
const myRer = { minutes: 'MIN', seconds: 'SEC' }
const short = pretty.short
assert.equal(pretty(1e6, short, myRer), '16 MINS 40 SECS')

// use #addUncountableReplacers (note: use single format of names!)
// if you want to make some of names are uncountable
pretty.addUncountableReplacers([ 'MIN', 'SEC' ])
assert.equal(pretty(1e6, short, myRer), '16 MIN 40 SEC')

// use #dropUncountableReplacers
// to drop all custom uncountable rules
pretty.dropUncountableReplacers()
assert.equal(pretty(1e6, short, myRer), '16 MINS 40 SECS')

// last note about custom replacers:
// if in your map of replacers something is missing,
// it will be complemented by default map.
const actByEmptyReplacers = pretty(1e15 + 1, pretty.verbose, {})
const expByEmptyReplacers =
  '31 millenniums 7 centuries 9 years 9 months ' +
  '19 days 1 hour 46 minutes 40 seconds 1 millisecond'
assert.equal(actByEmptyReplacers, expByEmptyReplacers)

// About SPACE options:
//
//   useSpace = ' '
//   notUseSpace = ''
// if you use longReplacers, default space = useSpace
// if you use shortReplacers, default space = notUseSpace
// if you use custom replacers, default space = useSpace
assert.equal(
  pretty(1e8, pretty.short, pretty.longReplacers, pretty.notUseSpace),
 '1day 3hours')
// also you can use custom space
assert.equal(
  pretty(1e8, pretty.short, pretty.longReplacers, '__'),
 '1__day 3__hours')

// About MS (first argument):
//
// it may be negative or positive number
// or string compatible with https://github.com/nskazki/str2num
assert.equal(pretty(1e9), '11 days 13 hours')
assert.equal(pretty(-1e9), '-11 days -13 hours')
assert.equal(pretty('-1e9'), '-11 days -13 hours')
assert.equal(pretty('-1 000,000,000'), '-11 days -13 hours')

// Last, about module missuse
assert.throws(() => pretty('-123e'), 'string not compatible with str2num')
assert.throws(() => pretty(0, 0), 'size < 1')
assert.throws(() => pretty(0, 1, []), 'wrong type of replacer')
assert.throws(() => pretty(0, 1, {}, []), 'wrong type of space')