1 | var _checkForMethod =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_checkForMethod.js");
|
---|
4 |
|
---|
5 | var _curry2 =
|
---|
6 | /*#__PURE__*/
|
---|
7 | require("./internal/_curry2.js");
|
---|
8 |
|
---|
9 | var reduceBy =
|
---|
10 | /*#__PURE__*/
|
---|
11 | require("./reduceBy.js");
|
---|
12 | /**
|
---|
13 | * Splits a list into sub-lists stored in an object, based on the result of
|
---|
14 | * calling a key-returning function on each element, and grouping the
|
---|
15 | * results according to values returned.
|
---|
16 | *
|
---|
17 | * Dispatches to the `groupBy` method of the second argument, if present.
|
---|
18 | *
|
---|
19 | * Acts as a transducer if a transformer is given in list position.
|
---|
20 | *
|
---|
21 | * @func
|
---|
22 | * @memberOf R
|
---|
23 | * @since v0.1.0
|
---|
24 | * @category List
|
---|
25 | * @typedefn Idx = String | Int | Symbol
|
---|
26 | * @sig Idx a => (b -> a) -> [b] -> {a: [b]}
|
---|
27 | * @param {Function} fn Function :: a -> Idx
|
---|
28 | * @param {Array} list The array to group
|
---|
29 | * @return {Object} An object with the output of `fn` for keys, mapped to arrays of elements
|
---|
30 | * that produced that key when passed to `fn`.
|
---|
31 | * @see R.reduceBy, R.transduce, R.indexBy, R.collectBy
|
---|
32 | * @example
|
---|
33 | *
|
---|
34 | * const byGrade = R.groupBy(function(student) {
|
---|
35 | * const score = student.score;
|
---|
36 | * return score < 65 ? 'F' :
|
---|
37 | * score < 70 ? 'D' :
|
---|
38 | * score < 80 ? 'C' :
|
---|
39 | * score < 90 ? 'B' : 'A';
|
---|
40 | * });
|
---|
41 | * const students = [{name: 'Abby', score: 84},
|
---|
42 | * {name: 'Eddy', score: 58},
|
---|
43 | * // ...
|
---|
44 | * {name: 'Jack', score: 69}];
|
---|
45 | * byGrade(students);
|
---|
46 | * // {
|
---|
47 | * // 'A': [{name: 'Dianne', score: 99}],
|
---|
48 | * // 'B': [{name: 'Abby', score: 84}]
|
---|
49 | * // // ...,
|
---|
50 | * // 'F': [{name: 'Eddy', score: 58}]
|
---|
51 | * // }
|
---|
52 | */
|
---|
53 |
|
---|
54 |
|
---|
55 | var groupBy =
|
---|
56 | /*#__PURE__*/
|
---|
57 | _curry2(
|
---|
58 | /*#__PURE__*/
|
---|
59 | _checkForMethod('groupBy',
|
---|
60 | /*#__PURE__*/
|
---|
61 | reduceBy(function (acc, item) {
|
---|
62 | acc.push(item);
|
---|
63 | return acc;
|
---|
64 | }, [])));
|
---|
65 |
|
---|
66 | module.exports = groupBy; |
---|