UNPKG

1.82 kBJavaScriptView Raw
1var _checkForMethod =
2/*#__PURE__*/
3require("./internal/_checkForMethod");
4
5var _curry2 =
6/*#__PURE__*/
7require("./internal/_curry2");
8
9var reduceBy =
10/*#__PURE__*/
11require("./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
54var groupBy =
55/*#__PURE__*/
56_curry2(
57/*#__PURE__*/
58_checkForMethod('groupBy',
59/*#__PURE__*/
60reduceBy(function (acc, item) {
61 if (acc == null) {
62 acc = [];
63 }
64
65 acc.push(item);
66 return acc;
67}, null)));
68
69module.exports = groupBy;
\No newline at end of file