From 652a36638c972606192f8b9618a1d07f193686c8 Mon Sep 17 00:00:00 2001 From: Pierre-Etienne Date: Fri, 28 Jun 2024 15:21:22 +0200 Subject: [PATCH] Remove explicit cast to tags when iterating over the lines of the playlist (#156) * Update dependencies to pass npm audit This updates the dependencies and run 'npm audit fix'. * Avoid explicit cast when parsing each line Each line can correspond to a URI or a tag so this avoids an explicit cast to the latter. --- parse.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/parse.ts b/parse.ts index 0b909b8..70a7177 100644 --- a/parse.ts +++ b/parse.ts @@ -403,7 +403,8 @@ function sameKey(key1: Key, key2: Key): boolean { function parseMasterPlaylist(lines: Line[], params: Record): MasterPlaylist { const playlist = new MasterPlaylist(); let variantIsScored = false; - for (const [index, {name, value, attributes}] of (lines as Tag[]).entries()) { + for (const [index, line] of lines.entries()) { + const {name, value, attributes} = mapTo(line); if (name === 'EXT-X-VERSION') { playlist.version = value; } else if (name === 'EXT-X-STREAM-INF') { @@ -490,7 +491,7 @@ function parseSegment(lines: Line[], uri: string, start: number, end: number, me let mapHint = false; let partHint = false; for (let i = start; i <= end; i++) { - const {name, value, attributes} = lines[i] as Tag; + const {name, value, attributes} = mapTo(lines[i]); if (name === 'EXTINF') { if (!Number.isInteger(value.duration) && params.compatibleVersion < 3) { params.compatibleVersion = 3; @@ -658,7 +659,7 @@ function parseMediaPlaylist(lines: Line[], params: Record): MediaPl let currentMap: MediaInitializationSection | null = null; let containsParts = false; for (const [index, line] of lines.entries()) { - const {name, value, attributes, category} = line as Tag; + const {name, value, attributes, category} = mapTo(line); if (category === 'Segment') { if (segmentStart === -1) { segmentStart = index; @@ -1038,4 +1039,8 @@ function parse(text: string): MasterPlaylist | MediaPlaylist { return playlist; } +function mapTo(value: T | string): Partial { + return typeof value === 'string' ? {} : value; +} + export default parse;