UNPKG

@studiometa/js-toolkit

Version:

A set of useful little bits of JavaScript to boost your project! 🚀

28 lines (27 loc) • 911 B
import { isArray, isObject, isString } from "../is.js"; import { dashCase } from "../string/changeCase.js"; function createElement(tag, attributes = {}, children = null) { const el = document.createElement(tag ?? "div"); if (children === null && (isArray(attributes) || isString(attributes) || attributes instanceof Node)) { children = attributes; attributes = {}; } for (const [name, value] of Object.entries(attributes)) { if (isString(value)) { el.setAttribute(dashCase(name), value); } else if (name === "data" && isObject(value)) { for (const [dataName, dataValue] of Object.entries(value)) { el.setAttribute(`data-${dashCase(dataName)}`, dataValue); } } } if (children) { children = isArray(children) ? children : [children]; el.append(...children); } return el; } export { createElement }; //# sourceMappingURL=createElement.js.map