UNPKG

1.45 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Converts human friendly time to milliseconds. Supports the format
5 * 00:00:00.000 for hours, minutes, seconds, and milliseconds respectively.
6 * And 0ms, 0s, 0m, 0h, and together 1m1s.
7 */
8const numberFormat = /^\d+$/;
9const timeFormat = /^(?:(?:(\d+):)?(\d{1,2}):)?(\d{1,2})(?:\.(\d{3}))?$/;
10const timeUnits = {
11 ms: 1,
12 s: 1000,
13 m: 60000,
14 h: 3600000,
15};
16exports.humanStr = (time) => {
17 if (typeof time === 'number') {
18 return time;
19 }
20 if (numberFormat.test(time)) {
21 return +time;
22 }
23 const firstFormat = timeFormat.exec(time);
24 if (firstFormat) {
25 return +(firstFormat[1] || 0) * timeUnits.h +
26 +(firstFormat[2] || 0) * timeUnits.m +
27 +firstFormat[3] * timeUnits.s +
28 +(firstFormat[4] || 0);
29 }
30 else {
31 let total = 0;
32 const r = /(-?\d+)(ms|s|m|h)/g;
33 let rs;
34 while ((rs = r.exec(time)) != null) {
35 total += +rs[1] * timeUnits[rs[2]];
36 }
37 return total;
38 }
39};
40/**
41 * Parses a duration string in the form of "123.456S", returns milliseconds.
42 */
43exports.durationStr = (time) => {
44 let total = 0;
45 const r = /(\d+(?:\.\d+)?)(S|M|H)/g;
46 let rs;
47 while ((rs = r.exec(time)) != null) {
48 total += +rs[1] * timeUnits[rs[2].toLowerCase()];
49 }
50 return total;
51};
52//# sourceMappingURL=parse-time.js.map
\No newline at end of file