/** * Clock formatting for the transport. A pure function, kept out of the view for * the same reason `lib/library/rows.ts` is: the list and the player bar must * agree on what `205` means, and the rule is worth unit-testing on its own. */ /** `205` → `3:25`, `3725` → `1:02:05`, `NaN`/undefined → `0:00`. */ export function formatClock(seconds: number | undefined): string { const s = Number.isFinite(seconds) ? Math.max(0, Math.floor(seconds as number)) : 0; const h = Math.floor(s / 3600); const m = Math.floor((s % 3600) / 60); const sec = String(s % 60).padStart(2, "0"); return h > 0 ? `${h}:${String(m).padStart(2, "0")}:${sec}` : `${m}:${sec}`; } /** * Parse a place in a track, typed the way the clock prints it: `3:25`, `1:02:05`, or a * bare number of seconds. A leading `+`/`-` survives as a signed number, which is how * `seek -20` says *move* rather than *jump to*. * * Refuses rather than rounds: `1:2o` is a typo for `1:20`, and reading it as 0 would put * the playhead at the top of the track when the reader meant the middle of it. The digits * have to be digits, and every part after the first is two places wide. */ export function parseClock(text: string): number | null { const m = /^([+-]?)(\d{1,2})(?::(\d{2})(?::(\d{2}))?)?$/.exec(text.trim()); if (!m) return null; const [, sign, a, b, c] = m as unknown as (string | undefined)[]; const parts = [a, b, c].filter((p): p is string => p !== undefined); const secs = parts.reduce((acc, p) => acc * 60 + Number(p), 0); if (!Number.isFinite(secs) || secs < 0) return null; return sign === "-" ? -secs : secs; }