1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
'use strict';
const parseMs = require('parse-ms');
const plur = require('plur');
module.exports = (ms, opts) => {
if (!Number.isFinite(ms)) {
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');
}
const ret = [];
const add = (val, long, short, valStr) => {
if (val === 0) {
return;
}
const postfix = opts.verbose ? ' ' + plur(long, val) : short;
ret.push((valStr || val) + postfix);
};
const parsed = parseMs(ms);
add(Math.trunc(parsed.days / 365), 'year', 'y');
add(parsed.days % 365, 'day', 'd');
add(parsed.hours, 'hour', 'h');
add(parsed.minutes, 'minute', 'm');
if (opts.compact) {
add(parsed.seconds, 'second', 's');
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);
return ret.join(' ');
};
|