Skip to content

Instantly share code, notes, and snippets.

@bergmark
Created October 25, 2010 06:33
Show Gist options
  • Select an option

  • Save bergmark/644504 to your computer and use it in GitHub Desktop.

Select an option

Save bergmark/644504 to your computer and use it in GitHub Desktop.
/**
* Collection.coerce(collection, iterate) -> Array
* - collection (collection)
* - iterate (boolean): Whether the collection is readOnly.
*
* Coerces a collection into an Array.
* If the object is readOnly, as with a NodeList, pass the iterate flag
* to iterate through the collection and create an array of the contents.
**/
coerce : function (collection, iterate) {
if (!iterate) {
return Array.prototype.slice.call(collection);
} else {
var a = [];
for (var i = 0; i < collection.length; i++) {
a.push(collection[i]);
}
return a;
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment