Use consistent nullish checks.

This commit is contained in:
John-David Dalton
2016-10-26 22:18:18 -07:00
parent 729d1a57aa
commit bc5729a9de
2 changed files with 70 additions and 76 deletions

130
lodash.js
View File

@@ -503,7 +503,7 @@
*/ */
function arrayAggregator(array, setter, iteratee, accumulator) { function arrayAggregator(array, setter, iteratee, accumulator) {
var index = -1, var index = -1,
length = array ? array.length : 0; length = array == null ? 0 : array.length;
while (++index < length) { while (++index < length) {
var value = array[index]; var value = array[index];
@@ -523,7 +523,7 @@
*/ */
function arrayEach(array, iteratee) { function arrayEach(array, iteratee) {
var index = -1, var index = -1,
length = array ? array.length : 0; length = array == null ? 0 : array.length;
while (++index < length) { while (++index < length) {
if (iteratee(array[index], index, array) === false) { if (iteratee(array[index], index, array) === false) {
@@ -543,7 +543,7 @@
* @returns {Array} Returns `array`. * @returns {Array} Returns `array`.
*/ */
function arrayEachRight(array, iteratee) { function arrayEachRight(array, iteratee) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
while (length--) { while (length--) {
if (iteratee(array[length], length, array) === false) { if (iteratee(array[length], length, array) === false) {
@@ -565,7 +565,7 @@
*/ */
function arrayEvery(array, predicate) { function arrayEvery(array, predicate) {
var index = -1, var index = -1,
length = array ? array.length : 0; length = array == null ? 0 : array.length;
while (++index < length) { while (++index < length) {
if (!predicate(array[index], index, array)) { if (!predicate(array[index], index, array)) {
@@ -586,7 +586,7 @@
*/ */
function arrayFilter(array, predicate) { function arrayFilter(array, predicate) {
var index = -1, var index = -1,
length = array ? array.length : 0, length = array == null ? 0 : array.length,
resIndex = 0, resIndex = 0,
result = []; result = [];
@@ -609,7 +609,7 @@
* @returns {boolean} Returns `true` if `target` is found, else `false`. * @returns {boolean} Returns `true` if `target` is found, else `false`.
*/ */
function arrayIncludes(array, value) { function arrayIncludes(array, value) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
return !!length && baseIndexOf(array, value, 0) > -1; return !!length && baseIndexOf(array, value, 0) > -1;
} }
@@ -624,7 +624,7 @@
*/ */
function arrayIncludesWith(array, value, comparator) { function arrayIncludesWith(array, value, comparator) {
var index = -1, var index = -1,
length = array ? array.length : 0; length = array == null ? 0 : array.length;
while (++index < length) { while (++index < length) {
if (comparator(value, array[index])) { if (comparator(value, array[index])) {
@@ -645,7 +645,7 @@
*/ */
function arrayMap(array, iteratee) { function arrayMap(array, iteratee) {
var index = -1, var index = -1,
length = array ? array.length : 0, length = array == null ? 0 : array.length,
result = Array(length); result = Array(length);
while (++index < length) { while (++index < length) {
@@ -687,7 +687,7 @@
*/ */
function arrayReduce(array, iteratee, accumulator, initAccum) { function arrayReduce(array, iteratee, accumulator, initAccum) {
var index = -1, var index = -1,
length = array ? array.length : 0; length = array == null ? 0 : array.length;
if (initAccum && length) { if (initAccum && length) {
accumulator = array[++index]; accumulator = array[++index];
@@ -711,7 +711,7 @@
* @returns {*} Returns the accumulated value. * @returns {*} Returns the accumulated value.
*/ */
function arrayReduceRight(array, iteratee, accumulator, initAccum) { function arrayReduceRight(array, iteratee, accumulator, initAccum) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (initAccum && length) { if (initAccum && length) {
accumulator = array[--length]; accumulator = array[--length];
} }
@@ -733,7 +733,7 @@
*/ */
function arraySome(array, predicate) { function arraySome(array, predicate) {
var index = -1, var index = -1,
length = array ? array.length : 0; length = array == null ? 0 : array.length;
while (++index < length) { while (++index < length) {
if (predicate(array[index], index, array)) { if (predicate(array[index], index, array)) {
@@ -877,7 +877,7 @@
* @returns {number} Returns the mean. * @returns {number} Returns the mean.
*/ */
function baseMean(array, iteratee) { function baseMean(array, iteratee) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
return length ? (baseSum(array, iteratee) / length) : NAN; return length ? (baseSum(array, iteratee) / length) : NAN;
} }
@@ -1417,7 +1417,7 @@
* var defer = _.runInContext({ 'setTimeout': setImmediate }).defer; * var defer = _.runInContext({ 'setTimeout': setImmediate }).defer;
*/ */
var runInContext = (function runInContext(context) { var runInContext = (function runInContext(context) {
context = context ? _.defaults(root.Object(), context, _.pick(root, contextProps)) : root; context = context == null ? root : _.defaults(root.Object(), context, _.pick(root, contextProps));
/** Built-in constructor references. */ /** Built-in constructor references. */
var Array = context.Array, var Array = context.Array,
@@ -1917,7 +1917,7 @@
*/ */
function Hash(entries) { function Hash(entries) {
var index = -1, var index = -1,
length = entries ? entries.length : 0; length = entries == null ? 0 : entries.length;
this.clear(); this.clear();
while (++index < length) { while (++index < length) {
@@ -2021,7 +2021,7 @@
*/ */
function ListCache(entries) { function ListCache(entries) {
var index = -1, var index = -1,
length = entries ? entries.length : 0; length = entries == null ? 0 : entries.length;
this.clear(); this.clear();
while (++index < length) { while (++index < length) {
@@ -2138,7 +2138,7 @@
*/ */
function MapCache(entries) { function MapCache(entries) {
var index = -1, var index = -1,
length = entries ? entries.length : 0; length = entries == null ? 0 : entries.length;
this.clear(); this.clear();
while (++index < length) { while (++index < length) {
@@ -2242,7 +2242,7 @@
*/ */
function SetCache(values) { function SetCache(values) {
var index = -1, var index = -1,
length = values ? values.length : 0; length = values == null ? 0 : values.length;
this.__data__ = new MapCache; this.__data__ = new MapCache;
while (++index < length) { while (++index < length) {
@@ -2589,12 +2589,12 @@
*/ */
function baseAt(object, paths) { function baseAt(object, paths) {
var index = -1, var index = -1,
isNil = object == null,
length = paths.length, length = paths.length,
result = Array(length); result = Array(length),
skip = object == null;
while (++index < length) { while (++index < length) {
result[index] = isNil ? undefined : get(object, paths[index]); result[index] = skip ? undefined : get(object, paths[index]);
} }
return result; return result;
} }
@@ -2784,7 +2784,7 @@
outer: outer:
while (++index < length) { while (++index < length) {
var value = array[index], var value = array[index],
computed = iteratee ? iteratee(value) : value; computed = iteratee == null ? value : iteratee(value);
value = (comparator || value !== 0) ? value : 0; value = (comparator || value !== 0) ? value : 0;
if (isCommon && computed === computed) { if (isCommon && computed === computed) {
@@ -4114,7 +4114,7 @@
*/ */
function baseSortedIndex(array, value, retHighest) { function baseSortedIndex(array, value, retHighest) {
var low = 0, var low = 0,
high = array ? array.length : low; high = array == null ? low : array.length;
if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) { if (typeof value == 'number' && value === value && high <= HALF_MAX_ARRAY_LENGTH) {
while (low < high) { while (low < high) {
@@ -4150,7 +4150,7 @@
value = iteratee(value); value = iteratee(value);
var low = 0, var low = 0,
high = array ? array.length : 0, high = array == null ? 0 : array.length,
valIsNaN = value !== value, valIsNaN = value !== value,
valIsNull = value === null, valIsNull = value === null,
valIsSymbol = isSymbol(value), valIsSymbol = isSymbol(value),
@@ -6107,7 +6107,7 @@
if (result || ++index != length) { if (result || ++index != length) {
return result; return result;
} }
length = object ? object.length : 0; length = object == null ? 0 : object.length;
return !!length && isLength(length) && isIndex(key, length) && return !!length && isLength(length) && isIndex(key, length) &&
(isArray(object) || isArguments(object)); (isArray(object) || isArguments(object));
} }
@@ -6819,7 +6819,7 @@
} else { } else {
size = nativeMax(toInteger(size), 0); size = nativeMax(toInteger(size), 0);
} }
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length || size < 1) { if (!length || size < 1) {
return []; return [];
} }
@@ -6850,7 +6850,7 @@
*/ */
function compact(array) { function compact(array) {
var index = -1, var index = -1,
length = array ? array.length : 0, length = array == null ? 0 : array.length,
resIndex = 0, resIndex = 0,
result = []; result = [];
@@ -7022,7 +7022,7 @@
* // => [1, 2, 3] * // => [1, 2, 3]
*/ */
function drop(array, n, guard) { function drop(array, n, guard) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return []; return [];
} }
@@ -7056,7 +7056,7 @@
* // => [1, 2, 3] * // => [1, 2, 3]
*/ */
function dropRight(array, n, guard) { function dropRight(array, n, guard) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return []; return [];
} }
@@ -7177,7 +7177,7 @@
* // => [4, '*', '*', 10] * // => [4, '*', '*', 10]
*/ */
function fill(array, value, start, end) { function fill(array, value, start, end) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return []; return [];
} }
@@ -7224,7 +7224,7 @@
* // => 2 * // => 2
*/ */
function findIndex(array, predicate, fromIndex) { function findIndex(array, predicate, fromIndex) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return -1; return -1;
} }
@@ -7271,7 +7271,7 @@
* // => 0 * // => 0
*/ */
function findLastIndex(array, predicate, fromIndex) { function findLastIndex(array, predicate, fromIndex) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return -1; return -1;
} }
@@ -7300,7 +7300,7 @@
* // => [1, 2, [3, [4]], 5] * // => [1, 2, [3, [4]], 5]
*/ */
function flatten(array) { function flatten(array) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
return length ? baseFlatten(array, 1) : []; return length ? baseFlatten(array, 1) : [];
} }
@@ -7319,7 +7319,7 @@
* // => [1, 2, 3, 4, 5] * // => [1, 2, 3, 4, 5]
*/ */
function flattenDeep(array) { function flattenDeep(array) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
return length ? baseFlatten(array, INFINITY) : []; return length ? baseFlatten(array, INFINITY) : [];
} }
@@ -7344,7 +7344,7 @@
* // => [1, 2, 3, [4], 5] * // => [1, 2, 3, [4], 5]
*/ */
function flattenDepth(array, depth) { function flattenDepth(array, depth) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return []; return [];
} }
@@ -7369,7 +7369,7 @@
*/ */
function fromPairs(pairs) { function fromPairs(pairs) {
var index = -1, var index = -1,
length = pairs ? pairs.length : 0, length = pairs == null ? 0 : pairs.length,
result = {}; result = {};
while (++index < length) { while (++index < length) {
@@ -7425,7 +7425,7 @@
* // => 3 * // => 3
*/ */
function indexOf(array, value, fromIndex) { function indexOf(array, value, fromIndex) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return -1; return -1;
} }
@@ -7451,7 +7451,7 @@
* // => [1, 2] * // => [1, 2]
*/ */
function initial(array) { function initial(array) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
return length ? baseSlice(array, 0, -1) : []; return length ? baseSlice(array, 0, -1) : [];
} }
@@ -7541,9 +7541,8 @@
var comparator = last(arrays), var comparator = last(arrays),
mapped = arrayMap(arrays, castArrayLikeObject); mapped = arrayMap(arrays, castArrayLikeObject);
if (comparator === last(mapped)) { comparator = typeof comparator == 'function' ? comparator : undefined;
comparator = undefined; if (comparator) {
} else {
mapped.pop(); mapped.pop();
} }
return (mapped.length && mapped[0] === arrays[0]) return (mapped.length && mapped[0] === arrays[0])
@@ -7567,7 +7566,7 @@
* // => 'a~b~c' * // => 'a~b~c'
*/ */
function join(array, separator) { function join(array, separator) {
return array ? nativeJoin.call(array, separator) : ''; return array == null ? '' : nativeJoin.call(array, separator);
} }
/** /**
@@ -7585,7 +7584,7 @@
* // => 3 * // => 3
*/ */
function last(array) { function last(array) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
return length ? array[length - 1] : undefined; return length ? array[length - 1] : undefined;
} }
@@ -7611,7 +7610,7 @@
* // => 1 * // => 1
*/ */
function lastIndexOf(array, value, fromIndex) { function lastIndexOf(array, value, fromIndex) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return -1; return -1;
} }
@@ -7784,7 +7783,7 @@
* // => ['b', 'd'] * // => ['b', 'd']
*/ */
var pullAt = flatRest(function(array, indexes) { var pullAt = flatRest(function(array, indexes) {
var length = array ? array.length : 0, var length = array == null ? 0 : array.length,
result = baseAt(array, indexes); result = baseAt(array, indexes);
basePullAt(array, arrayMap(indexes, function(index) { basePullAt(array, arrayMap(indexes, function(index) {
@@ -7867,7 +7866,7 @@
* // => [3, 2, 1] * // => [3, 2, 1]
*/ */
function reverse(array) { function reverse(array) {
return array ? nativeReverse.call(array) : array; return array == null ? array : nativeReverse.call(array);
} }
/** /**
@@ -7887,7 +7886,7 @@
* @returns {Array} Returns the slice of `array`. * @returns {Array} Returns the slice of `array`.
*/ */
function slice(array, start, end) { function slice(array, start, end) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return []; return [];
} }
@@ -7969,7 +7968,7 @@
* // => 1 * // => 1
*/ */
function sortedIndexOf(array, value) { function sortedIndexOf(array, value) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (length) { if (length) {
var index = baseSortedIndex(array, value); var index = baseSortedIndex(array, value);
if (index < length && eq(array[index], value)) { if (index < length && eq(array[index], value)) {
@@ -8047,7 +8046,7 @@
* // => 3 * // => 3
*/ */
function sortedLastIndexOf(array, value) { function sortedLastIndexOf(array, value) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (length) { if (length) {
var index = baseSortedIndex(array, value, true) - 1; var index = baseSortedIndex(array, value, true) - 1;
if (eq(array[index], value)) { if (eq(array[index], value)) {
@@ -8115,7 +8114,7 @@
* // => [2, 3] * // => [2, 3]
*/ */
function tail(array) { function tail(array) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
return length ? baseSlice(array, 1, length) : []; return length ? baseSlice(array, 1, length) : [];
} }
@@ -8178,7 +8177,7 @@
* // => [] * // => []
*/ */
function takeRight(array, n, guard) { function takeRight(array, n, guard) {
var length = array ? array.length : 0; var length = array == null ? 0 : array.length;
if (!length) { if (!length) {
return []; return [];
} }
@@ -8343,9 +8342,7 @@
*/ */
var unionWith = baseRest(function(arrays) { var unionWith = baseRest(function(arrays) {
var comparator = last(arrays); var comparator = last(arrays);
if (isArrayLikeObject(comparator)) { comparator = typeof comparator == 'function' ? comparator : undefined;
comparator = undefined;
}
return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), undefined, comparator); return baseUniq(baseFlatten(arrays, 1, isArrayLikeObject, true), undefined, comparator);
}); });
@@ -8368,9 +8365,7 @@
* // => [2, 1] * // => [2, 1]
*/ */
function uniq(array) { function uniq(array) {
return (array && array.length) return (array && array.length) ? baseUniq(array) : [];
? baseUniq(array)
: [];
} }
/** /**
@@ -8397,9 +8392,7 @@
* // => [{ 'x': 1 }, { 'x': 2 }] * // => [{ 'x': 1 }, { 'x': 2 }]
*/ */
function uniqBy(array, iteratee) { function uniqBy(array, iteratee) {
return (array && array.length) return (array && array.length) ? baseUniq(array, getIteratee(iteratee, 2)) : [];
? baseUniq(array, getIteratee(iteratee, 2))
: [];
} }
/** /**
@@ -8423,9 +8416,8 @@
* // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }] * // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
*/ */
function uniqWith(array, comparator) { function uniqWith(array, comparator) {
return (array && array.length) comparator = typeof comparator == 'function' ? comparator : undefined;
? baseUniq(array, undefined, comparator) return (array && array.length) ? baseUniq(array, undefined, comparator) : [];
: [];
} }
/** /**
@@ -8599,9 +8591,7 @@
*/ */
var xorWith = baseRest(function(arrays) { var xorWith = baseRest(function(arrays) {
var comparator = last(arrays); var comparator = last(arrays);
if (isArrayLikeObject(comparator)) { comparator = typeof comparator == 'function' ? comparator : undefined;
comparator = undefined;
}
return baseXor(arrayFilter(arrays, isArrayLikeObject), undefined, comparator); return baseXor(arrayFilter(arrays, isArrayLikeObject), undefined, comparator);
}); });
@@ -10524,7 +10514,7 @@
* _.memoize.Cache = WeakMap; * _.memoize.Cache = WeakMap;
*/ */
function memoize(func, resolver) { function memoize(func, resolver) {
if (typeof func != 'function' || (resolver && typeof resolver != 'function')) { if (typeof func != 'function' || (resolver != null && typeof resolver != 'function')) {
throw new TypeError(FUNC_ERROR_TEXT); throw new TypeError(FUNC_ERROR_TEXT);
} }
var memoized = function() { var memoized = function() {
@@ -11048,6 +11038,7 @@
* // => 0 * // => 0
*/ */
function cloneWith(value, customizer) { function cloneWith(value, customizer) {
customizer = typeof customizer == 'function' ? customizer : undefined;
return baseClone(value, false, true, customizer); return baseClone(value, false, true, customizer);
} }
@@ -11102,6 +11093,7 @@
* // => 20 * // => 20
*/ */
function cloneDeepWith(value, customizer) { function cloneDeepWith(value, customizer) {
customizer = typeof customizer == 'function' ? customizer : undefined;
return baseClone(value, true, true, customizer); return baseClone(value, true, true, customizer);
} }
@@ -12741,7 +12733,7 @@
*/ */
function create(prototype, properties) { function create(prototype, properties) {
var result = baseCreate(prototype); var result = baseCreate(prototype);
return properties ? baseAssign(result, properties) : result; return properties == null ? result : baseAssign(result, properties);
} }
/** /**
@@ -13848,7 +13840,7 @@
* // => ['h', 'i'] * // => ['h', 'i']
*/ */
function values(object) { function values(object) {
return object ? baseValues(object, keys(object)) : []; return object == null ? [] : baseValues(object, keys(object));
} }
/** /**
@@ -15235,7 +15227,7 @@
* // => 'no match' * // => 'no match'
*/ */
function cond(pairs) { function cond(pairs) {
var length = pairs ? pairs.length : 0, var length = pairs == null ? 0 : pairs.length,
toIteratee = getIteratee(); toIteratee = getIteratee();
pairs = !length ? [] : arrayMap(pairs, function(pair) { pairs = !length ? [] : arrayMap(pairs, function(pair) {

View File

@@ -14612,12 +14612,13 @@
assert.raises(function() { _.memoize(noop, true); }, TypeError); assert.raises(function() { _.memoize(noop, true); }, TypeError);
}); });
QUnit.test('should not error if `resolver` is falsey', function(assert) { QUnit.test('should not error if `resolver` is nullish', function(assert) {
assert.expect(1); assert.expect(1);
var expected = lodashStable.map(falsey, stubTrue); var values = [, null, undefined],
expected = lodashStable.map(values, stubTrue);
var actual = lodashStable.map(falsey, function(resolver, index) { var actual = lodashStable.map(values, function(resolver, index) {
try { try {
return _.isFunction(index ? _.memoize(noop, resolver) : _.memoize(noop)); return _.isFunction(index ? _.memoize(noop, resolver) : _.memoize(noop));
} catch (e) {} } catch (e) {}
@@ -14656,7 +14657,7 @@
lodashStable.times(2, function(index) { lodashStable.times(2, function(index) {
var count = 0, var count = 0,
resolver = index && identity; resolver = index ? identity : undefined;
var memoized = _.memoize(function() { var memoized = _.memoize(function() {
count++; count++;
@@ -20615,12 +20616,13 @@
assert.deepEqual(actual, expected); assert.deepEqual(actual, expected);
}); });
QUnit.test('`_.' + methodName + '` should accept a falsey `array` argument and a `value`', function(assert) { QUnit.test('`_.' + methodName + '` should accept a nullish `array` and a `value`', function(assert) {
assert.expect(1); assert.expect(1);
var expected = lodashStable.map(falsey, lodashStable.constant([0, 0, 0])); var values = [null, undefined],
expected = lodashStable.map(values, lodashStable.constant([0, 0, 0]));
var actual = lodashStable.map(falsey, function(array) { var actual = lodashStable.map(values, function(array) {
return [func(array, 1), func(array, undefined), func(array, NaN)]; return [func(array, 1), func(array, undefined), func(array, NaN)];
}); });