-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
102 lines (89 loc) · 1.75 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
'use strict';
var regex = new RegExp('^((?:\\d+)?\\.?\\d+) *('+ [
'milliseconds?',
'msecs?',
'ms',
'seconds?',
'secs?',
's',
'minutes?',
'mins?',
'm',
'hours?',
'hrs?',
'h',
'days?',
'd',
'weeks?',
'wks?',
'w',
'years?',
'yrs?',
'y'
].join('|') +')?$', 'i');
var second = 1000
, minute = second * 60
, hour = minute * 60
, day = hour * 24
, week = day * 7
, year = day * 365;
/**
* Parse a time string and return the number value of it.
*
* @param {String} ms Time string.
* @returns {Number}
* @api private
*/
module.exports = function millisecond(ms) {
var type = typeof ms
, amount
, match;
if ('number' === type) return ms;
if ('string' !== type) return 0;
if (!isNaN(+ms)) return +ms;
//
// We are vulnerable to the regular expression denial of service (ReDoS).
// In order to mitigate this we don't parse the input string if it is too long.
// See https://nodesecurity.io/advisories/46.
//
if (ms.length > 10000 || !(match = regex.exec(ms))) return 0;
amount = parseFloat(match[1]);
switch (match[2].toLowerCase()) {
case 'years':
case 'year':
case 'yrs':
case 'yr':
case 'y':
return amount * year;
case 'weeks':
case 'week':
case 'wks':
case 'wk':
case 'w':
return amount * week;
case 'days':
case 'day':
case 'd':
return amount * day;
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
return amount * hour;
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
return amount * minute;
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
return amount * second;
default:
return amount;
}
};