From c39132a93578cd8083512e3188da8e3f6589705f Mon Sep 17 00:00:00 2001 From: Christian Pillsbury Date: Wed, 15 Jul 2026 16:22:11 -0700 Subject: [PATCH] refactor(spf): parse X-TIMESTAMP-MAP LOCAL via split/reduce MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .../spf/src/media/text/parse-vtt-timestamp-map.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/spf/src/media/text/parse-vtt-timestamp-map.ts b/packages/spf/src/media/text/parse-vtt-timestamp-map.ts index 91ad015e..122a6761 100644 --- a/packages/spf/src/media/text/parse-vtt-timestamp-map.ts +++ b/packages/spf/src/media/text/parse-vtt-timestamp-map.ts @@ -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); }