Skip to content

Commit 652a366

Browse files
authored
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.
1 parent 2bb57e9 commit 652a366

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

parse.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@ function sameKey(key1: Key, key2: Key): boolean {
403403
function parseMasterPlaylist(lines: Line[], params: Record<string, any>): MasterPlaylist {
404404
const playlist = new MasterPlaylist();
405405
let variantIsScored = false;
406-
for (const [index, {name, value, attributes}] of (lines as Tag[]).entries()) {
406+
for (const [index, line] of lines.entries()) {
407+
const {name, value, attributes} = mapTo<Tag>(line);
407408
if (name === 'EXT-X-VERSION') {
408409
playlist.version = value;
409410
} else if (name === 'EXT-X-STREAM-INF') {
@@ -490,7 +491,7 @@ function parseSegment(lines: Line[], uri: string, start: number, end: number, me
490491
let mapHint = false;
491492
let partHint = false;
492493
for (let i = start; i <= end; i++) {
493-
const {name, value, attributes} = lines[i] as Tag;
494+
const {name, value, attributes} = mapTo<Tag>(lines[i]);
494495
if (name === 'EXTINF') {
495496
if (!Number.isInteger(value.duration) && params.compatibleVersion < 3) {
496497
params.compatibleVersion = 3;
@@ -658,7 +659,7 @@ function parseMediaPlaylist(lines: Line[], params: Record<string, any>): MediaPl
658659
let currentMap: MediaInitializationSection | null = null;
659660
let containsParts = false;
660661
for (const [index, line] of lines.entries()) {
661-
const {name, value, attributes, category} = line as Tag;
662+
const {name, value, attributes, category} = mapTo<Tag>(line);
662663
if (category === 'Segment') {
663664
if (segmentStart === -1) {
664665
segmentStart = index;
@@ -1038,4 +1039,8 @@ function parse(text: string): MasterPlaylist | MediaPlaylist {
10381039
return playlist;
10391040
}
10401041

1042+
function mapTo<T extends object>(value: T | string): Partial<T> {
1043+
return typeof value === 'string' ? {} : value;
1044+
}
1045+
10411046
export default parse;

0 commit comments

Comments
 (0)