Add fromIndex param to _.find and _.findLast.

This commit is contained in:
John-David Dalton
2016-05-12 16:19:35 -07:00
parent 3d078383cb
commit 6d02a64c47

View File

@@ -687,23 +687,21 @@
} }
/** /**
* The base implementation of methods like `_.find` and `_.findKey`, without * The base implementation of methods like `_.findKey` and `_.findLastKey`,
* support for iteratee shorthands, which iterates over `collection` using * without support for iteratee shorthands, which iterates over `collection`
* `eachFunc`. * using `eachFunc`.
* *
* @private * @private
* @param {Array|Object} collection The collection to search. * @param {Array|Object} collection The collection to search.
* @param {Function} predicate The function invoked per iteration. * @param {Function} predicate The function invoked per iteration.
* @param {Function} eachFunc The function to iterate over `collection`. * @param {Function} eachFunc The function to iterate over `collection`.
* @param {boolean} [retKey] Specify returning the key of the found element
* instead of the element itself.
* @returns {*} Returns the found element or its key, else `undefined`. * @returns {*} Returns the found element or its key, else `undefined`.
*/ */
function baseFind(collection, predicate, eachFunc, retKey) { function baseFindKey(collection, predicate, eachFunc) {
var result; var result;
eachFunc(collection, function(value, key, collection) { eachFunc(collection, function(value, key, collection) {
if (predicate(value, key, collection)) { if (predicate(value, key, collection)) {
result = retKey ? key : value; result = key;
return false; return false;
} }
}); });
@@ -8405,6 +8403,7 @@
* @param {Array|Object} collection The collection to search. * @param {Array|Object} collection The collection to search.
* @param {Array|Function|Object|string} [predicate=_.identity] * @param {Array|Function|Object|string} [predicate=_.identity]
* The function invoked per iteration. * The function invoked per iteration.
* @param {number} [fromIndex=0] The index to search from.
* @returns {*} Returns the matched element, else `undefined`. * @returns {*} Returns the matched element, else `undefined`.
* @example * @example
* *
@@ -8429,13 +8428,10 @@
* _.find(users, 'active'); * _.find(users, 'active');
* // => object for 'barney' * // => object for 'barney'
*/ */
function find(collection, predicate) { function find(collection, predicate, fromIndex) {
predicate = getIteratee(predicate, 3); collection = isArrayLike(collection) ? collection : values(collection);
if (isArray(collection)) { var index = findIndex(collection, predicate, fromIndex);
var index = baseFindIndex(collection, predicate, 0); return index > -1 ? collection[index] : undefined;
return index > -1 ? collection[index] : undefined;
}
return baseFind(collection, predicate, baseEach);
} }
/** /**
@@ -8449,6 +8445,7 @@
* @param {Array|Object} collection The collection to search. * @param {Array|Object} collection The collection to search.
* @param {Array|Function|Object|string} [predicate=_.identity] * @param {Array|Function|Object|string} [predicate=_.identity]
* The function invoked per iteration. * The function invoked per iteration.
* @param {number} [fromIndex=collection.length-1] The index to search from.
* @returns {*} Returns the matched element, else `undefined`. * @returns {*} Returns the matched element, else `undefined`.
* @example * @example
* *
@@ -8457,13 +8454,10 @@
* }); * });
* // => 3 * // => 3
*/ */
function findLast(collection, predicate) { function findLast(collection, predicate, fromIndex) {
predicate = getIteratee(predicate, 3); collection = isArrayLike(collection) ? collection : values(collection);
if (isArray(collection)) { var index = findLastIndex(collection, predicate, fromIndex);
var index = baseFindIndex(collection, predicate, collection.length - 1, true); return index > -1 ? collection[index] : undefined;
return index > -1 ? collection[index] : undefined;
}
return baseFind(collection, predicate, baseEachRight);
} }
/** /**
@@ -12131,7 +12125,7 @@
* // => 'barney' * // => 'barney'
*/ */
function findKey(object, predicate) { function findKey(object, predicate) {
return baseFind(object, getIteratee(predicate, 3), baseForOwn, true); return baseFindKey(object, getIteratee(predicate, 3), baseForOwn);
} }
/** /**
@@ -12171,7 +12165,7 @@
* // => 'pebbles' * // => 'pebbles'
*/ */
function findLastKey(object, predicate) { function findLastKey(object, predicate) {
return baseFind(object, getIteratee(predicate, 3), baseForOwnRight, true); return baseFindKey(object, getIteratee(predicate, 3), baseForOwnRight);
} }
/** /**