Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / collection / filter.js
1 var arrayFilter = require('../internal/arrayFilter'),
2     baseCallback = require('../internal/baseCallback'),
3     baseFilter = require('../internal/baseFilter'),
4     isArray = require('../lang/isArray');
5
6 /**
7  * Iterates over elements of `collection`, returning an array of all elements
8  * `predicate` returns truthy for. The predicate is bound to `thisArg` and
9  * invoked with three arguments: (value, index|key, collection).
10  *
11  * If a property name is provided for `predicate` the created `_.property`
12  * style callback returns the property value of the given element.
13  *
14  * If a value is also provided for `thisArg` the created `_.matchesProperty`
15  * style callback returns `true` for elements that have a matching property
16  * value, else `false`.
17  *
18  * If an object is provided for `predicate` the created `_.matches` style
19  * callback returns `true` for elements that have the properties of the given
20  * object, else `false`.
21  *
22  * @static
23  * @memberOf _
24  * @alias select
25  * @category Collection
26  * @param {Array|Object|string} collection The collection to iterate over.
27  * @param {Function|Object|string} [predicate=_.identity] The function invoked
28  *  per iteration.
29  * @param {*} [thisArg] The `this` binding of `predicate`.
30  * @returns {Array} Returns the new filtered array.
31  * @example
32  *
33  * _.filter([4, 5, 6], function(n) {
34  *   return n % 2 == 0;
35  * });
36  * // => [4, 6]
37  *
38  * var users = [
39  *   { 'user': 'barney', 'age': 36, 'active': true },
40  *   { 'user': 'fred',   'age': 40, 'active': false }
41  * ];
42  *
43  * // using the `_.matches` callback shorthand
44  * _.pluck(_.filter(users, { 'age': 36, 'active': true }), 'user');
45  * // => ['barney']
46  *
47  * // using the `_.matchesProperty` callback shorthand
48  * _.pluck(_.filter(users, 'active', false), 'user');
49  * // => ['fred']
50  *
51  * // using the `_.property` callback shorthand
52  * _.pluck(_.filter(users, 'active'), 'user');
53  * // => ['barney']
54  */
55 function filter(collection, predicate, thisArg) {
56   var func = isArray(collection) ? arrayFilter : baseFilter;
57   predicate = baseCallback(predicate, thisArg, 3);
58   return func(collection, predicate);
59 }
60
61 module.exports = filter;