Archived
1
0
Fork 0

refac(node): remove with child

This commit is contained in:
Dmitriy Pleshevskiy 2022-03-18 22:03:01 +03:00
parent cc9a67d055
commit e41b93ed61
3 changed files with 7 additions and 17 deletions

3
lib/nodes.d.ts vendored
View file

@ -10,8 +10,7 @@ export declare class Frag {
get children(): Nilable<AnyNode[]>; get children(): Nilable<AnyNode[]>;
withText(text: string): this; withText(text: string): this;
addText(text: string): void; addText(text: string): void;
withChildren(nodes: AnyNode[]): this; withChildren(...nodes: AnyNode[]): this;
withChild(node: AnyNode): this;
addChild(node: AnyNode): void; addChild(node: AnyNode): void;
} }
export declare function E( export declare function E(

View file

@ -2,7 +2,7 @@ import { isNil } from "./lang.mjs";
export class TextNode extends String { export class TextNode extends String {
} }
export function F(...children) { export function F(...children) {
return new Frag().withChildren(children); return new Frag().withChildren(...children);
} }
export class Frag { export class Frag {
#children; #children;
@ -19,14 +19,10 @@ export class Frag {
addText(text) { addText(text) {
this.addChild(new TextNode(text)); this.addChild(new TextNode(text));
} }
withChildren(nodes) { withChildren(...nodes) {
nodes.forEach((n) => this.addChild(n)); nodes.forEach((n) => this.addChild(n));
return this; return this;
} }
withChild(node) {
this.addChild(node);
return this;
}
addChild(node) { addChild(node) {
if (isNil(this.#children)) if (isNil(this.#children))
this.#children = []; this.#children = [];
@ -34,7 +30,7 @@ export class Frag {
} }
} }
export function E(tagName, attrs, ...children) { export function E(tagName, attrs, ...children) {
return new Elem(tagName).withAttrs(attrs).withChildren(children); return new Elem(tagName).withAttrs(attrs).withChildren(...children);
} }
export class Elem extends Frag { export class Elem extends Frag {
#tagName; #tagName;

View file

@ -5,7 +5,7 @@ export type AnyNode = TextNode | Elem | Frag | Nil | false;
export class TextNode extends String {} export class TextNode extends String {}
export function F(...children: AnyNode[]): Frag { export function F(...children: AnyNode[]): Frag {
return new Frag().withChildren(children); return new Frag().withChildren(...children);
} }
export class Frag { export class Frag {
@ -28,16 +28,11 @@ export class Frag {
this.addChild(new TextNode(text)); this.addChild(new TextNode(text));
} }
withChildren(nodes: AnyNode[]): this { withChildren(...nodes: AnyNode[]): this {
nodes.forEach((n) => this.addChild(n)); nodes.forEach((n) => this.addChild(n));
return this; return this;
} }
withChild(node: AnyNode): this {
this.addChild(node);
return this;
}
addChild(node: AnyNode): void { addChild(node: AnyNode): void {
if (isNil(this.#children)) this.#children = []; if (isNil(this.#children)) this.#children = [];
this.#children.push(node); this.#children.push(node);
@ -49,7 +44,7 @@ export function E(
attrs: ElemAttrs, attrs: ElemAttrs,
...children: AnyNode[] ...children: AnyNode[]
): Elem { ): Elem {
return new Elem(tagName).withAttrs(attrs).withChildren(children); return new Elem(tagName).withAttrs(attrs).withChildren(...children);
} }
export type ElemAttrs = Record<string, unknown>; export type ElemAttrs = Record<string, unknown>;