-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatDuration.js
47 lines (43 loc) · 1.86 KB
/
formatDuration.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
const moment = require('moment');
const formatNumber = require('./formatNumber');
let warnedAboutFormatDuration = false;
module.exports = function formatDuration(
forceMinutes = false,
customFormat = null,
forceMilliseconds = false,
highResSeconds = false
) {
return (millisecondArg) => {
if (!warnedAboutFormatDuration) {
console.warn(
'"formatDuration" is flawed as it formats a date not a quantity of time! Check your implementation to ensure this is what you want. If not you should use "humanizeDuration"'
);
warnedAboutFormatDuration = true;
}
let formattedDuration = '0s';
const milliseconds = parseFloat(millisecondArg);
const date = moment().startOf('year').milliseconds(parseInt(milliseconds));
const elapsed = moment.duration(milliseconds);
if (customFormat) {
formattedDuration = date.format(customFormat);
} else if (elapsed.asHours() >= 1) {
formattedDuration = date.format('h[h] mm[m]');
} else if (elapsed.asMinutes() >= 1) {
formattedDuration = date.format('m[m]');
} else if (forceMilliseconds) {
formattedDuration = date.format(
`${milliseconds > 9999 ? 's' : ''}${milliseconds > 999 ? 's,' : ''}SSS[ms]`
);
} else if (forceMinutes) {
formattedDuration =
date.format('m') + '.' + Math.round((10 * elapsed.format('ss')) / 60) + 'm';
} else if (elapsed.asSeconds() >= 1) {
formattedDuration = highResSeconds
? `${formatNumber(1, 2)(elapsed.asSeconds())}s`
: date.format('s[s]');
} else if (elapsed.asMilliseconds() >= 1) {
formattedDuration = `${formatNumber(1, 2)(elapsed.asSeconds())}s`;
}
return formattedDuration;
};
}