mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Merge pull request #504 from mwilliamson/master
Don't call iterable.toArray in _.toArray if not a function
This commit is contained in:
@@ -264,8 +264,8 @@ $(document).ready(function() {
|
|||||||
|
|
||||||
test('collections: shuffle', function() {
|
test('collections: shuffle', function() {
|
||||||
var numbers = _.range(10);
|
var numbers = _.range(10);
|
||||||
var shuffled = _.shuffle(numbers).sort();
|
var shuffled = _.shuffle(numbers).sort();
|
||||||
notStrictEqual(numbers, shuffled, 'original object is unmodified');
|
notStrictEqual(numbers, shuffled, 'original object is unmodified');
|
||||||
equal(shuffled.join(','), numbers.join(','), 'contains the same members before and after shuffle');
|
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});
|
var numbers = _.toArray({one : 1, two : 2, three : 3});
|
||||||
equal(numbers.join(', '), '1, 2, 3', 'object flattened into array');
|
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() {
|
test('collections: size', function() {
|
||||||
|
|||||||
@@ -304,7 +304,7 @@
|
|||||||
// Safely convert anything iterable into a real, live array.
|
// Safely convert anything iterable into a real, live array.
|
||||||
_.toArray = function(iterable) {
|
_.toArray = function(iterable) {
|
||||||
if (!iterable) return [];
|
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 (_.isArray(iterable)) return slice.call(iterable);
|
||||||
if (_.isArguments(iterable)) return slice.call(iterable);
|
if (_.isArguments(iterable)) return slice.call(iterable);
|
||||||
return _.values(iterable);
|
return _.values(iterable);
|
||||||
|
|||||||
Reference in New Issue
Block a user