Skip to content

Commit

Permalink
make toArray return a clone in case of an array
Browse files Browse the repository at this point in the history
In combination with backbone this makes coll.toArray() return an array
instead of just returning coll.models, enabling
coll.remove(coll.toArray()) whithout failing half way through.
  • Loading branch information
chaoflow committed Jun 21, 2011
1 parent 96dbdaf commit 0202f9f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ $(document).ready(function() {
test('collections: toArray', function() {
ok(!_.isArray(arguments), 'arguments object is not an array');
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
var a = [1,2,3];
ok(_.toArray(a) !== a, 'array is cloned');
equals(_.toArray(a).join(', '), '1, 2, 3', 'cloned array contains same elements');

var numbers = _.toArray({one : 1, two : 2, three : 3});
equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');
Expand Down
2 changes: 1 addition & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
_.toArray = function(iterable) {
if (!iterable) return [];
if (iterable.toArray) return iterable.toArray();
if (_.isArray(iterable)) return iterable;
if (_.isArray(iterable)) return slice.call(iterable);
if (_.isArguments(iterable)) return slice.call(iterable);
return _.values(iterable);
};
Expand Down

0 comments on commit 0202f9f

Please sign in to comment.