Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request jashkenas#866 from glasser/toarray-nodelist
Ensure `_.toArray` only call `Array#slice` on actual arrays.
  • Loading branch information
jdalton committed Dec 1, 2012
2 parents a3e0175 + 26a3055 commit 97037bc
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 97037bc

Please sign in to comment.