Skip to content

Instantly share code, notes, and snippets.

@madbence
Last active December 20, 2015 04:59
Show Gist options
  • Select an option

  • Save madbence/6074500 to your computer and use it in GitHub Desktop.

Select an option

Save madbence/6074500 to your computer and use it in GitHub Desktop.
Some crazy JavaScript stuff
function indent(n) { return Array(n+1).join(' '); }
function elem(name, wrap) {
var before = wrap === false ?
function() {} : function(n) {
console.log(indent(n) + '<' + name + '>');
};
var after = wrap === false ?
function() {} : function(n) {
console.log(indent(n) + '</' + name + '>');
};
var _elem = function(e) {
var childs = [];
var __elem = function(e) {
e && childs.push(e);
return __elem;
};
__elem.print = function(n) {
before(n);
childs.forEach(function(c) { typeof c === 'function' ? c.print(n+1) : console.log(indent(n)+c); });
after(n);
};
return e ? __elem(e) : __elem;
};
_elem.print = function(n) {
console.log(indent(n) + '<' + name + ' />');
};
return _elem;
}
var html = elem('html');
var head = elem('head');
var body = elem('body');
var div = elem('div');
var text = elem(null, false);
html
(head)
(body
(div
(text
('Hello world!')))
(div))
.print(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment