Skip to content

Commit

Permalink
Remove unneeded isIterateeCall checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Jul 29, 2015
1 parent ccb90f4 commit b17f6ee
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 94 deletions.
103 changes: 32 additions & 71 deletions lodash.js
Expand Up @@ -4293,9 +4293,7 @@
if (!(array && array.length)) {
return [];
}
if (guard ? isIterateeCall(array, n, guard) : n == null) {
n = 1;
}
n = (guard || n == null) ? 1 : n;
return baseSlice(array, n < 0 ? 0 : n);
}

Expand Down Expand Up @@ -4328,9 +4326,7 @@
if (!length) {
return [];
}
if (guard ? isIterateeCall(array, n, guard) : n == null) {
n = 1;
}
n = (guard || n == null) ? 1 : n;
n = length - (+n || 0);
return baseSlice(array, 0, n < 0 ? 0 : n);
}
Expand Down Expand Up @@ -5061,9 +5057,7 @@
if (!(array && array.length)) {
return [];
}
if (guard ? isIterateeCall(array, n, guard) : n == null) {
n = 1;
}
n = (guard || n == null) ? 1 : n;
return baseSlice(array, 0, n < 0 ? 0 : n);
}

Expand Down Expand Up @@ -5096,9 +5090,7 @@
if (!length) {
return [];
}
if (guard ? isIterateeCall(array, n, guard) : n == null) {
n = 1;
}
n = (guard || n == null) ? 1 : n;
n = length - (+n || 0);
return baseSlice(array, n < 0 ? 0 : n);
}
Expand Down Expand Up @@ -5265,7 +5257,7 @@
return [];
}
if (isSorted != null && typeof isSorted != 'boolean') {
iteratee = isIterateeCall(array, isSorted, iteratee) ? undefined : isSorted;
iteratee = isSorted;
isSorted = false;
}
var toIteratee = getIteratee();
Expand Down Expand Up @@ -5823,9 +5815,7 @@
*/
function every(collection, predicate, guard) {
var func = isArray(collection) ? arrayEvery : baseEvery;
if (guard && isIterateeCall(collection, predicate, guard)) {
predicate = undefined;
}
predicate = guard ? undefined : predicate;
return func(collection, getIteratee(predicate));
}

