From 69da13b26107a81da0700b0a2de7ad40f33eaeba Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 16 Mar 2014 13:36:18 -0700 Subject: [PATCH] Simplify `_.find` by having it use `_.findIndex`. --- lodash.js | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lodash.js b/lodash.js index 17a4cf659..6346dbd82 100644 --- a/lodash.js +++ b/lodash.js @@ -1392,7 +1392,8 @@ /** * The base implementation of `find`, 'findLast`, `findKey`, and `findLastKey` - * without support for callback shorthands or `this` binding. + * without support for callback shorthands or `this` binding which iterates + * over `collection` using the provided `eachFunc`. * * @private * @param {Array|Object|string} collection The collection to search. @@ -3821,20 +3822,12 @@ * // => { 'name': 'fred', 'age': 40, 'blocked': true } */ function find(collection, predicate, thisArg) { - predicate = lodash.createCallback(predicate, thisArg, 3); if (isArray(collection)) { - var index = -1, - length = collection.length; - - while (++index < length) { - var value = collection[index]; - if (predicate(value, index, collection)) { - return value; - } - } - } else { - return baseFind(collection, predicate, baseEach); + var index = findIndex(collection, predicate, thisArg); + return index > -1 ? collection[index] : undefined; } + predicate = lodash.createCallback(predicate, thisArg, 3); + return baseFind(collection, predicate, baseEach); } /**