source: node_modules/ramda/src/groupBy.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
1var _checkForMethod =
2/*#__PURE__*/
3require("./internal/_checkForMethod.js");
4
5var _curry2 =
6/*#__PURE__*/
7require("./internal/_curry2.js");
8
9var reduceBy =
10/*#__PURE__*/
11require("./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
55var groupBy =
56/*#__PURE__*/
57_curry2(
58/*#__PURE__*/
59_checkForMethod('groupBy',
60/*#__PURE__*/
61reduceBy(function (acc, item) {
62 acc.push(item);
63 return acc;
64}, [])));
65
66module.exports = groupBy;
Note: See TracBrowser for help on using the repository browser.