diff --git a/lodash.js b/lodash.js index d2af99a4a..149234fe0 100644 --- a/lodash.js +++ b/lodash.js @@ -616,7 +616,11 @@ /** Used to restore the original `_` reference in `noConflict` */ var oldDash = context._; - /** Used as the maximum value returned by `toLength` */ + /** + * Used as the maximum length an array-like object. + * See the [ES6 spec](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength) + * for more details. + */ var maxSafeInteger = Math.pow(2, 53) - 1; /** Used to resolve the internal [[Class]] of values */ @@ -1343,8 +1347,7 @@ iterable = collection, length = collection ? collection.length : 0; - if (typeof length == 'number') { - length = toLength(length); + if (typeof length == 'number' && length > -1 && length <= maxSafeInteger) { if (support.unindexedChars && isString(iterable)) { iterable = iterable.split(''); } @@ -1372,8 +1375,7 @@ var iterable = collection, length = collection ? collection.length : 0; - if (typeof length == 'number') { - length = toLength(length); + if (typeof length == 'number' && length > -1 && length <= maxSafeInteger) { if (support.unindexedChars && isString(iterable)) { iterable = iterable.split(''); } @@ -2205,20 +2207,6 @@ return result; } - /** - * Converts `value` to an integer suitable for use as the length of an array-like - * object. See the [ES6 spec](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tolength) - * for more details. - * - * @private - * @param {*} value The value to convert. - * @returns {number} Returns the converted integer. - */ - function toLength(value) { - var result = +value || 0; - return result < 0 ? 0 : (result < maxSafeInteger ? result : maxSafeInteger); - } - /*--------------------------------------------------------------------------*/ /** @@ -3573,10 +3561,10 @@ * // => true */ function contains(collection, target, fromIndex) { - var length = toLength(collection && collection.length); + var length = collection ? collection.length : 0; fromIndex = (typeof fromIndex == 'number' && +fromIndex) || 0; - if (length) { + if (typeof length == 'number' && length > -1 && length <= maxSafeInteger) { if (typeof collection == 'string' || !isArray(collection) && isString(collection)) { if (fromIndex >= length) { return false; @@ -4470,7 +4458,7 @@ collection = collection.split(''); } if (n == null || guard) { - var length = toLength(collection && collection.length); + var length = collection ? collection.length : 0; return length > 0 ? collection[baseRandom(0, length - 1)] : undefined; } var result = shuffle(collection); @@ -4529,7 +4517,9 @@ */ function size(collection) { var length = collection ? collection.length : 0; - return typeof length == 'number' && length > -1 ? length : keys(collection).length; + return (typeof length == 'number' && length > -1 && length <= maxSafeInteger) + ? length + : keys(collection).length; } /** @@ -4690,7 +4680,7 @@ */ function toArray(collection) { var length = collection && collection.length; - if (typeof length == 'number' && length > -1) { + if (typeof length == 'number' && length > -1 && length <= maxSafeInteger) { return (support.unindexedChars && isString(collection)) ? collection.split('') : slice(collection);