1 | var _checkForMethod =
|
2 | /*#__PURE__*/
|
3 | require("./internal/_checkForMethod");
|
4 |
|
5 | var _curry2 =
|
6 | /*#__PURE__*/
|
7 | require("./internal/_curry2");
|
8 |
|
9 | var reduceBy =
|
10 | /*#__PURE__*/
|
11 | require("./reduceBy");
|
12 | /**
|
13 | * Splits a list into sub-lists stored in an object, based on the result of
|
14 | * calling a String-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 | * @sig (a -> String) -> [a] -> {String: [a]}
|
26 | * @param {Function} fn Function :: a -> String
|
27 | * @param {Array} list The array to group
|
28 | * @return {Object} An object with the output of `fn` for keys, mapped to arrays of elements
|
29 | * that produced that key when passed to `fn`.
|
30 | * @see R.reduceBy, R.transduce
|
31 | * @example
|
32 | *
|
33 | * const byGrade = R.groupBy(function(student) {
|
34 | * const score = student.score;
|
35 | * return score < 65 ? 'F' :
|
36 | * score < 70 ? 'D' :
|
37 | * score < 80 ? 'C' :
|
38 | * score < 90 ? 'B' : 'A';
|
39 | * });
|
40 | * const students = [{name: 'Abby', score: 84},
|
41 | * {name: 'Eddy', score: 58},
|
42 | * // ...
|
43 | * {name: 'Jack', score: 69}];
|
44 | * byGrade(students);
|
45 | * // {
|
46 | * // 'A': [{name: 'Dianne', score: 99}],
|
47 | * // 'B': [{name: 'Abby', score: 84}]
|
48 | * // // ...,
|
49 | * // 'F': [{name: 'Eddy', score: 58}]
|
50 | * // }
|
51 | */
|
52 |
|
53 |
|
54 | var groupBy =
|
55 | /*#__PURE__*/
|
56 | _curry2(
|
57 | /*#__PURE__*/
|
58 | _checkForMethod('groupBy',
|
59 | /*#__PURE__*/
|
60 | reduceBy(function (acc, item) {
|
61 | if (acc == null) {
|
62 | acc = [];
|
63 | }
|
64 |
|
65 | acc.push(item);
|
66 | return acc;
|
67 | }, null)));
|
68 |
|
69 | module.exports = groupBy; |
\ | No newline at end of file |