-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdurations.ts
More file actions
32 lines (28 loc) · 887 Bytes
/
durations.ts
File metadata and controls
32 lines (28 loc) · 887 Bytes
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
/**
* Parse a duration string (e.g., "1s", "100ms", "5m", "1h", "1d") to milliseconds.
* @throws Error if the duration string is invalid
*/
export function parseDurationToMs(duration: string): number {
const match = duration.match(/^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/);
if (!match) {
throw new Error(
`Invalid duration string: "${duration}". Expected format: number + unit (ms, s, m, h, d)`
);
}
const [, value, unit] = match;
const numValue = parseFloat(value);
switch (unit) {
case "ms":
return Math.round(numValue);
case "s":
return Math.round(numValue * 1000);
case "m":
return Math.round(numValue * 60 * 1000);
case "h":
return Math.round(numValue * 60 * 60 * 1000);
case "d":
return Math.round(numValue * 24 * 60 * 60 * 1000);
default:
throw new Error(`Unknown duration unit: ${unit}`);
}
}