Last active
November 4, 2016 10:06
-
-
Save killercup/7c53829d216c276ccf1a886cd07d9330 to your computer and use it in GitHub Desktop.
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
/// PAascal's LIttle TEmplate THingy | |
/// | |
/// a.k.a. that thing I threw together in 10min | |
define("paliteth", [], function () { | |
function notNull(val) { | |
return !!val; | |
} | |
function isArray(vArg) { | |
return Object.prototype.toString.call(vArg) === "[object Array]"; | |
}; | |
/** | |
* Create dom element representation | |
* | |
* @param {string} name | |
* @param {Object} attrs | |
* @param {Object[]} children | |
* @returns {Object} | |
*/ | |
function element(name, attrs, children) { | |
return { | |
name: name, | |
attributes: attrs, | |
children: children | |
}; | |
} | |
/** | |
* Render element attributes | |
* | |
* @param {Object} attrs | |
* @returns {string} | |
*/ | |
function attrsToHtml(attrs) { | |
return Object.keys(attrs).filter(notNull).map(function (key) { | |
var value = attrs[key]; | |
return key + '="' + value + '"'; | |
}).join(" "); | |
} | |
/** | |
* Render element to string | |
* | |
* @param {Object} el | |
* @returns {string} | |
*/ | |
function toHtml(el) { | |
if (typeof el === 'string') { return el; } | |
if (isArray(el)) { return el.map(toHtml).join('\n'); } | |
var attrs = el.attributes ? " " + attrsToHtml(el.attributes) : ""; | |
var children; | |
if (typeof el.children === 'string') { | |
children = el.children; | |
} else { | |
children = (el.children || []).filter(notNull).map(toHtml).join('\n'); | |
} | |
return "<" + el.name + attrs + ">" + | |
children + | |
"</" + el.name + ">"; | |
} | |
return { | |
element: element, | |
toHtml: toHtml, | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment