From d110b64ee4af316c357a56cf283b7f02ec3d4973 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 23 May 2016 08:10:01 -0700 Subject: [PATCH] Ensure `_.find` and `_.findLast` pass the correct key param to `predicate`. [closes #2367] --- lodash.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/lodash.js b/lodash.js index 57ac04642..636564928 100644 --- a/lodash.js +++ b/lodash.js @@ -4549,6 +4549,31 @@ return wrapper; } + /** + * Creates a `_.find` or `_.findLast` function. + * + * @private + * @param {Function} findIndexFunc The function to find the collection index. + * @returns {Function} Returns the new find function. + */ + function createFind(findIndexFunc) { + return function(collection, predicate, fromIndex) { + var iterable = Object(collection); + predicate = getIteratee(predicate, 3); + if (!isArrayLike(collection)) { + var props = keys(collection); + } + var index = findIndexFunc(props || collection, function(value, key) { + if (props) { + key = value; + value = iterable[key]; + } + return predicate(value, key, iterable); + }, fromIndex); + return index > -1 ? collection[props ? props[index] : index] : undefined; + }; + } + /** * Creates a `_.flow` or `_.flowRight` function. * @@ -8472,11 +8497,7 @@ * _.find(users, 'active'); * // => object for 'barney' */ - function find(collection, predicate, fromIndex) { - collection = isArrayLike(collection) ? collection : values(collection); - var index = findIndex(collection, predicate, fromIndex); - return index > -1 ? collection[index] : undefined; - } + var find = createFind(findIndex); /** * This method is like `_.find` except that it iterates over elements of @@ -8498,11 +8519,7 @@ * }); * // => 3 */ - function findLast(collection, predicate, fromIndex) { - collection = isArrayLike(collection) ? collection : values(collection); - var index = findLastIndex(collection, predicate, fromIndex); - return index > -1 ? collection[index] : undefined; - } + var findLast = createFind(findLastIndex); /** * Creates a flattened array of values by running each element in `collection`