Files
pulsenode/lib/config/duration.ts
T

18 lines
442 B
TypeScript
Raw Normal View History

const DURATION_PATTERN = /^(\d+)(ms|s|m|h)$/;
const UNIT_MS: Record<string, number> = {
ms: 1,
s: 1_000,
m: 60_000,
h: 3_600_000,
};
export function parseDuration(value: string): number {
const match = DURATION_PATTERN.exec(value.trim());
if (!match) {
throw new Error(`Invalid duration "${value}" (expected e.g. "500ms", "5s", "1m", "1h")`);
}
const [, amount, unit] = match;
return Number(amount) * UNIT_MS[unit];
}