/
index.js
142 lines (120 loc) · 3.88 KB
/
index.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
const parseMilliseconds = require('parse-ms');
const pluralize = (word, count) => count === 1 ? word : `${word}s`;
module.exports = (milliseconds, options = {}) => {
if (!Number.isFinite(milliseconds)) {
throw new TypeError('Expected a finite number');
}
if (options.colonNotation) {
options.compact = false;
options.formatSubMilliseconds = false;
options.separateMilliseconds = false;
options.verbose = false;
}
if (options.compact) {
options.secondsDecimalDigits = 0;
options.millisecondsDecimalDigits = 0;
}
const result = [];
const add = (value, long, short, valueString) => {
if ((result.length === 0 || !options.colonNotation) && value === 0 && !(options.colonNotation && short === 'm')) {
return;
}
valueString = (valueString || value || '0').toString();
let prefix;
let suffix;
if (options.colonNotation) {
prefix = result.length > 0 ? ':' : '';
suffix = '';
const wholeDigits = valueString.includes('.') ? valueString.split('.')[0].length : valueString.length;
const minLength = result.length > 0 ? 2 : 1;
valueString = '0'.repeat(Math.max(0, minLength - wholeDigits)) + valueString;
} else {
prefix = '';
suffix = options.verbose ? ' ' + pluralize(long, value) : short;
}
result.push(prefix + valueString + suffix);
};
const secondsDecimalDigits =
typeof options.secondsDecimalDigits === 'number' ?
options.secondsDecimalDigits :
1;
if (secondsDecimalDigits < 1) {
const difference = 1000 - (milliseconds % 1000);
if (difference < 500) {
milliseconds += difference;
}
}
// Round up milliseconds for values lager than 1 minute - 50ms since these
// always need to be round up. This fixes issues when rounding seconds
// independently of minutes later on.
if (
milliseconds >= (1000 * 60) - 50 &&
!options.separateMilliseconds &&
!options.formatSubMilliseconds
) {
const difference = 60 - (milliseconds % 60);
if (difference <= 50) {
milliseconds += difference;
}
}
const parsed = parseMilliseconds(milliseconds);
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 (
options.separateMilliseconds ||
options.formatSubMilliseconds ||
milliseconds < 1000
) {
add(parsed.seconds, 'second', 's');
if (options.formatSubMilliseconds) {
add(parsed.milliseconds, 'millisecond', 'ms');
add(parsed.microseconds, 'microsecond', 'µs');
add(parsed.nanoseconds, 'nanosecond', 'ns');
} else {
const millisecondsAndBelow =
parsed.milliseconds +
(parsed.microseconds / 1000) +
(parsed.nanoseconds / 1e6);
const millisecondsDecimalDigits =
typeof options.millisecondsDecimalDigits === 'number' ?
options.millisecondsDecimalDigits :
0;
const roundedMiliseconds = millisecondsAndBelow >= 1 ?
Math.round(millisecondsAndBelow) :
Math.ceil(millisecondsAndBelow);
const millisecondsString = millisecondsDecimalDigits ?
millisecondsAndBelow.toFixed(millisecondsDecimalDigits) :
roundedMiliseconds;
add(
parseFloat(millisecondsString, 10),
'millisecond',
'ms',
millisecondsString
);
}
} else {
const seconds = (milliseconds / 1000) % 60;
const secondsDecimalDigits =
typeof options.secondsDecimalDigits === 'number' ?
options.secondsDecimalDigits :
1;
const secondsFixed = seconds.toFixed(secondsDecimalDigits);
const secondsString = options.keepDecimalsOnWholeSeconds ?
secondsFixed :
secondsFixed.replace(/\.0+$/, '');
add(parseFloat(secondsString, 10), 'second', 's', secondsString);
}
if (result.length === 0) {
return '0' + (options.verbose ? ' milliseconds' : 'ms');
}
if (options.compact) {
return result[0];
}
if (typeof options.unitCount === 'number') {
return result.slice(0, Math.max(options.unitCount, 1)).join(' ');
}
return options.colonNotation ? result.join('') : result.join(' ');
};