// From the 140bytes wishlist here: https://github.com/jed/140bytes/wiki/Wishlist // TASK: // Create a function which can create a DOM structure from this: // domBuilder( [/HTML/], [/HEAD/], [/TITLE/], "Wouldn't this be cool?", [], [], [/BODY/], [/DIV/, {id: "container"}], "Hello, world", [], [], [] ); // => // // Wouldn't this be cool? //
Hello, world
// ///////////////////// // My implementation: ///////////////////// function domBuilder(a, b, c, d, i, m) { a = [].slice.call(arguments); for (d = i = 0,m=document; b = a[i++];) if (b.sort) // an array? for ( c in // NOTE: this is not a notable part of the for..in -- it is placed here for space-saving // Here we create a new child or traverse upwards if b[0] isn't defined: d = b[0] ? (c = m.createElement(b[0].source),d ? d.appendChild(c) : c) : d.parentNode || d, b[1] ) // apply attribute: d[c]=b[1][c]; else // a string? -- add text: d.appendChild(m.createTextNode(b)); return d; } /////////////////////// // Minified (254 bytes) /////////////////////// function domBuilder(e,b,c,a,f,d){e=[].slice.call(arguments);a=f=0;for(d=document;b=e[f++];)if(b.sort)for(c in a=b[0]?(c=d.createElement(b[0].source),a?a.appendChild(c):c):a.parentNode||a,b[1])a[c]=b[1][c];else a.appendChild(d.createTextNode(b));return a}