Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make toArray return a clone in case of an array

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...
commit 0202f9f79b882723e3aa19598cc31492c07c3096 1 parent 96dbdaf
@chaoflow chaoflow authored
Showing with 4 additions and 1 deletion.
  1. +3 −0  test/collections.js
  2. +1 −1  underscore.js
View
3  test/collections.js
@@ -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');
View
2  underscore.js
@@ -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);
};
Please sign in to comment.
Something went wrong with that request. Please try again.