-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathindex.js
37 lines (30 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
import en from './locale/en.js'
const durationRE = /((?:\d{1,16}(?:\.\d{1,16})?|\.\d{1,16})(?:[eE][-+]?\d{1,4})?)\s?([\p{L}]{0,14})/gu
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
String(str)
.replace(new RegExp(`(\\d)[${parse.unit.placeholder}${parse.unit.group}](\\d)`, 'g'), '$1$2') // clean up group separators / placeholders
.replace(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 (const u in parse.unit) if (parse.unit[u] < prevUnits) { units = u; break }
}
else units = format
}
else units = units.toLowerCase()
prevUnits = units = parse.unit[units] || parse.unit[units.replace(/s$/, '')]
if (units) result = (result || 0) + n * units
})
return result && ((result / (parse.unit[format] || 1)) * (str[0] === '-' ? -1 : 1))
}