Skip to content

Instantly share code, notes, and snippets.

@callemo
Last active July 5, 2018 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callemo/73784f3c6b6796ee6138c5e20d664393 to your computer and use it in GitHub Desktop.
Save callemo/73784f3c6b6796ee6138c5e20d664393 to your computer and use it in GitHub Desktop.
htag.js – A Bare bones interpretation of the Hyperscript syntax.
/*
htag.js
A Bare bones interpretation of the Hyperscript syntax.
*/
var h = function h(tag, attributes, ...children) {
const node = document.createElement(tag);
attributes = attributes || {};
Object.keys(attributes).forEach(attr => {
if (attr === "style" || attr === "dataset") {
const value = attributes[attr];
if (Object.prototype.toString.call(value) === "[object Object]") {
Object.keys(value).forEach(name => {
node[attr][name] = value[name];
});
return;
}
}
node[attr] = attributes[attr];
});
if (Array.isArray(children)) {
children.forEach(child => {
if (typeof child === "string" || typeof child === "number") {
child = document.createTextNode(child);
}
node.appendChild(child);
});
}
return node;
};
if (typeof module === "object" && module.exports) {
module.exports = h;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment