Skip to content

Commit b53931b

Browse files
move parseItems function, add logic for selecting which content to use
1 parent ccf8200 commit b53931b

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

src/parser/classes/Tab.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,28 @@ export default class Tab extends YTNode {
1212
selected: boolean;
1313
endpoint: NavigationEndpoint;
1414
content: SectionList | MusicQueue | RichGrid | null;
15-
contents: (SectionList | MusicQueue | RichGrid)[] | null;
1615

1716
constructor(data: RawNode) {
1817
super();
1918
this.title = data.title || 'N/A';
2019
this.selected = !!data.selected;
2120
this.endpoint = new NavigationEndpoint(data.endpoint);
22-
this.content = Parser.parseItem(data.content, [ SectionList, MusicQueue, RichGrid ]);
23-
this.contents = Parser.parseItems(data.content, [ SectionList, MusicQueue, RichGrid ]);
21+
const contents = Parser.parseItems(data.content, [ SectionList, MusicQueue, RichGrid ]);
22+
this.content = null;
23+
if (contents !== null && contents.length > 0) {
24+
for (const item of contents) {
25+
if (item.is(SectionList) && item.contents.length > 0) {
26+
this.content = item;
27+
} else if (item.is(RichGrid) && item.contents.length > 0) {
28+
this.content = item;
29+
} else if (item.is(MusicQueue)) {
30+
this.content = item;
31+
}
32+
}
33+
34+
if (this.content === null) {
35+
this.content = contents[0];
36+
}
37+
}
2438
}
2539
}

src/parser/parser.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -521,29 +521,6 @@ export function parseResponse<T extends IParsedResponse = IParsedResponse>(data:
521521
return parsed_data;
522522
}
523523

524-
/**
525-
* Parses multiple items
526-
* @param data - The data to parse.
527-
* @param validTypes - YTNode types that are allowed to be parsed.
528-
*/
529-
export function parseItems<T extends YTNode, K extends YTNodeConstructor<T>[]>(data: RawNode | undefined, validTypes: K): InstanceType<K[number]>[] | null;
530-
export function parseItems<T extends YTNode>(data: RawNode | undefined, validTypes: YTNodeConstructor<T>): T[] | null;
531-
export function parseItems(data?: RawNode): YTNode[];
532-
export function parseItems(data?: RawNode, validTypes?: YTNodeConstructor | YTNodeConstructor[]) {
533-
if (!data) return null;
534-
const keys = Object.keys(data);
535-
const results: YTNode[] = [];
536-
for (const key of keys) {
537-
const temp_data = { [key]: data[key] };
538-
539-
const result = parseItem(temp_data, validTypes as YTNodeConstructor);
540-
if (result) {
541-
results.push(result);
542-
}
543-
}
544-
return results;
545-
}
546-
547524
/**
548525
* Parses an item.
549526
* @param data - The data to parse.
@@ -610,6 +587,29 @@ export function parseItem(data?: RawNode, validTypes?: YTNodeConstructor | YTNod
610587
return null;
611588
}
612589

590+
/**
591+
* Parses multiple items in an object as an array of items.
592+
* @param data - The data to parse.
593+
* @param validTypes - YTNode types that are allowed to be parsed.
594+
*/
595+
export function parseItems<T extends YTNode, K extends YTNodeConstructor<T>[]>(data: RawNode | undefined, validTypes: K): InstanceType<K[number]>[] | null;
596+
export function parseItems<T extends YTNode>(data: RawNode | undefined, validTypes: YTNodeConstructor<T>): T[] | null;
597+
export function parseItems(data?: RawNode): YTNode[];
598+
export function parseItems(data?: RawNode, validTypes?: YTNodeConstructor | YTNodeConstructor[]) {
599+
if (!data) return null;
600+
const keys = Object.keys(data);
601+
const results: YTNode[] = [];
602+
for (const key of keys) {
603+
const temp_data = { [key]: data[key] };
604+
605+
const result = parseItem(temp_data, validTypes as YTNodeConstructor);
606+
if (result) {
607+
results.push(result);
608+
}
609+
}
610+
return results;
611+
}
612+
613613
/**
614614
* Parses an array of items.
615615
* @param data - The data to parse.

0 commit comments

Comments
 (0)