-
Notifications
You must be signed in to change notification settings - Fork 7k
/
_createFind.js
27 lines (24 loc) · 958 Bytes
/
_createFind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
define(['./_baseIteratee', './isArrayLike', './keys'], function(baseIteratee, isArrayLike, keys) {
/** Used as a safe reference for `undefined` in pre-ES5 environments. */
var undefined;
/**
* 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);
if (!isArrayLike(collection)) {
var iteratee = baseIteratee(predicate, 3);
collection = keys(collection);
predicate = function(key) { return iteratee(iterable[key], key, iterable); };
}
var index = findIndexFunc(collection, predicate, fromIndex);
return index > -1 ? iterable[iteratee ? collection[index] : index] : undefined;
};
}
return createFind;
});