-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.js
78 lines (61 loc) · 1.55 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
'use strict'
var durationRE = /(-?(?:\d+\.?\d*|\d*\.?\d+)(?:e[-+]?\d+)?)\s*([\p{L}]*)/uig
module.exports = parse
// enable default import syntax in typescript
module.exports.default = parse
/**
* conversion ratios
*/
parse.year =
parse.yr =
parse.y = 60000 * 60 * 24 * 365.25
parse.month =
parse.b = 60000 * 60 * 24 * (365.25 / 12)
parse.week =
parse.wk =
parse.w = 60000 * 60 * 24 * 7
parse.day =
parse.d = 60000 * 60 * 24
parse.hour =
parse.hr =
parse.h = 60000 * 60
parse.minute =
parse.min =
parse.m = 60000
parse.second =
parse.sec =
parse.s = 1000
parse.millisecond =
parse.millisec =
parse.ms = 1
parse['µs'] =
parse['μs'] =
parse.us =
parse.microsecond = 1 / 1e3
parse.nanosecond =
parse.ns = 1 / 1e6
/**
* convert `str` to ms
*
* @param {String} str
* @param {String} format
* @return {Number}
*/
function parse(str = '', format = 'ms') {
var result = null, prevUnits
// ignore commas/placeholders
str = (str + '').replace(/(\d)[,_](\d)/g, '$1$2')
str.replace(durationRE, function (_, n, units) {
// if no units, find next smallest units or fall back to format value (ms)
if (!units) {
if (prevUnits) {
for (var u in parse) if (parse[u] < prevUnits) { units = u; break }
}
else units = format
}
else units = units.toLowerCase()
units = parse[units] || parse[units.replace(/s$/, '')]
if (units) result = (result || 0) + Math.abs(parseFloat(n, 10)) * units, prevUnits = units
})
return result && ((result / (parse[format] || 1)) * (str[0] === '-' ? -1 : 1))
}