Created
February 8, 2010 03:43
-
-
Save gifnksm/297855 to your computer and use it in GitHub Desktop.
DOM要素の配列を結合する
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
// DOM要素を結合する。引数はScalaのmkString風 | |
Array.prototype.joinDOM = function() { | |
var [sep, head, tail] = [null, null, null], | |
arg = Array.map(arguments, convertToDOM); | |
switch(arg.length) { | |
case 0: break; | |
case 1: [sep] = arg; break; | |
case 3: [head, sep, tail] = arg; break; | |
default: throw new Error('invalid arguments'); | |
} | |
var df = document.createDocumentFragment(); | |
function append(e, clone) { | |
if (e !== null) df.appendChild(clone ? e.cloneNode(true) : e); | |
} | |
append(head); | |
for (let [i, elem] in Iterator(this)) { | |
if (i > 0) append(sep, true); | |
append(convertToDOM(elem)); | |
} | |
append(tail); | |
return df; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment