Skip to content

Commit

Permalink
Remove explicit cast to tags when iterating over the lines of the pla…
Browse files Browse the repository at this point in the history
…ylist (#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.
  • Loading branch information
pepoirot authored Jun 28, 2024
1 parent 2bb57e9 commit 652a366
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ function sameKey(key1: Key, key2: Key): boolean {
function parseMasterPlaylist(lines: Line[], params: Record<string, any>): 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<Tag>(line);
if (name === 'EXT-X-VERSION') {
playlist.version = value;
} else if (name === 'EXT-X-STREAM-INF') {
Expand Down Expand Up @@ -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<Tag>(lines[i]);
if (name === 'EXTINF') {
if (!Number.isInteger(value.duration) && params.compatibleVersion < 3) {
params.compatibleVersion = 3;
Expand Down Expand Up @@ -658,7 +659,7 @@ function parseMediaPlaylist(lines: Line[], params: Record<string, any>): 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<Tag>(line);
if (category === 'Segment') {
if (segmentStart === -1) {
segmentStart = index;
Expand Down Expand Up @@ -1038,4 +1039,8 @@ function parse(text: string): MasterPlaylist | MediaPlaylist {
return playlist;
}

function mapTo<T extends object>(value: T | string): Partial<T> {
return typeof value === 'string' ? {} : value;
}

export default parse;

0 comments on commit 652a366

Please sign in to comment.