Remove unneeded isIterateeCall checks.

This commit is contained in:
John-David Dalton
2015-07-29 10:56:41 -07:00
parent ccb90f46e6
commit b17f6eee6a
2 changed files with 51 additions and 94 deletions

103
lodash.js
View File

@@ -4293,9 +4293,7 @@
if (!(array && array.length)) { if (!(array && array.length)) {
return []; return [];
} }
if (guard ? isIterateeCall(array, n, guard) : n == null) { n = (guard || n == null) ? 1 : n;
n = 1;
}
return baseSlice(array, n < 0 ? 0 : n); return baseSlice(array, n < 0 ? 0 : n);
} }
@@ -4328,9 +4326,7 @@
if (!length) { if (!length) {
return []; return [];
} }
if (guard ? isIterateeCall(array, n, guard) : n == null) { n = (guard || n == null) ? 1 : n;
n = 1;
}
n = length - (+n || 0); n = length - (+n || 0);
return baseSlice(array, 0, n < 0 ? 0 : n); return baseSlice(array, 0, n < 0 ? 0 : n);
} }
@@ -5061,9 +5057,7 @@
if (!(array && array.length)) { if (!(array && array.length)) {
return []; return [];
} }
if (guard ? isIterateeCall(array, n, guard) : n == null) { n = (guard || n == null) ? 1 : n;
n = 1;
}
return baseSlice(array, 0, n < 0 ? 0 : n); return baseSlice(array, 0, n < 0 ? 0 : n);
} }
@@ -5096,9 +5090,7 @@
if (!length) { if (!length) {
return []; return [];
} }
if (guard ? isIterateeCall(array, n, guard) : n == null) { n = (guard || n == null) ? 1 : n;
n = 1;
}
n = length - (+n || 0); n = length - (+n || 0);
return baseSlice(array, n < 0 ? 0 : n); return baseSlice(array, n < 0 ? 0 : n);
} }
@@ -5265,7 +5257,7 @@
return []; return [];
} }
if (isSorted != null && typeof isSorted != 'boolean') { if (isSorted != null && typeof isSorted != 'boolean') {
iteratee = isIterateeCall(array, isSorted, iteratee) ? undefined : isSorted; iteratee = isSorted;
isSorted = false; isSorted = false;
} }
var toIteratee = getIteratee(); var toIteratee = getIteratee();
@@ -5823,9 +5815,7 @@
*/ */
function every(collection, predicate, guard) { function every(collection, predicate, guard) {
var func = isArray(collection) ? arrayEvery : baseEvery; var func = isArray(collection) ? arrayEvery : baseEvery;
if (guard && isIterateeCall(collection, predicate, guard)) { predicate = guard ? undefined : predicate;
predicate = undefined;
}
return func(collection, getIteratee(predicate)); return func(collection, getIteratee(predicate));
} }
@@ -6060,7 +6050,7 @@
function includes(collection, target, fromIndex, guard) { function includes(collection, target, fromIndex, guard) {
collection = isArrayLike(collection) ? collection : values(collection); collection = isArrayLike(collection) ? collection : values(collection);
var length = collection.length; var length = collection.length;
if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) { if (guard || typeof fromIndex != 'number') {
fromIndex = 0; fromIndex = 0;
} else { } else {
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0); fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
@@ -6145,10 +6135,10 @@
* `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`. * `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
* *
* The guarded methods are: * The guarded methods are:
* `ary`, `callback`, `create`, `curry`, `curryRight`, `drop`, `dropRight`, * `ary`, `callback`, `curry`, `curryRight`, `drop`, `dropRight`, `every`,
* `every`, `fill`, `invert`, `parseInt`, `random`, `range`, `sample`, * `fill`, `invert`, `parseInt`, `random`, `range`, `sample`, `slice`, `some`,
* `slice`, `some`, `sortBy`, `sumBy`, `take`, `takeRight`, `template`, * `sortBy`, `take`, `takeRight`, `template`, `trim`, `trimLeft`, `trimRight`,
* `trim`, `trimLeft`, `trimRight`, `trunc`, `uniqBy`, and `words` * `uniq`, and `words`
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -6355,7 +6345,7 @@
* // => [3, 1] * // => [3, 1]
*/ */
function sample(collection, n, guard) { function sample(collection, n, guard) {
if (guard ? isIterateeCall(collection, n, guard) : n == null) { if (guard || n == null) {
collection = isArrayLike(collection) ? collection : values(collection); collection = isArrayLike(collection) ? collection : values(collection);
var length = collection.length; var length = collection.length;
return length > 0 ? collection[baseRandom(0, length - 1)] : undefined; return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
@@ -6458,9 +6448,7 @@
*/ */
function some(collection, predicate, guard) { function some(collection, predicate, guard) {
var func = isArray(collection) ? arraySome : baseSome; var func = isArray(collection) ? arraySome : baseSome;
if (guard && isIterateeCall(collection, predicate, guard)) { predicate = guard ? undefined : predicate;
predicate = undefined;
}
return func(collection, getIteratee(predicate)); return func(collection, getIteratee(predicate));
} }
@@ -6546,12 +6534,10 @@
if (collection == null) { if (collection == null) {
return []; return [];
} }
if (guard && isIterateeCall(iteratees, orders, guard)) {
orders = undefined;
}
if (!isArray(iteratees)) { if (!isArray(iteratees)) {
iteratees = iteratees == null ? [] : [iteratees]; iteratees = iteratees == null ? [] : [iteratees];
} }
orders = guard ? undefined : orders;
if (!isArray(orders)) { if (!isArray(orders)) {
orders = orders == null ? [] : [orders]; orders = orders == null ? [] : [orders];
} }
@@ -6638,9 +6624,7 @@
* // => [6, 8, 10] * // => [6, 8, 10]
*/ */
function ary(func, n, guard) { function ary(func, n, guard) {
if (guard && isIterateeCall(func, n, guard)) { n = guard ? undefined : n;
n = undefined;
}
n = (func && n == null) ? func.length : nativeMax(+n || 0, 0); n = (func && n == null) ? func.length : nativeMax(+n || 0, 0);
return createWrapper(func, ARY_FLAG, undefined, undefined, undefined, undefined, n); return createWrapper(func, ARY_FLAG, undefined, undefined, undefined, undefined, n);
} }
@@ -6860,9 +6844,7 @@
* // => [1, 2, 3] * // => [1, 2, 3]
*/ */
function curry(func, arity, guard) { function curry(func, arity, guard) {
if (guard && isIterateeCall(func, arity, guard)) { arity = guard ? undefined : arity;
arity = undefined;
}
var result = createWrapper(func, CURRY_FLAG, undefined, undefined, undefined, undefined, undefined, arity); var result = createWrapper(func, CURRY_FLAG, undefined, undefined, undefined, undefined, undefined, arity);
result.placeholder = curry.placeholder; result.placeholder = curry.placeholder;
return result; return result;
@@ -6906,9 +6888,7 @@
* // => [1, 2, 3] * // => [1, 2, 3]
*/ */
function curryRight(func, arity, guard) { function curryRight(func, arity, guard) {
if (guard && isIterateeCall(func, arity, guard)) { arity = guard ? undefined : arity;
arity = undefined;
}
var result = createWrapper(func, CURRY_RIGHT_FLAG, undefined, undefined, undefined, undefined, undefined, arity); var result = createWrapper(func, CURRY_RIGHT_FLAG, undefined, undefined, undefined, undefined, undefined, arity);
result.placeholder = curryRight.placeholder; result.placeholder = curryRight.placeholder;
return result; return result;
@@ -8577,7 +8557,6 @@
* @category Object * @category Object
* @param {Object} prototype The object to inherit from. * @param {Object} prototype The object to inherit from.
* @param {Object} [properties] The properties to assign to the object. * @param {Object} [properties] The properties to assign to the object.
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
* @returns {Object} Returns the new object. * @returns {Object} Returns the new object.
* @example * @example
* *
@@ -8601,11 +8580,8 @@
* circle instanceof Shape; * circle instanceof Shape;
* // => true * // => true
*/ */
function create(prototype, properties, guard) { function create(prototype, properties) {
var result = baseCreate(prototype); var result = baseCreate(prototype);
if (guard && isIterateeCall(prototype, properties, guard)) {
properties = undefined;
}
return properties ? baseAssign(result, properties) : result; return properties ? baseAssign(result, properties) : result;
} }
@@ -8985,7 +8961,6 @@
* @category Object * @category Object
* @param {Object} object The object to invert. * @param {Object} object The object to invert.
* @param {boolean} [multiValue] Allow multiple values per key. * @param {boolean} [multiValue] Allow multiple values per key.
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
* @returns {Object} Returns the new inverted object. * @returns {Object} Returns the new inverted object.
* @example * @example
* *
@@ -8998,10 +8973,9 @@
* _.invert(object, true); * _.invert(object, true);
* // => { '1': ['a', 'c'], '2': ['b'] } * // => { '1': ['a', 'c'], '2': ['b'] }
*/ */
function invert(object, multiValue, guard) { function invert(object, multiValue) {
if (guard && isIterateeCall(object, multiValue, guard)) { multiValue = typeof multiValue == 'boolean' && multiValue;
multiValue = undefined;
}
var index = -1, var index = -1,
props = keys(object), props = keys(object),
length = props.length, length = props.length,
@@ -9979,7 +9953,7 @@
function parseInt(string, radix, guard) { function parseInt(string, radix, guard) {
// Chrome fails to trim leading <BOM> whitespace characters. // Chrome fails to trim leading <BOM> whitespace characters.
// See https://code.google.com/p/v8/issues/detail?id=3109 for more details. // See https://code.google.com/p/v8/issues/detail?id=3109 for more details.
if (guard ? isIterateeCall(string, radix, guard) : radix == null) { if (guard || radix == null) {
radix = 0; radix = 0;
} else if (radix) { } else if (radix) {
radix = +radix; radix = +radix;
@@ -10333,7 +10307,7 @@
if (!string) { if (!string) {
return string; return string;
} }
if (guard ? isIterateeCall(value, chars, guard) : chars == null) { if (guard || chars == null) {
return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1); return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
} }
chars = (chars + ''); chars = (chars + '');
@@ -10364,7 +10338,7 @@
if (!string) { if (!string) {
return string; return string;
} }
if (guard ? isIterateeCall(value, chars, guard) : chars == null) { if (guard || chars == null) {
return string.slice(trimmedLeftIndex(string)); return string.slice(trimmedLeftIndex(string));
} }
return string.slice(charsLeftIndex(string, (chars + ''))); return string.slice(charsLeftIndex(string, (chars + '')));
@@ -10394,7 +10368,7 @@
if (!string) { if (!string) {
return string; return string;
} }
if (guard ? isIterateeCall(value, chars, guard) : chars == null) { if (guard || chars == null) {
return string.slice(0, trimmedRightIndex(string) + 1); return string.slice(0, trimmedRightIndex(string) + 1);
} }
return string.slice(0, charsRightIndex(string, (chars + '')) + 1); return string.slice(0, charsRightIndex(string, (chars + '')) + 1);
@@ -10525,10 +10499,8 @@
* // => ['fred', 'barney', '&', 'pebbles'] * // => ['fred', 'barney', '&', 'pebbles']
*/ */
function words(string, pattern, guard) { function words(string, pattern, guard) {
if (guard && isIterateeCall(string, pattern, guard)) {
pattern = undefined;
}
string = baseToString(string); string = baseToString(string);
pattern = guard ? undefined : guard;
return string.match(pattern || reWords) || []; return string.match(pattern || reWords) || [];
} }
@@ -11155,10 +11127,7 @@
* _.maxBy(users, 'age'); * _.maxBy(users, 'age');
* // => { 'user': 'fred', 'age': 40 } * // => { 'user': 'fred', 'age': 40 }
*/ */
function maxBy(array, iteratee, guard) { function maxBy(array, iteratee) {
if (guard && isIterateeCall(array, iteratee, guard)) {
iteratee = undefined;
}
return (array && array.length) return (array && array.length)
? arrayExtremum(array, getIteratee(iteratee), gt, NEGATIVE_INFINITY) ? arrayExtremum(array, getIteratee(iteratee), gt, NEGATIVE_INFINITY)
: NEGATIVE_INFINITY; : NEGATIVE_INFINITY;
@@ -11212,10 +11181,7 @@
* _.minBy(users, 'age'); * _.minBy(users, 'age');
* // => { 'user': 'barney', 'age': 36 } * // => { 'user': 'barney', 'age': 36 }
*/ */
function minBy(array, iteratee, guard) { function minBy(array, iteratee) {
if (guard && isIterateeCall(array, iteratee, guard)) {
iteratee = undefined;
}
return (array && array.length) return (array && array.length)
? arrayExtremum(array, getIteratee(iteratee), lt, POSITIVE_INFINITY) ? arrayExtremum(array, getIteratee(iteratee), lt, POSITIVE_INFINITY)
: POSITIVE_INFINITY; : POSITIVE_INFINITY;
@@ -11273,7 +11239,6 @@
* @category Math * @category Math
* @param {Array} array The array to iterate over. * @param {Array} array The array to iterate over.
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration. * @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
* @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
* @returns {number} Returns the sum. * @returns {number} Returns the sum.
* @example * @example
* *
@@ -11289,14 +11254,10 @@
* _.sum(objects, 'n'); * _.sum(objects, 'n');
* // => 10 * // => 10
*/ */
function sumBy(array, iteratee, guard) { function sumBy(array, iteratee) {
if (!(array && array.length)) { return (array && array.length)
return 0; ? arraySum(array, getIteratee(iteratee))
} : 0;
if (guard && isIterateeCall(array, iteratee, guard)) {
iteratee = undefined;
}
return arraySum(array, getIteratee(iteratee));
} }
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/

View File

@@ -4972,7 +4972,8 @@
var array = [1, 2, 3], var array = [1, 2, 3],
func = _[methodName], func = _[methodName],
isFind = /^find/.test(methodName), isFind = /^find/.test(methodName),
isSome = methodName == 'some'; isSome = methodName == 'some',
isReduce = /^reduce/.test(methodName);
test('`_.' + methodName + '` should ignore changes to `array.length`', 1, function() { test('`_.' + methodName + '` should ignore changes to `array.length`', 1, function() {
if (func) { if (func) {
@@ -4984,7 +4985,7 @@
array.push(2); array.push(2);
} }
return !(isFind || isSome); return !(isFind || isSome);
}, array); }, isReduce ? array : null);
strictEqual(count, 1); strictEqual(count, 1);
} }
@@ -4997,7 +4998,8 @@
_.each(_.difference(_.union(methods, collectionMethods), arrayMethods), function(methodName) { _.each(_.difference(_.union(methods, collectionMethods), arrayMethods), function(methodName) {
var func = _[methodName], var func = _[methodName],
isFind = /^find/.test(methodName), isFind = /^find/.test(methodName),
isSome = methodName == 'some'; isSome = methodName == 'some',
isReduce = /^reduce/.test(methodName);
test('`_.' + methodName + '` should ignore added `object` properties', 1, function() { test('`_.' + methodName + '` should ignore added `object` properties', 1, function() {
if (func) { if (func) {
@@ -5009,7 +5011,7 @@
object.b = 2; object.b = 2;
} }
return !(isFind || isSome); return !(isFind || isSome);
}, object); }, isReduce ? object : null);
strictEqual(count, 1); strictEqual(count, 1);
} }
@@ -10645,13 +10647,6 @@
strictEqual(func(array), isMax ? 499999 : 0); strictEqual(func(array), isMax ? 499999 : 0);
}); });
test('`_.' + methodName + '` should work as an iteratee for methods like `_.map`', 1, function() {
var arrays = [[2, 1], [5, 4], [7, 8]],
expected = isMax ? [2, 5, 8] : [1, 4, 7];
deepEqual(_.map(arrays, func), expected);
});
test('`_.' + methodName + '` should work when chaining on an array with only one value', 1, function() { test('`_.' + methodName + '` should work when chaining on an array with only one value', 1, function() {
if (!isNpm) { if (!isNpm) {
var actual = _([40])[methodName](); var actual = _([40])[methodName]();
@@ -14346,11 +14341,6 @@
strictEqual(_.sumBy(arrays, 0), 6); strictEqual(_.sumBy(arrays, 0), 6);
strictEqual(_.sumBy(objects, 'a'), 6); strictEqual(_.sumBy(objects, 'a'), 6);
}); });
test('should perform basic sum when used as an iteratee for methods like `_.map`', 1, function() {
var actual = _.map([array, array], _.sumBy);
deepEqual(actual, [12, 12]);
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -15793,6 +15783,19 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('lodash.uniq');
(function() {
test('should perform an unsorted uniq when used as an iteratee for methods like `_.map`', 1, function() {
var array = [[2, 1, 2], [1, 2, 1]],
actual = _.map(array, _.uniq);
deepEqual(actual, [[2, 1], [1, 2]]);
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.uniqBy'); QUnit.module('lodash.uniqBy');
(function() { (function() {
@@ -15887,13 +15890,6 @@
deepEqual(func([1, 2, 3, 3, 3, 3, 3], true), expected); deepEqual(func([1, 2, 3, 3, 3, 3, 3], true), expected);
}); });
test('`_.' + methodName + '` should perform an unsorted uniq when used as an iteratee for methods like `_.map`', 1, function() {
var array = [[2, 1, 2], [1, 2, 1]],
actual = _.map(array, func);
deepEqual(actual, [[2, 1], [1, 2]]);
});
test('`_.' + methodName + '` should work with large arrays', 1, function() { test('`_.' + methodName + '` should work with large arrays', 1, function() {
var largeArray = [], var largeArray = [],
expected = [0, 'a', {}], expected = [0, 'a', {}],