refactor(spf): parse X-TIMESTAMP-MAP LOCAL via split/reduce

Replace the regex + padEnd parseWebVttTimestamp with split(/[:.]/) + a reduce over
right-aligned weights — handles the [HH:]MM:SS.ttt hours-optional forms in one
expression. Behavior-equivalent for spec-valid LOCAL (X-TIMESTAMP-MAP is always
[HH:]MM:SS.ttt); intentionally narrows off the padEnd/undefined tolerance for
non-spec input. −51 B gzipped in the hls bundle; 6/6 tests pass.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Christian Pillsbury
2026-07-15 16:22:11 -07:00
co-authored by Claude Opus 4.8
parent d68698fdb7
commit c39132a935
@@ -46,12 +46,9 @@ function parseTimestampMapBody(body: string): TimestampMap | undefined {
) as TimestampMap;
}
function parseWebVttTimestamp(value: string): number | undefined {
const match = value.match(/^(?:(\d+):)?(\d{1,2}):(\d{2})\.(\d{1,3})$/);
if (!match) return undefined;
const hours = match[1] ? Number(match[1]) : 0;
const minutes = Number(match[2]);
const seconds = Number(match[3]);
const millis = Number((match[4] ?? '').padEnd(3, '0'));
return hours * 3600 + minutes * 60 + seconds + millis / 1000;
function parseWebVttTimestamp(value: string): number {
// Computes the math cleanly whether hours are provided or omitted (right-aligned weights).
return value
.split(/[:.]/)
.reduce((acc, val, i, parts) => acc + +val * [3600, 60, 1, 0.001][i + 4 - parts.length]!, 0);
}