Created
April 24, 2018 00:54
-
-
Save tmlbl/5a78933f09999a298ecfc2539fb5b33a 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
function isNode(x) { | |
return typeof x.blur == 'function'; | |
} | |
function writeStyles(obj) { | |
var strs = []; | |
Object.keys(obj).forEach(function (k) { | |
strs.push(k + ':' + obj[k]); | |
}); | |
return strs.join(';'); | |
} | |
function renderItem(element, arg) { | |
if (Array.isArray(arg)) { | |
return arg.forEach(renderItem.bind(null, element)); | |
} | |
switch (typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) { | |
case 'string': | |
element.innerHTML = arg; | |
break; | |
case 'number': | |
element.innerHTML = arg.toString(); | |
break; | |
case 'object': | |
if (isNode(arg)) { | |
// Treat as node | |
element.appendChild(arg); | |
} else { | |
// Treat as attributes object | |
Object.keys(arg).forEach(function (key) { | |
if (key == 'style') { | |
element.setAttribute(key, writeStyles(arg[key])); | |
} else { | |
element.setAttribute(key, arg[key]); | |
} | |
}); | |
} | |
break; | |
} | |
} | |
function el() { | |
var name = arguments[0]; | |
var element = document.createElement(name); | |
for (var i = 1; i < arguments.length; i++) { | |
var arg = arguments[i]; | |
renderItem(element, arg); | |
} | |
return element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment