(duration: string)
| 10 | const PAIR_SPLIT = /^([0-9]+)([dhmsu]+)$/; |
| 11 | |
| 12 | export function parseDurationToMs(duration: string): number { |
| 13 | const matches: string[] = []; |
| 14 | |
| 15 | let array: RegExpExecArray | null; |
| 16 | while ((array = PARSE_TO_PAIRS.exec(duration)) !== null) { |
| 17 | matches.push(array[0]); |
| 18 | } |
| 19 | return matches |
| 20 | .map((match) => { |
| 21 | const res = PAIR_SPLIT.exec(match); |
| 22 | if (res === null) { |
| 23 | throw new Error(`Not a valid duration: ${match}`); |
| 24 | } |
| 25 | let factor: number = 0; |
| 26 | switch (res[2]) { |
| 27 | case 'd': |
| 28 | factor = 86400000; |
| 29 | break; |
| 30 | case 'h': |
| 31 | factor = 3600000; |
| 32 | break; |
| 33 | case 'm': |
| 34 | factor = 60000; |
| 35 | break; |
| 36 | case 's': |
| 37 | factor = 1000; |
| 38 | break; |
| 39 | case 'u': |
| 40 | factor = 1; |
| 41 | break; |
| 42 | default: |
| 43 | throw new Error(`Not a valid duration unit: ${res[2]}`); |
| 44 | } |
| 45 | return parseInt(res[1]) * factor; |
| 46 | }) |
| 47 | .reduce((total, value) => total + value, 0); |
| 48 | } |
no test coverage detected
searching dependent graphs…