Skip to content

Commit

Permalink
explicit return types
Browse files Browse the repository at this point in the history
  • Loading branch information
dbushell committed Feb 15, 2024
1 parent befa0f6 commit 433a6c9
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/
export class Node {
#type: string;
#children: Node[];
#children: Array<Node>;
#parent?: Node;
#attr?: {[key: string]: string};
#attr?: Record<string, string>;
#raw?: string;

constructor(type: string, parent?: Node, raw?: string) {
Expand All @@ -15,23 +15,23 @@ export class Node {
this.#children = [];
}

get type() {
get type(): string {
return this.#type;
}

get raw(): string {
return this.#raw ?? '';
}

get parent() {
get parent(): Node | undefined {
return this.#parent;
}

get children() {
get children(): Array<Node> {
return this.#children;
}

get attributes() {
get attributes(): Record<string, string> {
if (this.#attr) {
return this.#attr;
}
Expand All @@ -47,7 +47,7 @@ export class Node {
return this.#attr;
}

get innerText() {
get innerText(): string {
if (this.children.length) {
let text = '';
for (const child of this.children) {
Expand All @@ -58,15 +58,15 @@ export class Node {
return (this.raw.match(/<!\[CDATA\[(.*?)]]>/s) ?? [, this.raw])[1];
}

addChild(child: Node) {
addChild(child: Node): void {
this.#children.push(child);
}

/**
* Returns true if node and parents match the key hierarchy
* @param keys - XML tag names
*/
is(...keys: string[]) {
is(...keys: Array<string>): boolean {
if (!keys.length) return false;
let parent: Node | undefined;
for (const key of keys.toReversed()) {
Expand All @@ -82,17 +82,17 @@ export class Node {
* Return the first child matching the key
* @param key - XML tag name
*/
first(key: string) {
first(key: string): Node | undefined {
return this.children.find((n) => n.type === key);
}

/**
* Return all children matching the key hierarchy
* @param keys - XML tag names
*/
all(...keys: string[]) {
let nodes: Node[] | undefined = this.children;
let found: Node[] = [];
all(...keys: Array<string>): Array<Node> {
let nodes: Array<Node> | undefined = this.children;
let found: Array<Node> = [];
for (const [i, k] of Object.entries(keys)) {
if (Number.parseInt(i) === keys.length - 1) {
found = nodes.filter((n) => n.type === k);
Expand Down

0 comments on commit 433a6c9

Please sign in to comment.