Ensure _.toIterable converts primitives to objects for easier detection of callback signatures.

This commit is contained in:
John-David Dalton
2014-07-25 07:44:27 -07:00
parent d5e55f9dbd
commit c159da1fbc

View File

@@ -1693,7 +1693,7 @@
iterable = toIterable(collection); iterable = toIterable(collection);
while (++index < length) { while (++index < length) {
if (iterator(iterable[index], index, collection) === false) { if (iterator(iterable[index], index, iterable) === false) {
break; break;
} }
} }
@@ -1716,7 +1716,7 @@
} }
var iterable = toIterable(collection); var iterable = toIterable(collection);
while (length--) { while (length--) {
if (iterator(iterable[length], length, collection) === false) { if (iterator(iterable[length], length, iterable) === false) {
break; break;
} }
} }
@@ -3083,13 +3083,23 @@
* @returns {Array} Returns the iterable object. * @returns {Array} Returns the iterable object.
*/ */
function toIterable(collection) { function toIterable(collection) {
var length = collection ? collection.length : 0; if (collection == null) {
return [];
}
var length = collection.length;
if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) { if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) {
return values(collection); return values(collection);
} else if (support.unindexedChars && isString(collection)) {
return collection.split('');
} }
return collection || []; collection = Object(collection);
if (support.unindexedChars && isString(collection)) {
var index = -1;
length = collection.length;
while (++index < length) {
collection[index] = collection.charAt(index);
}
}
return collection;
} }
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -5547,8 +5557,13 @@
* // => [2, 3, 4] * // => [2, 3, 4]
*/ */
function toArray(collection) { function toArray(collection) {
var iterable = toIterable(collection); var length = collection ? collection.length : 0;
return iterable === collection ? slice(collection) : iterable; if (typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER) {
return (support.unindexedChars && isString(collection))
? collection.split('')
: slice(collection);
}
return values(collection);
} }
/** /**