-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathfind.js
50 lines (47 loc) · 1.74 KB
/
find.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
define(['./internal/baseEach', './internal/baseFind', './internal/baseFindIndex', './internal/baseIteratee', './isArray'], function(baseEach, baseFind, baseFindIndex, baseIteratee, isArray) {
/** Used as a safe reference for `undefined` in pre-ES5 environments. */
var undefined;
/**
* Iterates over elements of `collection`, returning the first element
* `predicate` returns truthy for. The predicate is invoked with three arguments:
* (value, index|key, collection).
*
* @static
* @memberOf _
* @category Collection
* @param {Array|Object} collection The collection to search.
* @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.
* @returns {*} Returns the matched element, else `undefined`.
* @example
*
* var users = [
* { 'user': 'barney', 'age': 36, 'active': true },
* { 'user': 'fred', 'age': 40, 'active': false },
* { 'user': 'pebbles', 'age': 1, 'active': true }
* ];
*
* _.find(users, function(o) { return o.age < 40; });
* // => object for 'barney'
*
* // using the `_.matches` iteratee shorthand
* _.find(users, { 'age': 1, 'active': true });
* // => object for 'pebbles'
*
* // using the `_.matchesProperty` iteratee shorthand
* _.find(users, ['active', false]);
* // => object for 'fred'
*
* // using the `_.property` iteratee shorthand
* _.find(users, 'active');
* // => object for 'barney'
*/
function find(collection, predicate) {
predicate = baseIteratee(predicate, 3);
if (isArray(collection)) {
var index = baseFindIndex(collection, predicate);
return index > -1 ? collection[index] : undefined;
}
return baseFind(collection, predicate, baseEach);
}
return find;
});