Created
December 2, 2016 01:57
-
-
Save suissa/922223550bbc05912ae4806b2dc260a4 to your computer and use it in GitHub Desktop.
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
const tag = (name, children)=> ({ | |
name, | |
children: children.map ? children: [children] | |
}); | |
const div = tag.bind(null, 'div'); | |
const span = tag.bind(null, 'span'); | |
const p = tag.bind(null, 'p'); | |
const b = tag.bind(null, 'b'); | |
const a = tag.bind(null, 'a'); | |
const appendChildren = (el, children) => ( | |
children.map(render.bind(null, el)), | |
el | |
) | |
const createEl = name => | |
document.createElement(name.toUpperCase()) | |
const renderEl = tree => | |
appendChildren( | |
createEl(tree.name.toUpperCase()), | |
tree.children | |
) | |
const renderText = text => | |
document.createTextNode(text) | |
const isElement = tree => | |
tree.name && tree.children | |
// <div id="stage" /> | |
const renderTree = tree => | |
isElement(tree) ? renderEl(tree) : renderText(tree) | |
const render = (root, tree) => | |
root.appendChild(renderTree(tree)) | |
const dom = div([ | |
p('olá mundo'), | |
span([ | |
'teste: ', | |
b('olá mundo') | |
]), | |
a('link') | |
]); | |
render(window.stage, dom); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Esse code eh do mestre @halan https://jsfiddle.net/halan/yn5z77ay/