Skip to content

Instantly share code, notes, and snippets.

@kthwaite
Created September 21, 2021 13:19
Show Gist options
  • Save kthwaite/69ef53c6911e413a5c7d59a2bed8dac5 to your computer and use it in GitHub Desktop.
Save kthwaite/69ef53c6911e413a5c7d59a2bed8dac5 to your computer and use it in GitHub Desktop.
function h()
function h() {
const sel = arguments[0];
const tag = sel.match(/^[^\.#]+/)[0].trim();
// WONTFIX: i should handle tag === '', but instead i'll just avoid
// doing that, ha ha
const e = document.createElement(tag);
let classes = sel.match(/\.[^\.#]+/g);
if (classes != null) {
classes = classes.map((c) => c.slice(1));
e.classList.add(...classes);
}
const id = sel.match(/\#[^\.#]+/g);
if (id !== null) {
e.id = id[0];
}
const options = arguments[1];
if (options != null) {
if (typeof options === 'object' && Object.getPrototypeOf(options) === Object.prototype) {
Object.entries(options).forEach((pair) => {
const [key, value] = pair;
e.setAttribute(key, value);
});
} else {
e.append(options);
}
}
const children = Array.from(arguments).slice(2);
e.append(...children);
return e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment