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