diff --git a/lib/nodes.d.ts b/lib/nodes.d.ts index 0db6890..e2c0b4d 100644 --- a/lib/nodes.d.ts +++ b/lib/nodes.d.ts @@ -10,8 +10,7 @@ export declare class Frag { get children(): Nilable; 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( diff --git a/lib/nodes.mjs b/lib/nodes.mjs index 1492569..deaafa2 100644 --- a/lib/nodes.mjs +++ b/lib/nodes.mjs @@ -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; diff --git a/src/nodes.mts b/src/nodes.mts index 5bd8370..bed0b45 100644 --- a/src/nodes.mts +++ b/src/nodes.mts @@ -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;