Skip to content

Commit

Permalink
Avoid explicit cast when parsing each line
Browse files Browse the repository at this point in the history
Each line can correspond to a URI or a tag so this avoids an explicit
cast to the latter.
  • Loading branch information
poirotp-bpk committed Jun 28, 2024
1 parent d6ac857 commit ab6c173
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>(line: T | string): Partial<T> {
return typeof line === 'string' ? {} : line;
}

export default parse;

0 comments on commit ab6c173

Please sign in to comment.