1 var arrayFilter = require('../internal/arrayFilter'),
2 baseCallback = require('../internal/baseCallback'),
3 baseFilter = require('../internal/baseFilter'),
4 isArray = require('../lang/isArray');
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).
11 * If a property name is provided for `predicate` the created `_.property`
12 * style callback returns the property value of the given element.
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`.
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`.
25 * @category Collection
26 * @param {Array|Object|string} collection The collection to iterate over.
27 * @param {Function|Object|string} [predicate=_.identity] The function invoked
29 * @param {*} [thisArg] The `this` binding of `predicate`.
30 * @returns {Array} Returns the new filtered array.
33 * _.filter([4, 5, 6], function(n) {
39 * { 'user': 'barney', 'age': 36, 'active': true },
40 * { 'user': 'fred', 'age': 40, 'active': false }
43 * // using the `_.matches` callback shorthand
44 * _.pluck(_.filter(users, { 'age': 36, 'active': true }), 'user');
47 * // using the `_.matchesProperty` callback shorthand
48 * _.pluck(_.filter(users, 'active', false), 'user');
51 * // using the `_.property` callback shorthand
52 * _.pluck(_.filter(users, 'active'), 'user');
55 function filter(collection, predicate, thisArg) {
56 var func = isArray(collection) ? arrayFilter : baseFilter;
57 predicate = baseCallback(predicate, thisArg, 3);
58 return func(collection, predicate);
61 module.exports = filter;