mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-11 10:57:07 +00:00
Merge main into project migration branch
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Parse duration string to milliseconds.
|
||||
* Supports formats like: 5m, 30s, 1h, 2h30m, 90, etc.
|
||||
* Supports formats like: 5m, 30s, 1h, 2h30m, 1d, 90, etc.
|
||||
* If no unit is specified, assumes seconds.
|
||||
*/
|
||||
export function parseDuration(input: string): number {
|
||||
@@ -11,14 +11,16 @@ export function parseDuration(input: string): number {
|
||||
return parseInt(trimmed, 10) * 1000;
|
||||
}
|
||||
|
||||
if (!/^(?:\d+[smhd])+$/.test(trimmed)) {
|
||||
throw new Error(`Invalid duration format: ${input}. Use formats like: 5m, 30s, 1h, 2h30m, 1d`);
|
||||
}
|
||||
|
||||
// Parse duration with units
|
||||
let totalMs = 0;
|
||||
const regex = /(\d+)([smh])/g;
|
||||
const regex = /(\d+)([smhd])/g;
|
||||
let match;
|
||||
let hasMatch = false;
|
||||
|
||||
while ((match = regex.exec(trimmed)) !== null) {
|
||||
hasMatch = true;
|
||||
const value = parseInt(match[1], 10);
|
||||
const unit = match[2];
|
||||
|
||||
@@ -32,12 +34,11 @@ export function parseDuration(input: string): number {
|
||||
case "h":
|
||||
totalMs += value * 60 * 60 * 1000;
|
||||
break;
|
||||
case "d":
|
||||
totalMs += value * 24 * 60 * 60 * 1000;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasMatch) {
|
||||
throw new Error(`Invalid duration format: ${input}. Use formats like: 5m, 30s, 1h, 2h30m`);
|
||||
}
|
||||
|
||||
return totalMs;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user