Version 1
[yaffs-website] / node_modules / uncss / node_modules / lodash / find.js
1 var baseEach = require('./internal/baseEach'),
2     baseFind = require('./internal/baseFind'),
3     baseFindIndex = require('./internal/baseFindIndex'),
4     baseIteratee = require('./internal/baseIteratee'),
5     isArray = require('./isArray');
6
7 /**
8  * Iterates over elements of `collection`, returning the first element
9  * `predicate` returns truthy for. The predicate is invoked with three arguments:
10  * (value, index|key, collection).
11  *
12  * @static
13  * @memberOf _
14  * @category Collection
15  * @param {Array|Object} collection The collection to search.
16  * @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.
17  * @returns {*} Returns the matched element, else `undefined`.
18  * @example
19  *
20  * var users = [
21  *   { 'user': 'barney',  'age': 36, 'active': true },
22  *   { 'user': 'fred',    'age': 40, 'active': false },
23  *   { 'user': 'pebbles', 'age': 1,  'active': true }
24  * ];
25  *
26  * _.find(users, function(o) { return o.age < 40; });
27  * // => object for 'barney'
28  *
29  * // using the `_.matches` iteratee shorthand
30  * _.find(users, { 'age': 1, 'active': true });
31  * // => object for 'pebbles'
32  *
33  * // using the `_.matchesProperty` iteratee shorthand
34  * _.find(users, ['active', false]);
35  * // => object for 'fred'
36  *
37  * // using the `_.property` iteratee shorthand
38  * _.find(users, 'active');
39  * // => object for 'barney'
40  */
41 function find(collection, predicate) {
42   predicate = baseIteratee(predicate, 3);
43   if (isArray(collection)) {
44     var index = baseFindIndex(collection, predicate);
45     return index > -1 ? collection[index] : undefined;
46   }
47   return baseFind(collection, predicate, baseEach);
48 }
49
50 module.exports = find;