1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const numberFormat = /^\d+$/;
|
9 | const timeFormat = /^(?:(?:(\d+):)?(\d{1,2}):)?(\d{1,2})(?:\.(\d{3}))?$/;
|
10 | const timeUnits = {
|
11 | ms: 1,
|
12 | s: 1000,
|
13 | m: 60000,
|
14 | h: 3600000,
|
15 | };
|
16 | exports.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 |
|
42 |
|
43 | exports.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 |
|
\ | No newline at end of file |