1 var createAggregator = require('../internal/createAggregator');
4 * Creates an array of elements split into two groups, the first of which
5 * contains elements `predicate` returns truthy for, while the second of which
6 * contains elements `predicate` returns falsey for. The predicate is bound
7 * to `thisArg` and invoked with three arguments: (value, index|key, collection).
9 * If a property name is provided for `predicate` the created `_.property`
10 * style callback returns the property value of the given element.
12 * If a value is also provided for `thisArg` the created `_.matchesProperty`
13 * style callback returns `true` for elements that have a matching property
14 * value, else `false`.
16 * If an object is provided for `predicate` the created `_.matches` style
17 * callback returns `true` for elements that have the properties of the given
18 * object, else `false`.
22 * @category Collection
23 * @param {Array|Object|string} collection The collection to iterate over.
24 * @param {Function|Object|string} [predicate=_.identity] The function invoked
26 * @param {*} [thisArg] The `this` binding of `predicate`.
27 * @returns {Array} Returns the array of grouped elements.
30 * _.partition([1, 2, 3], function(n) {
35 * _.partition([1.2, 2.3, 3.4], function(n) {
36 * return this.floor(n) % 2;
38 * // => [[1.2, 3.4], [2.3]]
41 * { 'user': 'barney', 'age': 36, 'active': false },
42 * { 'user': 'fred', 'age': 40, 'active': true },
43 * { 'user': 'pebbles', 'age': 1, 'active': false }
46 * var mapper = function(array) {
47 * return _.pluck(array, 'user');
50 * // using the `_.matches` callback shorthand
51 * _.map(_.partition(users, { 'age': 1, 'active': false }), mapper);
52 * // => [['pebbles'], ['barney', 'fred']]
54 * // using the `_.matchesProperty` callback shorthand
55 * _.map(_.partition(users, 'active', false), mapper);
56 * // => [['barney', 'pebbles'], ['fred']]
58 * // using the `_.property` callback shorthand
59 * _.map(_.partition(users, 'active'), mapper);
60 * // => [['fred'], ['barney', 'pebbles']]
62 var partition = createAggregator(function(result, value, key) {
63 result[key ? 0 : 1].push(value);
64 }, function() { return [[], []]; });
66 module.exports = partition;