Last active
December 20, 2015 04:59
-
-
Save madbence/6074500 to your computer and use it in GitHub Desktop.
Some crazy JavaScript stuff
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 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