Skip to content

Instantly share code, notes, and snippets.

@swilliams
Created May 6, 2013 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swilliams/5528743 to your computer and use it in GitHub Desktop.
Save swilliams/5528743 to your computer and use it in GitHub Desktop.
Currying FTW
var tag = 'div',
attrs = { className: 'foo', id: 'bar' },
slice = Array.prototype.slice;
isContent = function(c) {
var un = "undefined";
return c !== null && typeof c !== un &&
(typeof c.nodeType !== un ||
typeof c === "string");
}
el = function(tagName, attributes, content) {
tagName = tagName || "div";
if (isContent(attributes)) {
content = attributes;
attributes = {};
}
attributes = attributes || {}
e = document.createElement(tagName);
for (var k in attributes) {
e[k] = attributes[k];
}
if (content) {
if (typeof content === "string") {
e.appendChild(document.createTextNode(content));
} else {
e.appendChild(content);
}
}
return e;
}
partial = function(fn) {
var args = slice.call(arguments, 1);
return function() {
return fn.apply(this, args.concat(slice.call(arguments)));
}
}
DOM = {};
allTags = ["div", "p", "ul", "li"];
allTags.forEach(function(t) {
DOM[t] = partial(el, t);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment