-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
44 lines (35 loc) · 1021 Bytes
/
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
function quantify (data, unit, value, allowZero) {
if (value || (allowZero && !value)) {
if (value > 1 || value < -1 || value === 0) {
unit += 's'
}
data.push(value + ' ' + unit)
}
return data
}
function fix10 (number) {
return number.toFixed(10)
}
module.exports = function prettySeconds (seconds) {
let prettyString = ''
let data = []
if (typeof seconds === 'number') {
data = quantify(data, 'day', parseInt(fix10(seconds / 86400)))
data = quantify(data, 'hour', parseInt(fix10((seconds % 86400) / 3600)))
data = quantify(data, 'minute', parseInt(fix10((seconds % 3600) / 60)))
data = quantify(data, 'second', Math.floor(seconds % 60), data.length < 1)
const length = data.length
let i
for (i = 0; i < length; i++) {
if (prettyString.length > 0) {
if (i === length - 1) {
prettyString += ' and '
} else {
prettyString += ', '
}
}
prettyString += data[i]
}
}
return prettyString
}