Expand Down Expand Up @@ -6060,7 +6050,7 @@
function includes(collection, target, fromIndex, guard) {
collection = isArrayLike(collection) ? collection : values(collection);
var length = collection.length;
if (typeof fromIndex != 'number' || (guard && isIterateeCall(target, fromIndex, guard))) {
if (guard || typeof fromIndex != 'number') {
fromIndex = 0;
} else {
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
Expand Down Expand Up @@ -6145,10 +6135,10 @@
* `_.every`, `_.filter`, `_.map`, `_.mapValues`, `_.reject`, and `_.some`.
*
* The guarded methods are:
* `ary`, `callback`, `create`, `curry`, `curryRight`, `drop`, `dropRight`,
* `every`, `fill`, `invert`, `parseInt`, `random`, `range`, `sample`,
* `slice`, `some`, `sortBy`, `sumBy`, `take`, `takeRight`, `template`,
* `trim`, `trimLeft`, `trimRight`, `trunc`, `uniqBy`, and `words`
* `ary`, `callback`, `curry`, `curryRight`, `drop`, `dropRight`, `every`,
* `fill`, `invert`, `parseInt`, `random`, `range`, `sample`, `slice`, `some`,
* `sortBy`, `take`, `takeRight`, `template`, `trim`, `trimLeft`, `trimRight`,
* `uniq`, and `words`
*
* @static
* @memberOf _
Expand Down Expand Up @@ -6355,7 +6345,7 @@
* // => [3, 1]
*/
function sample(collection, n, guard) {
if (guard ? isIterateeCall(collection, n, guard) : n == null) {
if (guard || n == null) {
collection = isArrayLike(collection) ? collection : values(collection);
var length = collection.length;
return length > 0 ? collection[baseRandom(0, length - 1)] : undefined;
Expand Down Expand Up @@ -6458,9 +6448,7 @@
*/
function some(collection, predicate, guard) {
var func = isArray(collection) ? arraySome : baseSome;
if (guard && isIterateeCall(collection, predicate, guard)) {
predicate = undefined;
}
predicate = guard ? undefined : predicate;
return func(collection, getIteratee(predicate));
}

Expand Down Expand Up @@ -6546,12 +6534,10 @@
if (collection == null) {
return [];
}
if (guard && isIterateeCall(iteratees, orders, guard)) {
orders = undefined;
}
if (!isArray(iteratees)) {
iteratees = iteratees == null ? [] : [iteratees];
}
orders = guard ? undefined : orders;
if (!isArray(orders)) {
orders = orders == null ? [] : [orders];
}
Expand Down Expand Up @@ -6638,9 +6624,7 @@
* // => [6, 8, 10]
*/
function ary(func, n, guard) {
if (guard && isIterateeCall(func, n, guard)) {
n = undefined;
}
n = guard ? undefined : n;
n = (func && n == null) ? func.length : nativeMax(+n || 0, 0);
return createWrapper(func, ARY_FLAG, undefined, undefined, undefined, undefined, n);
}
Expand Down Expand Up @@ -6860,9 +6844,7 @@
* // => [1, 2, 3]
*/
function curry(func, arity, guard) {
if (guard && isIterateeCall(func, arity, guard)) {
arity = undefined;
}
arity = guard ? undefined : arity;
var result = createWrapper(func, CURRY_FLAG, undefined, undefined, undefined, undefined, undefined, arity);
result.placeholder = curry.placeholder;
return result;
Expand Down Expand Up @@ -6906,9 +6888,7 @@
* // => [1, 2, 3]
*/
function curryRight(func, arity, guard) {
if (guard && isIterateeCall(func, arity, guard)) {
arity = undefined;
}
arity = guard ? undefined : arity;
var result = createWrapper(func, CURRY_RIGHT_FLAG, undefined, undefined, undefined, undefined, undefined, arity);
result.placeholder = curryRight.placeholder;
return result;
Expand Down Expand Up @@ -8577,7 +8557,6 @@
* @category Object
* @param {Object} prototype The object to inherit from.
* @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.
* @example
*
Expand All @@ -8601,11 +8580,8 @@
* circle instanceof Shape;
* // => true
*/
function create(prototype, properties, guard) {
function create(prototype, properties) {
var result = baseCreate(prototype);
if (guard && isIterateeCall(prototype, properties, guard)) {
properties = undefined;
}
return properties ? baseAssign(result, properties) : result;
}

Expand Down Expand Up @@ -8985,7 +8961,6 @@
* @category Object
* @param {Object} object The object to invert.
* @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.
* @example
*
Expand All @@ -8998,10 +8973,9 @@
* _.invert(object, true);
* // => { '1': ['a', 'c'], '2': ['b'] }
*/
function invert(object, multiValue, guard) {
if (guard && isIterateeCall(object, multiValue, guard)) {
multiValue = undefined;
}
function invert(object, multiValue) {
multiValue = typeof multiValue == 'boolean' && multiValue;

var index = -1,
props = keys(object),
length = props.length,
Expand Down Expand Up @@ -9979,7 +9953,7 @@
function parseInt(string, radix, guard) {
// Chrome fails to trim leading <BOM> whitespace characters.
// 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;
} else if (radix) {
radix = +radix;
Expand Down Expand Up @@ -10333,7 +10307,7 @@
if (!string) {
return string;
}
if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
if (guard || chars == null) {
return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
}
chars = (chars + '');
Expand Down Expand Up @@ -10364,7 +10338,7 @@
if (!string) {
return string;
}
if (guard ? isIterateeCall(value, chars, guard) : chars == null) {
if (guard || chars == null) {
return string.slice(trimmedLeftIndex(string));
}
return string.slice(charsLeftIndex(string, (chars + '')));
Expand Down Expand Up @@ -10394,7 +10368,7 @@
if (!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, charsRightIndex(string, (chars + '')) + 1);
Expand Down Expand Up @@ -10525,10 +10499,8 @@
* // => ['fred', 'barney', '&', 'pebbles']
*/
function words(string, pattern, guard) {
if (guard && isIterateeCall(string, pattern, guard)) {
pattern = undefined;
}
string = baseToString(string);
pattern = guard ? undefined : guard;
return string.match(pattern || reWords) || [];
}

Expand Down Expand Up @@ -11155,10 +11127,7 @@
* _.maxBy(users, 'age');
* // => { 'user': 'fred', 'age': 40 }
*/
function maxBy(array, iteratee, guard) {
if (guard && isIterateeCall(array, iteratee, guard)) {
iteratee = undefined;
}
function maxBy(array, iteratee) {
return (array && array.length)
? arrayExtremum(array, getIteratee(iteratee), gt, NEGATIVE_INFINITY)
: NEGATIVE_INFINITY;
Expand Down Expand Up @@ -11212,10 +11181,7 @@
* _.minBy(users, 'age');
* // => { 'user': 'barney', 'age': 36 }
*/
function minBy(array, iteratee, guard) {
if (guard && isIterateeCall(array, iteratee, guard)) {
iteratee = undefined;
}
function minBy(array, iteratee) {
return (array && array.length)
? arrayExtremum(array, getIteratee(iteratee), lt, POSITIVE_INFINITY)
: POSITIVE_INFINITY;
Expand Down Expand Up @@ -11273,7 +11239,6 @@
* @category Math
* @param {Array} array The array to iterate over.
* @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.
* @example
*
Expand All @@ -11289,14 +11254,10 @@
* _.sum(objects, 'n');
* // => 10
*/
function sumBy(array, iteratee, guard) {
if (!(array && array.length)) {
return 0;
}
if (guard && isIterateeCall(array, iteratee, guard)) {
iteratee = undefined;
}
return arraySum(array, getIteratee(iteratee));
function sumBy(array, iteratee) {
return (array && array.length)
? arraySum(array, getIteratee(iteratee))
: 0;
}

/*------------------------------------------------------------------------*/
Expand Down
42 changes: 19 additions & 23 deletions test/test.js
Expand Up @@ -4972,7 +4972,8 @@
var array = [1, 2, 3],
func = _[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() {
if (func) {
Expand All @@ -4984,7 +4985,7 @@
array.push(2);
}
return !(isFind || isSome);
}, array);
}, isReduce ? array : null);

strictEqual(count, 1);
}
Expand All @@ -4997,7 +4998,8 @@
_.each(_.difference(_.union(methods, collectionMethods), arrayMethods), function(methodName) {
var func = _[methodName],
isFind = /^find/.test(methodName),
isSome = methodName == 'some';
isSome = methodName == 'some',
isReduce = /^reduce/.test(methodName);

test('`_.' + methodName + '` should ignore added `object` properties', 1, function() {
if (func) {
Expand All @@ -5009,7 +5011,7 @@
object.b = 2;
}
return !(isFind || isSome);
}, object);
}, isReduce ? object : null);

strictEqual(count, 1);
}
Expand Down Expand Up @@ -10645,13 +10647,6 @@
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() {
if (!isNpm) {
var actual = _([40])[methodName]();
Expand Down Expand Up @@ -14346,11 +14341,6 @@
strictEqual(_.sumBy(arrays, 0), 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]);
});
}());

/*--------------------------------------------------------------------------*/
Expand Down Expand Up @@ -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');

(function() {
Expand Down Expand Up @@ -15887,13 +15890,6 @@
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() {
var largeArray = [],
expected = [0, 'a', {}],
Expand Down

2 comments on commit b17f6ee

@megawac
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdalton
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't removed its use all-out. I'm just being more picky about it.

Please sign in to comment.