Skip to content

Instantly share code, notes, and snippets.

@suissa
Created December 2, 2016 01:57
Show Gist options
  • Save suissa/922223550bbc05912ae4806b2dc260a4 to your computer and use it in GitHub Desktop.
Save suissa/922223550bbc05912ae4806b2dc260a4 to your computer and use it in GitHub Desktop.
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);
@suissa
Copy link
Author

suissa commented Dec 2, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment