-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.js
38 lines (31 loc) · 1.12 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
import en from './locale/en.js'
const durationRE = /(-?(?:\d+\.?\d*|\d*\.?\d+)(?:e[-+]?\d+)?)\s*([\p{L}]*)/uig
parse.unit = en
/**
* convert `str` to ms
*
* @param {String} str
* @param {String} format
* @return {Number}
*/
export default function parse(str = '', format = 'ms') {
let result = null, prevUnits
(str + '')
.replace(/(\d)[_ ](\d)/g, '$1$2') // ignore placeholders
.replaceAll(parse.unit.group, '') // remove group separator
.replaceAll(parse.unit.decimal, '.') // normalize decimal separator
.replace(durationRE, (_, n, units) => {
// if no units, find next smallest units or fall back to format value
// eg. 1h30 -> 1h30m
if (!units) {
if (prevUnits) {
for (var u in parse.unit) if (parse.unit[u] < prevUnits) { units = u; break }
}
else units = format
}
else units = units.toLowerCase()
units = parse.unit[units] || parse.unit[units.replace(/s$/, '')]
if (units) result = (result || 0) + Math.abs(parseFloat(n, 10)) * units, prevUnits = units
})
return result && ((result / (parse.unit[format] || 1)) * (str[0] === '-' ? -1 : 1))
}