|
1 | 1 | const unit = Object.create(null);
|
2 | 2 |
|
3 | 3 | const m = 60000,
|
4 |
| - h = m * 60, |
5 |
| - d = h * 24, |
6 |
| - y = d * 365.25; |
| 4 | + h = m * 60, |
| 5 | + d = h * 24, |
| 6 | + y = d * 365.25; |
7 | 7 |
|
8 | 8 | unit.year = unit.yr = unit.y = y;
|
9 | 9 | unit.month = unit.mo = unit.mth = y / 12;
|
@@ -37,34 +37,34 @@ export default function parse(str = '', format = 'ms') {
|
37 | 37 |
|
38 | 38 | String(str)
|
39 | 39 | .replace(
|
40 |
| - new RegExp(`(\\d)[${parse.unit.placeholder}${parse.unit.group}](\\d)`, 'g'), |
41 |
| - '$1$2' |
| 40 | + new RegExp(`(\\d)[${parse.unit.placeholder}${parse.unit.group}](\\d)`, 'g'), |
| 41 | + '$1$2' |
42 | 42 | ) // clean up group separators / placeholders
|
43 | 43 | .replace(parse.unit.decimal, '.') // normalize decimal separator
|
44 | 44 | .replace(durationRE, (_, n, units) => {
|
45 |
| - // if no units, find next smallest units or fall back to format value |
46 |
| - // eg. 1h30 -> 1h30m |
47 |
| - if (!units) { |
48 |
| - if (prevUnits) { |
49 |
| - for (const u in parse.unit) { |
50 |
| - if (parse.unit[u] < prevUnits) { |
51 |
| - units = u; |
52 |
| - break; |
53 |
| - } |
| 45 | + // if no units, find next smallest units or fall back to format value |
| 46 | + // eg. 1h30 -> 1h30m |
| 47 | + if (!units) { |
| 48 | + if (prevUnits) { |
| 49 | + for (const u in parse.unit) { |
| 50 | + if (parse.unit[u] < prevUnits) { |
| 51 | + units = u; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + } else { |
| 56 | + units = format; |
| 57 | + } |
| 58 | + } else { |
| 59 | + units = units.toLowerCase(); |
54 | 60 | }
|
55 |
| - } else { |
56 |
| - units = format; |
57 |
| - } |
58 |
| - } else { |
59 |
| - units = units.toLowerCase(); |
60 |
| - } |
61 | 61 |
|
62 |
| - prevUnits = units = parse.unit[units] || parse.unit[units.replace(/s$/, '')]; |
| 62 | + prevUnits = units = parse.unit[units] || parse.unit[units.replace(/s$/, '')]; |
63 | 63 |
|
64 |
| - if (units) { |
65 |
| - result = (result || 0) + n * units; |
66 |
| - } |
| 64 | + if (units) { |
| 65 | + result = (result || 0) + n * units; |
| 66 | + } |
67 | 67 | });
|
68 | 68 |
|
69 | 69 | return result && (result / (parse.unit[format] || 1)) * (str[0] === '-' ? -1 : 1);
|
70 |
| - } |
| 70 | +} |
0 commit comments