lodash-es
Version:
The modern build of lodash exported as ES modules.
26 lines (23 loc) • 829 B
JavaScript
import baseCallback from './baseCallback';
import baseFind from './baseFind';
import baseFindIndex from './baseFindIndex';
import isArray from '../lang/isArray';
/**
* Creates a `_.find` or `_.findLast` function.
*
* @private
* @param {Function} eachFunc The function to iterate over a collection.
* @param {boolean} [fromRight] Specify iterating from right to left.
* @returns {Function} Returns the new find function.
*/
function createFind(eachFunc, fromRight) {
return function(collection, predicate, thisArg) {
predicate = baseCallback(predicate, thisArg, 3);
if (isArray(collection)) {
var index = baseFindIndex(collection, predicate, fromRight);
return index > -1 ? collection[index] : undefined;
}
return baseFind(collection, predicate, eachFunc);
};
}
export default createFind;