Last active
July 5, 2018 14:47
-
-
Save callemo/73784f3c6b6796ee6138c5e20d664393 to your computer and use it in GitHub Desktop.
htag.js – A Bare bones interpretation of the Hyperscript syntax.
This file contains 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
/* | |
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