-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
48 lines (38 loc) · 1.05 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
const numbered = require('numbered');
const units = {};
units.second = 1000;
units.minute = units.second * 60;
units.hour = units.minute * 60;
units.day = units.hour * 24;
units.week = units.day * 7;
units.month = units.day * 30;
units.year = units.day * 365;
const regexp = /(second|minute|hour|day|week|month|year)s?/;
const humanInterval = time => {
if (!time || typeof time === 'number') {
return time;
}
let result = Number.NaN;
time = time.replace(/([^a-z\d.-]|and)+/g, ' ');
for (;;) {
const match = time.match(regexp);
if (!match) {
return result;
}
const matchedNumber = time.slice(0, match.index).trim();
const unit = units[match[1]];
let number = 1;
if (matchedNumber.length > 0) {
number = Number.parseFloat(matchedNumber);
if (Number.isNaN(number)) {
number = numbered.parse(matchedNumber);
}
}
if (Number.isNaN(result)) {
result = 0;
}
result += number * unit;
time = time.slice(match.index + match[0].length);
}
};
module.exports = humanInterval;