Last active
April 3, 2022 18:41
-
-
Save fabiangarga/68df2dc50c170c9e32a74cced1bfd6ad to your computer and use it in GitHub Desktop.
Render Elements
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 el (parent) { | |
return function (...args) { | |
let out = ''; | |
if(args.length > 1) { | |
let outChild = ''; | |
args.forEach(arg => { | |
outChild += arg; | |
}); | |
out += renderElChild(parent, outChild) | |
} | |
else { | |
return renderElChild(parent, args[0]); | |
} | |
return out; | |
} | |
} | |
function renderElChild (parent, child) { | |
return `<${parent}>${child}</${parent}>` | |
} | |
function main() { | |
const div = el('div'); | |
const p = el('p'); | |
const html = div( | |
p(' Hello '), | |
p(' Random '), | |
div( | |
p(' Hi from p '), | |
), | |
p(' Last message ') | |
); | |
console.log(html); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment