diff --git a/lib/nodes.d.ts b/lib/nodes.d.ts index f054efd..0db6890 100644 --- a/lib/nodes.d.ts +++ b/lib/nodes.d.ts @@ -1,29 +1,32 @@ import { Nil, Nilable } from "./lang.js"; -export declare type AnyNode = AnySyncNode | AnyAsyncNode; -export declare type AnyAsyncNode = Promise; -export declare type AnySyncNode = TextNode | Elem | Frag | Nil | false; -export declare class TextNode extends String { -} +export declare type AnyNode = TextNode | Elem | Frag | Nil | false; + +export declare class TextNode extends String {} + export declare function F(...children: AnyNode[]): Frag; export declare class Frag { - #private; - constructor(); - get children(): Nilable; - withText(text: string): this; - addText(text: string): void; - withChildren(nodes: AnyNode[]): this; - withChild(node: AnyNode): this; - addChild(node: AnyNode): void; + #private; + constructor(); + get children(): Nilable; + withText(text: string): this; + addText(text: string): void; + withChildren(nodes: AnyNode[]): this; + withChild(node: AnyNode): this; + addChild(node: AnyNode): void; } -export declare function E(tagName: string, attrs: ElemAttrs, ...children: AnyNode[]): Elem; +export declare function E( + tagName: string, + attrs: ElemAttrs, + ...children: AnyNode[] +): Elem; export declare type ElemAttrs = Record; export declare class Elem extends Frag { - #private; - constructor(tagName: string); - get tagName(): string; - get attrs(): Record; - withAttrs(attrs: Record): Elem; - withAttr(name: string, value: unknown): Elem; - addAttr(name: string, value: unknown): void; - addChild(node: AnySyncNode): void; + #private; + constructor(tagName: string); + get tagName(): string; + get attrs(): Record; + withAttrs(attrs: Record): Elem; + withAttr(name: string, value: unknown): Elem; + addAttr(name: string, value: unknown): void; + addChild(node: AnyNode): void; } diff --git a/lib/str.d.ts b/lib/str.d.ts index e602328..6e569d0 100644 --- a/lib/str.d.ts +++ b/lib/str.d.ts @@ -1,5 +1,5 @@ import { Renderer } from "./types.js"; import { Elem } from "./nodes.js"; export declare class StrRenderer implements Renderer { - render(node: Elem | Promise): Promise; + render(node: Elem): string; } diff --git a/lib/str.mjs b/lib/str.mjs index 11b2dda..451da25 100644 --- a/lib/str.mjs +++ b/lib/str.mjs @@ -1,25 +1,24 @@ import { isBool, isNil } from "./lang.mjs"; import { Elem, TextNode } from "./nodes.mjs"; export class StrRenderer { - async render(node) { - return encodeNode(await node); + render(node) { + return encodeNode(node); } } -async function encodeAnyNode(node) { - const syncNode = await node; - return !syncNode +function encodeAnyNode(node) { + return !node ? null - : syncNode instanceof TextNode - ? encodeTextNode(syncNode) - : encodeNode(syncNode); + : node instanceof TextNode + ? encodeTextNode(node) + : encodeNode(node); } function encodeTextNode(node) { return String(node); } -async function encodeNode(node) { +function encodeNode(node) { const encodedChildren = isNil(node.children) ? undefined - : await Promise.all(node.children.map(encodeAnyNode)).then((children) => children.filter((c) => Boolean(c))); + : node.children.map(encodeAnyNode).filter((c) => Boolean(c)); return node instanceof Elem ? encodeHtmlElement(node.tagName, node.attrs, encodedChildren) : encodeHtmlFragment(encodedChildren); diff --git a/lib/types.d.ts b/lib/types.d.ts index 3db235b..e25b42b 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -1,4 +1,4 @@ import { Elem } from "./nodes.js"; export interface Renderer { - render(node: Elem | Promise): Promise; + render(node: Elem): T; } diff --git a/src/nodes.mts b/src/nodes.mts index 6ed2927..5bd8370 100644 --- a/src/nodes.mts +++ b/src/nodes.mts @@ -1,8 +1,6 @@ import { isNil, Nil, Nilable } from "./lang.mjs"; -export type AnyNode = AnySyncNode | AnyAsyncNode; -export type AnyAsyncNode = Promise; -export type AnySyncNode = TextNode | Elem | Frag | Nil | false; +export type AnyNode = TextNode | Elem | Frag | Nil | false; export class TextNode extends String {} @@ -90,7 +88,7 @@ export class Elem extends Frag { this.#attrs[name] = value; } - addChild(node: AnySyncNode): void { + addChild(node: AnyNode): void { if (this.#isSelfClosed) throw new Error("You cannot add child to self closed element"); super.addChild(node); diff --git a/src/str.mts b/src/str.mts index 448f09c..fd69ce8 100644 --- a/src/str.mts +++ b/src/str.mts @@ -3,30 +3,27 @@ import { isBool, isNil, Nullable } from "./lang.mjs"; import { AnyNode, Elem, Frag, TextNode } from "./nodes.mjs"; export class StrRenderer implements Renderer { - async render(node: Elem | Promise): Promise { - return encodeNode(await node); + render(node: Elem): string { + return encodeNode(node); } } -async function encodeAnyNode(node: AnyNode): Promise> { - const syncNode = await node; - return !syncNode +function encodeAnyNode(node: AnyNode): Nullable { + return !node ? null - : syncNode instanceof TextNode - ? encodeTextNode(syncNode) - : encodeNode(syncNode); + : node instanceof TextNode + ? encodeTextNode(node) + : encodeNode(node); } function encodeTextNode(node: TextNode): string { return String(node); } -async function encodeNode(node: Elem | Frag): Promise { +function encodeNode(node: Elem | Frag): string { const encodedChildren = isNil(node.children) ? undefined - : await Promise.all(node.children.map(encodeAnyNode)).then((children) => - children.filter((c): c is string => Boolean(c)) - ); + : node.children.map(encodeAnyNode).filter((c): c is string => Boolean(c)); return node instanceof Elem ? encodeHtmlElement(node.tagName, node.attrs, encodedChildren) : encodeHtmlFragment(encodedChildren); diff --git a/src/types.mts b/src/types.mts index 3e04a75..d93d7ad 100644 --- a/src/types.mts +++ b/src/types.mts @@ -1,5 +1,5 @@ import { Elem } from "./nodes.mjs"; export interface Renderer { - render(node: Elem | Promise): Promise; + render(node: Elem): T; }