mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
31 lines
908 B
JavaScript
31 lines
908 B
JavaScript
import baseIteratee from './_baseIteratee.js';
|
|
import isArrayLike from './isArrayLike.js';
|
|
import keys from './keys.js';
|
|
|
|
/**
|
|
* 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 = baseIteratee(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;
|
|
};
|
|
}
|
|
|
|
export default createFind;
|