Add isLength helper.

This commit is contained in:
John-David Dalton
2014-10-22 22:28:24 -07:00
parent d1d1be49ba
commit b930bd7326

View File

@@ -740,23 +740,6 @@
}; };
}()); }());
/**
* Checks if the provided arguments are from an iteratee call.
*
* @private
* @param {*} value The potential iteratee value argument.
* @param {*} index The potential iteratee index or key argument.
* @param {*} object The potential iteratee object argument.
* @returns {boolean} Returns `true` if the arguments are from an iteratee call, else `false`.
*/
function isIterateeCall(value, index, object) {
var indexType = typeof index,
objectType = typeof object;
return (object && (indexType == 'number' || indexType == 'string') &&
(objectType == 'function' || objectType == 'object') && object[index] === value) || false;
}
/** /**
* Used by `trimmedLeftIndex` and `trimmedRightIndex` to determine if a * Used by `trimmedLeftIndex` and `trimmedRightIndex` to determine if a
* character code is whitespace. * character code is whitespace.
@@ -1689,7 +1672,7 @@
*/ */
function baseEach(collection, iteratee) { function baseEach(collection, iteratee) {
var length = collection ? collection.length : 0; var length = collection ? collection.length : 0;
if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) { if (!isLength(length)) {
return baseForOwn(collection, iteratee); return baseForOwn(collection, iteratee);
} }
var index = -1, var index = -1,
@@ -1714,7 +1697,7 @@
*/ */
function baseEachRight(collection, iteratee) { function baseEachRight(collection, iteratee) {
var length = collection ? collection.length : 0; var length = collection ? collection.length : 0;
if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) { if (!isLength(length)) {
return baseForOwnRight(collection, iteratee); return baseForOwnRight(collection, iteratee);
} }
var iterable = toObject(collection); var iterable = toObject(collection);
@@ -2159,7 +2142,7 @@
length = collection ? collection.length : 0, length = collection ? collection.length : 0,
result = []; result = [];
if (typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER) { if (isLength(length)) {
result.length = length; result.length = length;
} }
baseEach(collection, function(value) { baseEach(collection, function(value) {
@@ -3072,7 +3055,7 @@
* @returns {boolean} Returns `true` if `value` is an array-like object, else `false`. * @returns {boolean} Returns `true` if `value` is an array-like object, else `false`.
*/ */
function isArrayLike(value) { function isArrayLike(value) {
return (value && typeof value == 'object' && typeof value.length == 'number' && return (value && typeof value == 'object' && isLength(value.length) &&
arrayLikeClasses[toString.call(value)]) || false; arrayLikeClasses[toString.call(value)]) || false;
} }
@@ -3087,6 +3070,40 @@
return (value && cloneableClasses[toString.call(value)] && !isHostObject(value)) || false; return (value && cloneableClasses[toString.call(value)] && !isHostObject(value)) || false;
} }
/**
* Checks if the provided arguments are from an iteratee call.
*
* @private
* @param {*} value The potential iteratee value argument.
* @param {*} index The potential iteratee index or key argument.
* @param {*} object The potential iteratee object argument.
* @returns {boolean} Returns `true` if the arguments are from an iteratee call, else `false`.
*/
function isIterateeCall(value, index, object) {
if (!isObject(object)) {
return false;
}
var type = typeof index,
prereq = type == 'string';
if (type == 'number') {
var length = object.length;
prereq = (isLength(length) && index > -1 && index < length && index % 1 == 0);
}
return prereq && object[index] === value;
}
/**
* Checks if `value` is valid array-like length.
*
* @private
* @param {*} value The value to check.
* @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
*/
function isLength(value) {
return typeof value == 'number' && value > -1 && value <= MAX_SAFE_INTEGER;
}
/** /**
* Checks if `value` is suitable for strict equality comparisons, i.e. `===`. * Checks if `value` is suitable for strict equality comparisons, i.e. `===`.
* *
@@ -3260,8 +3277,7 @@
if (value == null) { if (value == null) {
return []; return [];
} }
var length = value.length; if (!isLength(value.length)) {
if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) {
return values(value); return values(value);
} }
if (lodash.support.unindexedChars && isString(value)) { if (lodash.support.unindexedChars && isString(value)) {
@@ -4920,9 +4936,7 @@
* // => ['fred', 'pebbles'] * // => ['fred', 'pebbles']
*/ */
function at(collection) { function at(collection) {
var length = collection ? collection.length : 0; if (!collection || isLength(collection.length)) {
if (typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER) {
collection = toIterable(collection); collection = toIterable(collection);
} }
return baseAt(collection, baseFlatten(arguments, false, false, 1)); return baseAt(collection, baseFlatten(arguments, false, false, 1));
@@ -4963,7 +4977,7 @@
function contains(collection, target, fromIndex) { function contains(collection, target, fromIndex) {
var length = collection ? collection.length : 0; var length = collection ? collection.length : 0;
if (!(typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER)) { if (!isLength(length)) {
collection = values(collection); collection = values(collection);
length = collection.length; length = collection.length;
} }
@@ -5834,9 +5848,7 @@
*/ */
function size(collection) { function size(collection) {
var length = collection ? collection.length : 0; var length = collection ? collection.length : 0;
return (typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER) return isLength(length) ? length : keys(collection).length;
? length
: keys(collection).length;
} }
/** /**
@@ -5947,7 +5959,7 @@
multi = iteratee && isArray(iteratee), multi = iteratee && isArray(iteratee),
result = []; result = [];
if (typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER) { if (isLength(length)) {
result.length = length; result.length = length;
} }
if (!multi) { if (!multi) {
@@ -5990,7 +6002,7 @@
*/ */
function toArray(collection) { function toArray(collection) {
var length = collection ? collection.length : 0; var length = collection ? collection.length : 0;
if (typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER) { if (isLength(length)) {
return (lodash.support.unindexedChars && isString(collection)) return (lodash.support.unindexedChars && isString(collection))
? collection.split('') ? collection.split('')
: baseSlice(collection); : baseSlice(collection);
@@ -7048,15 +7060,14 @@
*/ */
function isArguments(value) { function isArguments(value) {
var length = (value && typeof value == 'object') ? value.length : undefined; var length = (value && typeof value == 'object') ? value.length : undefined;
return (typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER && return (isLength(length) && toString.call(value) == argsClass) || false;
toString.call(value) == argsClass) || false;
} }
// fallback for environments without a `[[Class]]` for `arguments` objects // fallback for environments without a `[[Class]]` for `arguments` objects
if (!support.argsClass) { if (!support.argsClass) {
isArguments = function(value) { isArguments = function(value) {
var length = (value && typeof value == 'object') ? value.length : undefined; var length = (value && typeof value == 'object') ? value.length : undefined;
return (typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER && return (isLength(length) && hasOwnProperty.call(value, 'callee') &&
hasOwnProperty.call(value, 'callee') && !propertyIsEnumerable.call(value, 'callee')) || false; !propertyIsEnumerable.call(value, 'callee')) || false;
}; };
} }
@@ -7181,9 +7192,8 @@
return true; return true;
} }
var length = value.length; var length = value.length;
if ((typeof length == 'number' && length > -1 && length <= MAX_SAFE_INTEGER) && if (isLength(length) && (isArray(value) || isString(value) || isArguments(value) ||
(isArray(value) || isString(value) || isArguments(value) || (typeof value == 'object' && isFunction(value.splice)))) {
(typeof value == 'object' && isFunction(value.splice)))) {
return !length; return !length;
} }
return !keys(value).length; return !keys(value).length;
@@ -8289,11 +8299,13 @@
var isArr = isArrayLike(object); var isArr = isArrayLike(object);
if (accumulator == null) { if (accumulator == null) {
if (isArr) { if (isArr || isObject(object)) {
accumulator = [];
} else if (isObject(object)) {
var Ctor = object.constructor; var Ctor = object.constructor;
accumulator = baseCreate(typeof Ctor == 'function' && Ctor.prototype); if (isArr) {
accumulator = isArray(object) ? new Ctor : [];
} else {
accumulator = baseCreate(typeof Ctor == 'function' && Ctor.prototype);
}
} else { } else {
accumulator = {}; accumulator = {};
} }