Skip to content

Instantly share code, notes, and snippets.

@pdaug
Created May 22, 2024 20:45
Show Gist options
  • Save pdaug/d5d47290f6948a6b8a13cd21c52d7adc to your computer and use it in GitHub Desktop.
Save pdaug/d5d47290f6948a6b8a13cd21c52d7adc to your computer and use it in GitHub Desktop.
type ServiceHtmlElementParameters = {
tag: string;
close?: boolean;
attributes?: Record<string, string>;
children?: string;
};
export const ServiceHtmlElement = function ({ tag, close, attributes, children }: ServiceHtmlElementParameters): string {
const attributesEntries = Object.entries(attributes || new Object());
const attributesConverted = attributesEntries.map(function ([key, value]) {
const attribute = `${key}="${value}"`;
return attribute;
});
const attributeJoinned = attributesConverted.join(" ");
const element = (children || close) ?
`<${tag} ${attributeJoinned}>
${children || ""}
</${tag}>`
:
`<${tag} ${attributeJoinned} />`;
return element;
};
export default ServiceHtmlElement;
@eoguvo
Copy link

eoguvo commented May 22, 2024

wise code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment