refactor: clean up parser and tests (#387)

* tests: improve coverage

* refactor: clean up nodes

* chore: lint

* feat(parser): ignore `BrandVideoShelf`

Seems to be used for ads.

* feat(parser): ignore `BrandVideoSingleton` too
This commit is contained in:
LuanRT
2023-04-23 06:37:33 -03:00
committed by GitHub
parent f66f0bd656
commit 257bd475a0
358 changed files with 2823 additions and 3126 deletions

View File

@@ -1,7 +1,7 @@
import TextRun from './TextRun.js';
import EmojiRun from './EmojiRun.js';
import NavigationEndpoint from '../NavigationEndpoint.js';
import type { RawNode } from '../../index.js';
import NavigationEndpoint from '../NavigationEndpoint.js';
import EmojiRun from './EmojiRun.js';
import TextRun from './TextRun.js';
export interface Run {
text: string;
@@ -20,12 +20,12 @@ export function escape(text: string) {
export default class Text {
text?: string;
runs;
runs?: (EmojiRun | TextRun)[];
endpoint?: NavigationEndpoint;
constructor(data: RawNode) {
if (data?.hasOwnProperty('runs') && Array.isArray(data.runs)) {
this.runs = (data.runs as any[]).map((run: any) => run.emoji ?
if (typeof data === 'object' && data !== null && Reflect.has(data, 'runs') && Array.isArray(data.runs)) {
this.runs = data.runs.map((run: RawNode) => run.emoji ?
new EmojiRun(run) :
new TextRun(run)
);
@@ -46,15 +46,27 @@ export default class Text {
}
}
toHTML() {
/**
* Converts the text to HTML.
* @returns The HTML.
*/
toHTML(): string | undefined {
return this.runs ? this.runs.map((run) => run.toHTML()).join('') : this.text;
}
isEmpty() {
/**
* Checks if the text is empty.
* @returns Whether the text is empty.
*/
isEmpty(): boolean {
return this.text === undefined;
}
toString() {
/**
* Converts the text to a string.
* @returns The text.
*/
toString(): string {
return this.text || 'N/A';
}
}