Skip to content

Instantly share code, notes, and snippets.

@gifnksm
Created February 8, 2010 03:43
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 gifnksm/297855 to your computer and use it in GitHub Desktop.
Save gifnksm/297855 to your computer and use it in GitHub Desktop.
DOM要素の配列を結合する
// 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