Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var createAggregator = require('../internal/createAggregator');
2
3
/**
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).
8
*
9
* If a property name is provided for `predicate` the created `_.property`
10
* style callback returns the property value of the given element.
11
*
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`.
15
*
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`.
19
*
20
* @static
21
* @memberOf _
22
* @category Collection
23
* @param {Array|Object|string} collection The collection to iterate over.
24
* @param {Function|Object|string} [predicate=_.identity] The function invoked
25
* per iteration.
26
* @param {*} [thisArg] The `this` binding of `predicate`.
27
* @returns {Array} Returns the array of grouped elements.
28
* @example
29
*
30
* _.partition([1, 2, 3], function(n) {
31
* return n % 2;
32
* });
33
* // => [[1, 3], [2]]
34
*
35
* _.partition([1.2, 2.3, 3.4], function(n) {
36
* return this.floor(n) % 2;
37
* }, Math);
38
* // => [[1.2, 3.4], [2.3]]
39
*
40
* var users = [
41
* { 'user': 'barney', 'age': 36, 'active': false },
42
* { 'user': 'fred', 'age': 40, 'active': true },
43
* { 'user': 'pebbles', 'age': 1, 'active': false }
44
* ];
45
*
46
* var mapper = function(array) {
47
* return _.pluck(array, 'user');
48
* };
49
*
50
* // using the `_.matches` callback shorthand
51
* _.map(_.partition(users, { 'age': 1, 'active': false }), mapper);
52
* // => [['pebbles'], ['barney', 'fred']]
53
*
54
* // using the `_.matchesProperty` callback shorthand
55
* _.map(_.partition(users, 'active', false), mapper);
56
* // => [['barney', 'pebbles'], ['fred']]
57
*
58
* // using the `_.property` callback shorthand
59
* _.map(_.partition(users, 'active'), mapper);
60
* // => [['fred'], ['barney', 'pebbles']]
61
*/
62
var partition = createAggregator(function(result, value, key) {
63
result[key ? 0 : 1].push(value);
64
}, function() { return [[], []]; });
65
66
module.exports = partition;
67
68