Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

TypeScript · 25 lines · 879 B · at 355e324

// Bug: regex alternation tries "m" before "ms" (so "250ms" reads as "250m" + "s"), and unit order/duplicates are never checked.export class DurationError extends Error {  constructor(    public readonly kind: "invalid_duration",    public readonly input: string,  ) {    super(`${kind}: ${input}`);  }}const MULT: Record<string, number> = { d: 86400000, h: 3600000, m: 60000, s: 1000, ms: 1 };export function parseDuration(text: string): number {  const re = /(\d+)(d|h|m|s|ms)/g;  let total = 0;  let consumed = 0;  let m: RegExpExecArray | null;  while ((m = re.exec(text)) !== null) {    if (m.index !== consumed) throw new DurationError("invalid_duration", text);    consumed = re.lastIndex;    total += Number(m[1]) * MULT[m[2]];  }  if (consumed === 0 || consumed !== text.length) throw new DurationError("invalid_duration", text);  return total;}