Use Array.isArray.

This commit is contained in:
John-David Dalton
2017-01-09 18:44:38 -08:00
parent 395a81058b
commit 68190b8546
31 changed files with 34 additions and 64 deletions

View File

@@ -1,6 +1,5 @@
import baseTimes from './_baseTimes.js';
import isArguments from './isArguments.js';
import isArray from './isArray.js';
import isBuffer from './isBuffer.js';
import isIndex from './_isIndex.js';
import isTypedArray from './isTypedArray.js';
@@ -17,7 +16,7 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
* @returns {Array} Returns the array of property names.
*/
function arrayLikeKeys(value, inherited) {
const isArr = isArray(value);
const isArr = Array.isArray(value);
const isArg = !isArr && isArguments(value);
const isBuff = !isArr && !isArg && isBuffer(value);
const isType = !isArr && !isArg && !isBuff && isTypedArray(value);

View File

@@ -13,7 +13,6 @@ import getTag from './_getTag.js';
import initCloneArray from './_initCloneArray.js';
import initCloneByTag from './_initCloneByTag.js';
import initCloneObject from './_initCloneObject.js';
import isArray from './isArray.js';
import isBuffer from './isBuffer.js';
import isObject from './isObject.js';
import keys from './keys.js';
@@ -99,7 +98,7 @@ function baseClone(value, bitmask, customizer, key, object, stack) {
if (!isObject(value)) {
return value;
}
const isArr = isArray(value);
const isArr = Array.isArray(value);
if (isArr) {
result = initCloneArray(value);
if (!isDeep) {

View File

@@ -1,5 +1,4 @@
import arrayPush from './_arrayPush.js';
import isArray from './isArray.js';
/**
* The base implementation of `getAllKeys` and `getAllKeysIn` which uses
@@ -14,7 +13,7 @@ import isArray from './isArray.js';
*/
function baseGetAllKeys(object, keysFunc, symbolsFunc) {
const result = keysFunc(object);
return isArray(object) ? result : arrayPush(result, symbolsFunc(object));
return Array.isArray(object) ? result : arrayPush(result, symbolsFunc(object));
}
export default baseGetAllKeys;

View File

@@ -3,7 +3,6 @@ import equalArrays from './_equalArrays.js';
import equalByTag from './_equalByTag.js';
import equalObjects from './_equalObjects.js';
import getTag from './_getTag.js';
import isArray from './isArray.js';
import isBuffer from './isBuffer.js';
import isTypedArray from './isTypedArray.js';
@@ -33,8 +32,8 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
* @returns {boolean} Returns `true` if the objects are equivalent, else `false`.
*/
function baseIsEqualDeep(object, other, bitmask, customizer, equalFunc, stack) {
let objIsArr = isArray(object);
const othIsArr = isArray(other);
let objIsArr = Array.isArray(object);
const othIsArr = Array.isArray(other);
let objTag = objIsArr ? arrayTag : getTag(object);
let othTag = othIsArr ? arrayTag : getTag(other);

View File

@@ -4,7 +4,6 @@ import cloneTypedArray from './_cloneTypedArray.js';
import copyArray from './_copyArray.js';
import initCloneObject from './_initCloneObject.js';
import isArguments from './isArguments.js';
import isArray from './isArray.js';
import isArrayLikeObject from './isArrayLikeObject.js';
import isBuffer from './isBuffer.js';
import isFunction from './isFunction.js';
@@ -44,13 +43,13 @@ function baseMergeDeep(object, source, key, srcIndex, mergeFunc, customizer, sta
let isCommon = newValue === undefined;
if (isCommon) {
const isArr = isArray(srcValue);
const isArr = Array.isArray(srcValue);
const isBuff = !isArr && isBuffer(srcValue);
const isTyped = !isArr && !isBuff && isTypedArray(srcValue);
newValue = srcValue;
if (isArr || isBuff || isTyped) {
if (isArray(objValue)) {
if (Array.isArray(objValue)) {
newValue = objValue;
}
else if (isArrayLikeObject(objValue)) {

View File

@@ -1,6 +1,5 @@
import Symbol from './_Symbol.js';
import arrayMap from './_arrayMap.js';
import isArray from './isArray.js';
import isSymbol from './isSymbol.js';
/** Used as references for various `Number` constants. */
@@ -23,7 +22,7 @@ function baseToString(value) {
if (typeof value == 'string') {
return value;
}
if (isArray(value)) {
if (Array.isArray(value)) {
// Recursively convert values (susceptible to call stack limits).
return `${ arrayMap(value, baseToString) }`;
}

View File

@@ -1,4 +1,3 @@
import isArray from './isArray.js';
import isKey from './_isKey.js';
import stringToPath from './_stringToPath.js';
import toString from './toString.js';
@@ -12,7 +11,7 @@ import toString from './toString.js';
* @returns {Array} Returns the cast property path array.
*/
function castPath(value, object) {
if (isArray(value)) {
if (Array.isArray(value)) {
return value;
}
return isKey(value, object) ? [value] : stringToPath(toString(value));

View File

@@ -1,6 +1,5 @@
import arrayAggregator from './_arrayAggregator.js';
import baseAggregator from './_baseAggregator.js';
import isArray from './isArray.js';
/**
* Creates a function like `groupBy`.
@@ -12,7 +11,7 @@ import isArray from './isArray.js';
*/
function createAggregator(setter, initializer) {
return (collection, iteratee) => {
const func = isArray(collection) ? arrayAggregator : baseAggregator;
const func = Array.isArray(collection) ? arrayAggregator : baseAggregator;
const accumulator = initializer ? initializer() : {};
return func(collection, setter, iteratee, accumulator);
};

View File

@@ -1,6 +1,5 @@
import castPath from './_castPath.js';
import isArguments from './isArguments.js';
import isArray from './isArray.js';
import isIndex from './_isIndex.js';
import isLength from './isLength.js';
import toKey from './_toKey.js';
@@ -34,7 +33,7 @@ function hasPath(object, path, hasFunc) {
}
length = object == null ? 0 : object.length;
return !!length && isLength(length) && isIndex(key, length) &&
(isArray(object) || isArguments(object));
(Array.isArray(object) || isArguments(object));
}
export default hasPath;

View File

@@ -1,6 +1,5 @@
import Symbol from './_Symbol.js';
import isArguments from './isArguments.js';
import isArray from './isArray.js';
/** Built-in value references. */
const spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined;
@@ -13,7 +12,7 @@ const spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined;
* @returns {boolean} Returns `true` if `value` is flattenable, else `false`.
*/
function isFlattenable(value) {
return isArray(value) || isArguments(value) ||
return Array.isArray(value) || isArguments(value) ||
!!(spreadableSymbol && value && value[spreadableSymbol]);
}

View File

@@ -1,4 +1,3 @@
import isArray from './isArray.js';
import isSymbol from './isSymbol.js';
/** Used to match property names within property paths. */
@@ -14,7 +13,7 @@ const reIsPlainProp = /^\w*$/;
* @returns {boolean} Returns `true` if `value` is a property name, else `false`.
*/
function isKey(value, object) {
if (isArray(value)) {
if (Array.isArray(value)) {
return false;
}
const type = typeof value;

View File

@@ -1,4 +1,3 @@
import isArray from './isArray.js';
/**
* Casts `value` as an array if it's not one.
@@ -37,7 +36,7 @@ function castArray(...args) {
return [];
}
const value = args[0];
return isArray(value) ? value : [value];
return Array.isArray(value) ? value : [value];
}
export default castArray;

View File

@@ -1,7 +1,6 @@
import arrayPush from './_arrayPush.js';
import baseFlatten from './_baseFlatten.js';
import copyArray from './_copyArray.js';
import isArray from './isArray.js';
/**
* Creates a new array concatenating `array` with any additional arrays
@@ -25,7 +24,7 @@ import isArray from './isArray.js';
* // => [1]
*/
function concat(array, ...values) {
return arrayPush(isArray(array) ? copyArray(array) : [array], baseFlatten(args, 1));
return arrayPush(Array.isArray(array) ? copyArray(array) : [array], baseFlatten(args, 1));
}
export default concat;

View File

@@ -1,6 +1,5 @@
import arrayEvery from './_arrayEvery.js';
import baseEvery from './_baseEvery.js';
import isArray from './isArray.js';
import isIterateeCall from './_isIterateeCall.js';
/**
@@ -27,7 +26,7 @@ import isIterateeCall from './_isIterateeCall.js';
* // => false
*/
function every(collection, predicate, guard) {
const func = isArray(collection) ? arrayEvery : baseEvery;
const func = Array.isArray(collection) ? arrayEvery : baseEvery;
if (guard && isIterateeCall(collection, predicate, guard)) {
predicate = undefined;
}

View File

@@ -1,6 +1,5 @@
import arrayFilter from './_arrayFilter.js';
import baseFilter from './_baseFilter.js';
import isArray from './isArray.js';
/**
* Iterates over elements of `collection`, returning an array of all elements
@@ -27,7 +26,7 @@ import isArray from './isArray.js';
* // => objects for ['fred']
*/
function filter(collection, predicate) {
const func = isArray(collection) ? arrayFilter : baseFilter;
const func = Array.isArray(collection) ? arrayFilter : baseFilter;
return func(collection, predicate);
}

View File

@@ -1,6 +1,5 @@
import arrayEach from './_arrayEach.js';
import baseEach from './_baseEach.js';;
import isArray from './isArray.js';
/**
* Iterates over elements of `collection` and invokes `iteratee` for each element.
@@ -32,7 +31,7 @@ import isArray from './isArray.js';
* // => Logs 'a' then 'b' (iteration order is not guaranteed).
*/
function forEach(collection, iteratee) {
const func = isArray(collection) ? arrayEach : baseEach;
const func = Array.isArray(collection) ? arrayEach : baseEach;
return func(collection, iteratee);
}

View File

@@ -1,6 +1,5 @@
import arrayEachRight from './_arrayEachRight.js';
import baseEachRight from './_baseEachRight.js';
import isArray from './isArray.js';
/**
* This method is like `forEach` except that it iterates over elements of
@@ -22,7 +21,7 @@ import isArray from './isArray.js';
* // => Logs `2` then `1`.
*/
function forEachRight(collection, iteratee) {
const func = isArray(collection) ? arrayEachRight : baseEachRight;
const func = Array.isArray(collection) ? arrayEachRight : baseEachRight;
return func(collection, iteratee);
}

View File

@@ -1,7 +1,6 @@
import baseKeys from './_baseKeys.js';
import getTag from './_getTag.js';
import isArguments from './isArguments.js';
import isArray from './isArray.js';
import isArrayLike from './isArrayLike.js';
import isBuffer from './isBuffer.js';
import isPrototype from './_isPrototype.js';
@@ -51,7 +50,7 @@ function isEmpty(value) {
return true;
}
if (isArrayLike(value) &&
(isArray(value) || typeof value == 'string' || typeof value.splice == 'function' ||
(Array.isArray(value) || typeof value == 'string' || typeof value.splice == 'function' ||
isBuffer(value) || isTypedArray(value) || isArguments(value))) {
return !value.length;
}

View File

@@ -1,5 +1,4 @@
import baseGetTag from './_baseGetTag.js';
import isArray from './isArray.js';
import isObjectLike from './isObjectLike.js';
/** `Object#toString` result references. */
@@ -23,7 +22,7 @@ const stringTag = '[object String]';
*/
function isString(value) {
return typeof value == 'string' ||
(!isArray(value) && isObjectLike(value) && baseGetTag(value) == stringTag);
(!Array.isArray(value) && isObjectLike(value) && baseGetTag(value) == stringTag);
}
export default isString;

3
map.js
View File

@@ -1,6 +1,5 @@
import arrayMap from './_arrayMap.js';
import baseMap from './_baseMap.js';
import isArray from './isArray.js';
/**
* Creates an array of values by running each element in `collection` thru
@@ -35,7 +34,7 @@ import isArray from './isArray.js';
* // => [16, 64] (iteration order is not guaranteed)
*/
function map(collection, iteratee) {
const func = isArray(collection) ? arrayMap : baseMap;
const func = Array.isArray(collection) ? arrayMap : baseMap;
return func(collection, iteratee);
}

View File

@@ -20,7 +20,7 @@ import createAssigner from './_createAssigner.js';
* @example
*
* function customizer(objValue, srcValue) {
* if (isArray(objValue)) {
* if (Array.isArray(objValue)) {
* return objValue.concat(srcValue);
* }
* }

View File

@@ -1,5 +1,4 @@
import baseOrderBy from './_baseOrderBy.js';
import isArray from './isArray.js';
/**
* This method is like `sortBy` except that it allows specifying the sort
@@ -33,11 +32,11 @@ function orderBy(collection, iteratees, orders, guard) {
if (collection == null) {
return [];
}
if (!isArray(iteratees)) {
if (!Array.isArray(iteratees)) {
iteratees = iteratees == null ? [] : [iteratees];
}
orders = guard ? undefined : orders;
if (!isArray(orders)) {
if (!Array.isArray(orders)) {
orders = orders == null ? [] : [orders];
}
return baseOrderBy(collection, iteratees, orders);

View File

@@ -1,7 +1,6 @@
import arrayReduce from './_arrayReduce.js';
import baseEach from './_baseEach.js';
import baseReduce from './_baseReduce.js';
import isArray from './isArray.js';
/**
* Reduces `collection` to a value which is the accumulated result of running
@@ -40,7 +39,7 @@ import isArray from './isArray.js';
* // => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)
*/
function reduce(collection, iteratee, accumulator) {
const func = isArray(collection) ? arrayReduce : baseReduce;
const func = Array.isArray(collection) ? arrayReduce : baseReduce;
const initAccum = arguments.length < 3;
return func(collection, iteratee, accumulator, initAccum, baseEach);
}

View File

@@ -1,7 +1,6 @@
import arrayReduceRight from './_arrayReduceRight.js';
import baseEachRight from './_baseEachRight.js';
import baseReduce from './_baseReduce.js';
import isArray from './isArray.js';
/**
* This method is like `reduce` except that it iterates over elements of
@@ -25,7 +24,7 @@ import isArray from './isArray.js';
* // => [4, 5, 2, 3, 0, 1]
*/
function reduceRight(collection, iteratee, accumulator) {
const func = isArray(collection) ? arrayReduceRight : baseReduce;
const func = Array.isArray(collection) ? arrayReduceRight : baseReduce;
const initAccum = arguments.length < 3;
return func(collection, iteratee, accumulator, initAccum, baseEachRight);
}

View File

@@ -1,6 +1,5 @@
import arrayFilter from './_arrayFilter.js';
import baseFilter from './_baseFilter.js';
import isArray from './isArray.js';
import negate from './negate.js';
/**
@@ -25,7 +24,7 @@ import negate from './negate.js';
* // => objects for ['fred']
*/
function reject(collection, predicate) {
const func = isArray(collection) ? arrayFilter : baseFilter;
const func = Array.isArray(collection) ? arrayFilter : baseFilter;
return func(collection, negate(predicate));
}

View File

@@ -1,6 +1,5 @@
import arraySample from './_arraySample.js';
import baseSample from './_baseSample.js';
import isArray from './isArray.js';
/**
* Gets a random element from `collection`.
@@ -16,7 +15,7 @@ import isArray from './isArray.js';
* // => 2
*/
function sample(collection) {
const func = isArray(collection) ? arraySample : baseSample;
const func = Array.isArray(collection) ? arraySample : baseSample;
return func(collection);
}

View File

@@ -1,6 +1,5 @@
import arraySampleSize from './_arraySampleSize.js';
import baseSampleSize from './_baseSampleSize.js';
import isArray from './isArray.js';
import isIterateeCall from './_isIterateeCall.js';
import toInteger from './toInteger.js';
@@ -29,7 +28,7 @@ function sampleSize(collection, n, guard) {
} else {
n = toInteger(n);
}
const func = isArray(collection) ? arraySampleSize : baseSampleSize;
const func = Array.isArray(collection) ? arraySampleSize : baseSampleSize;
return func(collection, n);
}

View File

@@ -1,6 +1,5 @@
import arrayShuffle from './_arrayShuffle.js';
import baseShuffle from './_baseShuffle.js';
import isArray from './isArray.js';
/**
* Creates an array of shuffled values, using a version of the
@@ -17,7 +16,7 @@ import isArray from './isArray.js';
* // => [4, 1, 3, 2]
*/
function shuffle(collection) {
const func = isArray(collection) ? arrayShuffle : baseShuffle;
const func = Array.isArray(collection) ? arrayShuffle : baseShuffle;
return func(collection);
}

View File

@@ -1,6 +1,5 @@
import arraySome from './_arraySome.js';
import baseSome from './_baseSome.js';
import isArray from './isArray.js';
import isIterateeCall from './_isIterateeCall.js';
/**
@@ -22,7 +21,7 @@ import isIterateeCall from './_isIterateeCall.js';
* // => true
*/
function some(collection, predicate, guard) {
const func = isArray(collection) ? arraySome : baseSome;
const func = Array.isArray(collection) ? arraySome : baseSome;
if (guard && isIterateeCall(collection, predicate, guard)) {
predicate = undefined;
}

View File

@@ -1,6 +1,5 @@
import arrayMap from './_arrayMap.js';
import copyArray from './_copyArray.js';
import isArray from './isArray.js';
import isSymbol from './isSymbol.js';
import stringToPath from './_stringToPath.js';
import toKey from './_toKey.js';
@@ -23,7 +22,7 @@ import toString from './toString.js';
* // => ['a', '0', 'b', 'c']
*/
function toPath(value) {
if (isArray(value)) {
if (Array.isArray(value)) {
return arrayMap(value, toKey);
}
return isSymbol(value) ? [value] : copyArray(stringToPath(toString(value)));

View File

@@ -2,7 +2,6 @@ import arrayEach from './_arrayEach.js';
import baseCreate from './_baseCreate.js';
import baseForOwn from './_baseForOwn.js';
import getPrototype from './_getPrototype.js';
import isArray from './isArray.js';
import isBuffer from './isBuffer.js';
import isFunction from './isFunction.js';
import isObject from './isObject.js';
@@ -38,7 +37,7 @@ import isTypedArray from './isTypedArray.js';
* // => { '1': ['a', 'c'], '2': ['b'] }
*/
function transform(object, iteratee, accumulator) {
const isArr = isArray(object);
const isArr = Array.isArray(object);
const isArrLike = isArr || isBuffer(object) || isTypedArray(object);
if (accumulator == null) {