Socket
Socket
Sign inDemoInstall

pretty-ms

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pretty-ms - npm Package Compare versions

Comparing version 3.2.0 to 4.0.0

62

index.js
'use strict';
const parseMs = require('parse-ms');
const plur = (word, count) => count === 1 ? word : word + 's';
const pluralize = (word, count) => count === 1 ? word : word + 's';
module.exports = (ms, opts) => {
module.exports = (ms, options = {}) => {
if (!Number.isFinite(ms)) {

@@ -11,7 +11,5 @@ throw new TypeError('Expected a finite number');

opts = opts || {};
if (ms < 1000) {
const msDecimalDigits = typeof opts.msDecimalDigits === 'number' ? opts.msDecimalDigits : 0;
return (msDecimalDigits ? ms.toFixed(msDecimalDigits) : Math.ceil(ms)) + (opts.verbose ? ' ' + plur('millisecond', Math.ceil(ms)) : 'ms');
if (options.compact) {
options.secDecimalDigits = 0;
options.msDecimalDigits = 0;
}

@@ -21,12 +19,21 @@

const add = (val, long, short, valStr) => {
if (val === 0) {
const add = (value, long, short, valueString) => {
if (value === 0) {
return;
}
const postfix = opts.verbose ? ' ' + plur(long, val) : short;
const postfix = options.verbose ? ' ' + pluralize(long, value) : short;
ret.push((valStr || val) + postfix);
ret.push((valueString || value) + postfix);
};
const secDecimalDigits = typeof options.secDecimalDigits === 'number' ? options.secDecimalDigits : 1;
if (secDecimalDigits < 1) {
const diff = 1000 - (ms % 1000);
if (diff < 500) {
ms += diff;
}
}
const parsed = parseMs(ms);

@@ -39,14 +46,35 @@

if (opts.compact) {
if (options.separateMs || options.formatSubMs || ms < 1000) {
add(parsed.seconds, 'second', 's');
if (options.formatSubMs) {
add(parsed.milliseconds, 'millisecond', 'ms');
add(parsed.microseconds, 'microsecond', 'µs');
add(parsed.nanoseconds, 'nanosecond', 'ns');
} else {
const msAndBelow = parsed.milliseconds + (parsed.microseconds / 1000) + (parsed.nanoseconds / 1e6);
const msDecimalDigits = typeof options.msDecimalDigits === 'number' ? options.msDecimalDigits : 0;
const msStr = msDecimalDigits ? msAndBelow.toFixed(msDecimalDigits) : Math.ceil(msAndBelow);
add(parseFloat(msStr, 10), 'millisecond', 'ms', msStr);
}
} else {
const sec = ms / 1000 % 60;
const secDecimalDigits = typeof options.secDecimalDigits === 'number' ? options.secDecimalDigits : 1;
const secFixed = sec.toFixed(secDecimalDigits);
const secStr = options.keepDecimalsOnWholeSeconds ? secFixed : secFixed.replace(/\.0+$/, '');
add(parseFloat(secStr, 10), 'second', 's', secStr);
}
if (ret.length === 0) {
return '0' + (options.verbose ? ' milliseconds' : 'ms');
}
if (options.compact) {
return '~' + ret[0];
}
const sec = ms / 1000 % 60;
const secDecimalDigits = typeof opts.secDecimalDigits === 'number' ? opts.secDecimalDigits : 1;
const secFixed = sec.toFixed(secDecimalDigits);
const secStr = opts.keepDecimalsOnWholeSeconds ? secFixed : secFixed.replace(/\.0+$/, '');
add(sec, 'second', 's', secStr);
if (typeof options.unitCount === 'number') {
return '~' + ret.slice(0, Math.max(options.unitCount, 1)).join(' ');
}
return ret.join(' ');
};
{
"name": "pretty-ms",
"version": "3.2.0",
"description": "Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s`",
"license": "MIT",
"repository": "sindresorhus/pretty-ms",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"pretty",
"prettify",
"human",
"humanize",
"humanized",
"readable",
"time",
"ms",
"milliseconds",
"duration",
"period",
"range",
"text",
"string",
"str",
"number",
"hrtime"
],
"dependencies": {
"parse-ms": "^1.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "pretty-ms",
"version": "4.0.0",
"description": "Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s`",
"license": "MIT",
"repository": "sindresorhus/pretty-ms",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"pretty",
"prettify",
"human",
"humanize",
"humanized",
"readable",
"time",
"ms",
"milliseconds",
"duration",
"period",
"range",
"text",
"string",
"str",
"number",
"hrtime"
],
"dependencies": {
"parse-ms": "^2.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"xo": "^0.23.0"
}
}

@@ -6,3 +6,3 @@ # pretty-ms [![Build Status](https://travis-ci.org/sindresorhus/pretty-ms.svg?branch=master)](https://travis-ci.org/sindresorhus/pretty-ms)

## Usage
## Install

@@ -13,2 +13,5 @@ ```

## Usage
```js

@@ -26,11 +29,15 @@ const prettyMs = require('pretty-ms');

// compact option
// `compact` option
prettyMs(1337, {compact: true});
//=> '~1s'
// verbose option
// `verbose` option
prettyMs(1335669000, {verbose: true});
//=> '15 days 11 hours 1 minute 9 seconds'
// can be useful for time durations
// `formatSubMs` option
prettyMs(100.400080, {formatSubMs: true})
//=> '100ms 400µs 80ns'
// Can be useful for time durations
prettyMs(new Date(2014, 0, 1, 10, 40) - new Date(2014, 0, 1, 10, 5))

@@ -86,3 +93,11 @@ //=> '35m'

Only show the first unit: `1h 10m` → `~1h`.
Also ensures that `msDecimalDigits` and `secDecimalDigits` are both set to `0`.
##### unitCount
Type: `number`<br>
Default: `Infinity`
Number of units to show. Setting `compact` to `true` overrides this option.
##### verbose

@@ -95,6 +110,22 @@

##### separateMs
Type: `boolean`<br>
Default: `false`
Show milliseconds separately. This means they won't be included in the decimal part of the seconds.
##### formatSubMs
Type: `boolean`<br>
Default: `false`
Show microseconds and nanoseconds.
## Related
- [pretty-ms-cli](https://github.com/sindresorhus/pretty-ms-cli) - CLI for this module
- [parse-ms](https://github.com/sindresorhus/parse-ms) - Parse milliseconds into an object
- [to-milliseconds](https://github.com/sindresorhus/to-milliseconds) - Convert an object of time properties to milliseconds

@@ -101,0 +132,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc