refac(node): remove with child
This commit is contained in:
parent
cc9a67d055
commit
e41b93ed61
3 changed files with 7 additions and 17 deletions
3
lib/nodes.d.ts
vendored
3
lib/nodes.d.ts
vendored
|
@ -10,8 +10,7 @@ export declare class Frag {
|
|||
get children(): Nilable<AnyNode[]>;
|
||||
withText(text: string): this;
|
||||
addText(text: string): void;
|
||||
withChildren(nodes: AnyNode[]): this;
|
||||
withChild(node: AnyNode): this;
|
||||
withChildren(...nodes: AnyNode[]): this;
|
||||
addChild(node: AnyNode): void;
|
||||
}
|
||||
export declare function E(
|
||||
|
|
|
@ -2,7 +2,7 @@ import { isNil } from "./lang.mjs";
|
|||
export class TextNode extends String {
|
||||
}
|
||||
export function F(...children) {
|
||||
return new Frag().withChildren(children);
|
||||
return new Frag().withChildren(...children);
|
||||
}
|
||||
export class Frag {
|
||||
#children;
|
||||
|
@ -19,14 +19,10 @@ export class Frag {
|
|||
addText(text) {
|
||||
this.addChild(new TextNode(text));
|
||||
}
|
||||
withChildren(nodes) {
|
||||
withChildren(...nodes) {
|
||||
nodes.forEach((n) => this.addChild(n));
|
||||
return this;
|
||||
}
|
||||
withChild(node) {
|
||||
this.addChild(node);
|
||||
return this;
|
||||
}
|
||||
addChild(node) {
|
||||
if (isNil(this.#children))
|
||||
this.#children = [];
|
||||
|
@ -34,7 +30,7 @@ export class Frag {
|
|||
}
|
||||
}
|
||||
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 {
|
||||
#tagName;
|
||||
|
|
|
@ -5,7 +5,7 @@ export type AnyNode = TextNode | Elem | Frag | Nil | false;
|
|||
export class TextNode extends String {}
|
||||
|
||||
export function F(...children: AnyNode[]): Frag {
|
||||
return new Frag().withChildren(children);
|
||||
return new Frag().withChildren(...children);
|
||||
}
|
||||
|
||||
export class Frag {
|
||||
|
@ -28,16 +28,11 @@ export class Frag {
|
|||
this.addChild(new TextNode(text));
|
||||
}
|
||||
|
||||
withChildren(nodes: AnyNode[]): this {
|
||||
withChildren(...nodes: AnyNode[]): this {
|
||||
nodes.forEach((n) => this.addChild(n));
|
||||
return this;
|
||||
}
|
||||
|
||||
withChild(node: AnyNode): this {
|
||||
this.addChild(node);
|
||||
return this;
|
||||
}
|
||||
|
||||
addChild(node: AnyNode): void {
|
||||
if (isNil(this.#children)) this.#children = [];
|
||||
this.#children.push(node);
|
||||
|
@ -49,7 +44,7 @@ export function E(
|
|||
attrs: ElemAttrs,
|
||||
...children: AnyNode[]
|
||||
): Elem {
|
||||
return new Elem(tagName).withAttrs(attrs).withChildren(children);
|
||||
return new Elem(tagName).withAttrs(attrs).withChildren(...children);
|
||||
}
|
||||
|
||||
export type ElemAttrs = Record<string, unknown>;
|
||||
|
|
Reference in a new issue