Comparing version 8.0.0 to 9.0.0
@@ -1,2 +0,2 @@ | ||
export interface Options { | ||
export type Options = { | ||
/** | ||
@@ -78,3 +78,3 @@ Number of digits to appear after the seconds decimal point. | ||
readonly colonNotation?: boolean; | ||
} | ||
}; | ||
@@ -121,5 +121,5 @@ /** | ||
export default function prettyMilliseconds( | ||
milliseconds: number, | ||
milliseconds: number | bigint, | ||
options?: Options | ||
): string; | ||
80
index.js
import parseMilliseconds from 'parse-ms'; | ||
const pluralize = (word, count) => count === 1 ? word : `${word}s`; | ||
const isZero = value => value === 0 || value === 0n; | ||
const pluralize = (word, count) => (count === 1 || count === 1n) ? word : `${word}s`; | ||
const SECOND_ROUNDING_EPSILON = 0.000_000_1; | ||
const ONE_DAY_IN_MILLISECONDS = 24n * 60n * 60n * 1000n; | ||
export default function prettyMilliseconds(milliseconds, options = {}) { | ||
if (!Number.isFinite(milliseconds)) { | ||
throw new TypeError('Expected a finite number'); | ||
export default function prettyMilliseconds(milliseconds, options) { | ||
const isBigInt = typeof milliseconds === 'bigint'; | ||
if (!isBigInt && !Number.isFinite(milliseconds)) { | ||
throw new TypeError('Expected a finite number or bigint'); | ||
} | ||
options = {...options}; | ||
if (options.colonNotation) { | ||
@@ -20,2 +25,3 @@ options.compact = false; | ||
if (options.compact) { | ||
options.unitCount = 1; | ||
options.secondsDecimalDigits = 0; | ||
@@ -25,3 +31,3 @@ options.millisecondsDecimalDigits = 0; | ||
const result = []; | ||
let result = []; | ||
@@ -35,12 +41,11 @@ const floorDecimals = (value, decimalDigits) => { | ||
const add = (value, long, short, valueString) => { | ||
if ((result.length === 0 || !options.colonNotation) && value === 0 && !(options.colonNotation && short === 'm')) { | ||
if ( | ||
(result.length === 0 || !options.colonNotation) | ||
&& isZero(value) | ||
&& !(options.colonNotation && short === 'm')) { | ||
return; | ||
} | ||
valueString = (valueString || value || '0').toString(); | ||
let prefix; | ||
let suffix; | ||
valueString = valueString ?? String(value); | ||
if (options.colonNotation) { | ||
prefix = result.length > 0 ? ':' : ''; | ||
suffix = ''; | ||
const wholeDigits = valueString.includes('.') ? valueString.split('.')[0].length : valueString.length; | ||
@@ -50,15 +55,15 @@ const minLength = result.length > 0 ? 2 : 1; | ||
} else { | ||
prefix = ''; | ||
suffix = options.verbose ? ' ' + pluralize(long, value) : short; | ||
valueString += options.verbose ? ' ' + pluralize(long, value) : short; | ||
} | ||
result.push(prefix + valueString + suffix); | ||
result.push(valueString); | ||
}; | ||
const parsed = parseMilliseconds(milliseconds); | ||
const days = BigInt(parsed.days); | ||
add(Math.trunc(parsed.days / 365), 'year', 'y'); | ||
add(parsed.days % 365, 'day', 'd'); | ||
add(parsed.hours, 'hour', 'h'); | ||
add(parsed.minutes, 'minute', 'm'); | ||
add(days / 365n, 'year', 'y'); | ||
add(days % 365n, 'day', 'd'); | ||
add(Number(parsed.hours), 'hour', 'h'); | ||
add(Number(parsed.minutes), 'minute', 'm'); | ||
@@ -70,12 +75,18 @@ if ( | ||
) { | ||
add(parsed.seconds, 'second', 's'); | ||
const seconds = Number(parsed.seconds); | ||
const milliseconds = Number(parsed.milliseconds); | ||
const microseconds = Number(parsed.microseconds); | ||
const nanoseconds = Number(parsed.nanoseconds); | ||
add(seconds, 'second', 's'); | ||
if (options.formatSubMilliseconds) { | ||
add(parsed.milliseconds, 'millisecond', 'ms'); | ||
add(parsed.microseconds, 'microsecond', 'µs'); | ||
add(parsed.nanoseconds, 'nanosecond', 'ns'); | ||
add(milliseconds, 'millisecond', 'ms'); | ||
add(microseconds, 'microsecond', 'µs'); | ||
add(nanoseconds, 'nanosecond', 'ns'); | ||
} else { | ||
const millisecondsAndBelow | ||
= parsed.milliseconds | ||
+ (parsed.microseconds / 1000) | ||
+ (parsed.nanoseconds / 1e6); | ||
= milliseconds | ||
+ (microseconds / 1000) | ||
+ (nanoseconds / 1e6); | ||
@@ -87,3 +98,3 @@ const millisecondsDecimalDigits | ||
const roundedMiliseconds = millisecondsAndBelow >= 1 | ||
const roundedMilliseconds = millisecondsAndBelow >= 1 | ||
? Math.round(millisecondsAndBelow) | ||
@@ -94,3 +105,3 @@ : Math.ceil(millisecondsAndBelow); | ||
? millisecondsAndBelow.toFixed(millisecondsDecimalDigits) | ||
: roundedMiliseconds; | ||
: roundedMilliseconds; | ||
@@ -105,3 +116,6 @@ add( | ||
} else { | ||
const seconds = (milliseconds / 1000) % 60; | ||
const seconds = ( | ||
(isBigInt ? Number(milliseconds % ONE_DAY_IN_MILLISECONDS) : milliseconds) | ||
/ 1000 | ||
) % 60; | ||
const secondsDecimalDigits | ||
@@ -122,12 +136,8 @@ = typeof options.secondsDecimalDigits === 'number' | ||
if (options.compact) { | ||
return result[0]; | ||
} | ||
const separator = options.colonNotation ? ':' : ' '; | ||
if (typeof options.unitCount === 'number') { | ||
const separator = options.colonNotation ? '' : ' '; | ||
return result.slice(0, Math.max(options.unitCount, 1)).join(separator); | ||
result = result.slice(0, Math.max(options.unitCount, 1)); | ||
} | ||
return options.colonNotation ? result.join('') : result.join(' '); | ||
return result.join(separator); | ||
} |
{ | ||
"name": "pretty-ms", | ||
"version": "8.0.0", | ||
"version": "9.0.0", | ||
"description": "Convert milliseconds to a human readable string: `1337000000` → `15d 11h 23m 20s`", | ||
@@ -14,6 +14,9 @@ "license": "MIT", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"types": "./index.d.ts", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"sideEffects": false, | ||
"engines": { | ||
"node": ">=14.16" | ||
"node": ">=18" | ||
}, | ||
@@ -46,9 +49,9 @@ "scripts": { | ||
"dependencies": { | ||
"parse-ms": "^3.0.0" | ||
"parse-ms": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^4.3.0", | ||
"tsd": "^0.20.0", | ||
"xo": "^0.49.0" | ||
"ava": "^6.0.1", | ||
"tsd": "^0.30.4", | ||
"xo": "^0.56.0" | ||
} | ||
} |
@@ -19,2 +19,5 @@ # pretty-ms | ||
prettyMilliseconds(1337000000n); | ||
//=> '15d 11h 23m 20s' | ||
prettyMilliseconds(1337); | ||
@@ -53,3 +56,3 @@ //=> '1.3s' | ||
Type: `number` | ||
Type: `number | bigint` | ||
@@ -56,0 +59,0 @@ Milliseconds to humanize. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12176
200
146
+ Addedparse-ms@4.0.0(transitive)
- Removedparse-ms@3.0.0(transitive)
Updatedparse-ms@^4.0.0