/* ============================================================================ * Copyright (c) Cloud Annotations * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * ========================================================================== */ export type Children = string | undefined | (string | undefined)[]; export type Props = Record & { children?: Children }; export function create(tag: string, props: Props): string { const { children, ...rest } = props; let propString = ""; for (const [key, value] of Object.entries(rest)) { propString += ` ${key}={${JSON.stringify(value)}}`; } return `<${tag}${propString}>${render(children)}`; } export function guard( value: T | undefined, cb: (value: T) => Children ): string { if (value) { const children = cb(value); return render(children); } return ""; } export function render(children: Children): string { if (Array.isArray(children)) { return children.filter((c) => c !== undefined).join(""); } return children ?? ""; }