Skip to content

Commit

Permalink
toArray: only call Array.prototype.slice on actual arrays.
Browse files Browse the repository at this point in the history
Specifically, this fixes _.toArray on NodeList objects on IE8, which worked in
Underscore 1.3.3 but throws "JScript object expected"  in 1.4.0.
  • Loading branch information
glasser committed Nov 20, 2012
1 parent 0a2adcb commit 26a3055
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions test/collections.js
Expand Up @@ -418,6 +418,9 @@ $(document).ready(function() {

var numbers = _.toArray({one : 1, two : 2, three : 3});
equal(numbers.join(', '), '1, 2, 3', 'object flattened into array');

// _.toArray on a NodeList should not throw.
ok(_.isArray(_.toArray(document.childNodes)));
});

test('size', function() {
Expand Down
3 changes: 2 additions & 1 deletion underscore.js
Expand Up @@ -358,7 +358,8 @@
// Safely convert anything iterable into a real, live array.
_.toArray = function(obj) {
if (!obj) return [];
if (obj.length === +obj.length) return slice.call(obj);
if (_.isArray(obj)) return slice.call(obj);
if (obj.length === +obj.length) return _.map(obj, _.identity);
return _.values(obj);
};

Expand Down

0 comments on commit 26a3055

Please sign in to comment.