Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #504 from mwilliamson/master

Don't call iterable.toArray in _.toArray if not a function
  • Loading branch information...
commit 62114126d96cbd18b4bbc81cfc87f888be30e1d3 2 parents 6f5489f + c8ec654
@jashkenas jashkenas authored
Showing with 11 additions and 3 deletions.
  1. +10 −2 test/collections.js
  2. +1 −1  underscore.js
View
12 test/collections.js
@@ -264,8 +264,8 @@ $(document).ready(function() {
test('collections: shuffle', function() {
var numbers = _.range(10);
- var shuffled = _.shuffle(numbers).sort();
- notStrictEqual(numbers, shuffled, 'original object is unmodified');
+ var shuffled = _.shuffle(numbers).sort();
+ notStrictEqual(numbers, shuffled, 'original object is unmodified');
equal(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle');
});
@@ -278,6 +278,14 @@ $(document).ready(function() {
var numbers = _.toArray({one : 1, two : 2, three : 3});
equal(numbers.join(', '), '1, 2, 3', 'object flattened into array');
+
+ var objectWithToArrayFunction = {toArray: function() {
+ return [1, 2, 3];
+ }};
+ equal(_.toArray(objectWithToArrayFunction).join(', '), '1, 2, 3', 'toArray method used if present');
+
+ var objectWithToArrayValue = {toArray: 1};
+ equal(_.toArray(objectWithToArrayValue).join(', '), '1', 'toArray property ignored if not a function');
});
test('collections: size', function() {
View
2  underscore.js
@@ -304,7 +304,7 @@
// Safely convert anything iterable into a real, live array.
_.toArray = function(iterable) {
if (!iterable) return [];
- if (iterable.toArray) return iterable.toArray();
+ if (iterable.toArray && _.isFunction(iterable.toArray)) return iterable.toArray();
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.