-
-
Save kthwaite/69ef53c6911e413a5c7d59a2bed8dac5 to your computer and use it in GitHub Desktop.
function h()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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