-
Notifications
You must be signed in to change notification settings - Fork 0
/
humanizeDuration.js
50 lines (43 loc) · 1.7 KB
/
humanizeDuration.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
const moment = require('moment');
const formatNumber = require('./formatNumber');
const abbreviations = {
millisecond: 'ms',
second: 's',
minute: 'min',
hour: 'hr',
day: 'd',
week: 'wk',
month: 'mo',
year: 'yr',
};
const abbreviateUnit = (unit) => abbreviations[unit] || unit;
// Assumes the duration you're passing in is a quantity of milliseconds
module.exports = function humanizeDuration(useAbbreviation, decimalPlaces) {
useAbbreviation = useAbbreviation || false;
decimalPlaces = decimalPlaces === 0 ? 0 : decimalPlaces || 1;
const thresholds = {
millisecond: 1000,
second: moment.duration(60, 'second').as('milliseconds'),
minute: moment.duration(60, 'minute').as('milliseconds'),
hour: moment.duration(24, 'hour').as('milliseconds'),
day: moment.duration(7, 'day').as('milliseconds'),
week: moment.duration(4, 'week').as('milliseconds'),
month: moment.duration(12, 'month').as('milliseconds'),
year: moment.duration(1, 'year').as('milliseconds'),
};
return (ms) => {
let result = Object.entries(thresholds).find(([, v]) => ms < v);
// If no result, it's greater than 1yr
if (!result) {
result = ['year'];
}
const [_threshold] = result;
const threshold = useAbbreviation ? abbreviateUnit(_threshold) : _threshold;
const count = formatNumber(
1,
decimalPlaces
)(moment.duration(ms).as(_threshold));
const unit = count > 1 && !useAbbreviation ? `${threshold}s` : threshold; // abbreviations don't normally have plural suffixes, so just use threshold if abbreviating
return `${count} ${unit}`;
};
}