Ensure _.find and _.findLast pass the correct key param to predicate. [closes #2367]

This commit is contained in:
John-David Dalton
2016-05-23 08:10:01 -07:00
parent a2617d810c
commit d110b64ee4

View File

@@ -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`