|
|
- import _checkForMethod from "./internal/_checkForMethod.js";
- import _curry2 from "./internal/_curry2.js";
- import reduceBy from "./reduceBy.js";
- /**
- * Splits a list into sub-lists stored in an object, based on the result of
- * calling a String-returning function on each element, and grouping the
- * results according to values returned.
- *
- * Dispatches to the `groupBy` method of the second argument, if present.
- *
- * Acts as a transducer if a transformer is given in list position.
- *
- * @func
- * @memberOf R
- * @since v0.1.0
- * @category List
- * @sig (a -> String) -> [a] -> {String: [a]}
- * @param {Function} fn Function :: a -> String
- * @param {Array} list The array to group
- * @return {Object} An object with the output of `fn` for keys, mapped to arrays of elements
- * that produced that key when passed to `fn`.
- * @see R.reduceBy, R.transduce
- * @example
- *
- * const byGrade = R.groupBy(function(student) {
- * const score = student.score;
- * return score < 65 ? 'F' :
- * score < 70 ? 'D' :
- * score < 80 ? 'C' :
- * score < 90 ? 'B' : 'A';
- * });
- * const students = [{name: 'Abby', score: 84},
- * {name: 'Eddy', score: 58},
- * // ...
- * {name: 'Jack', score: 69}];
- * byGrade(students);
- * // {
- * // 'A': [{name: 'Dianne', score: 99}],
- * // 'B': [{name: 'Abby', score: 84}]
- * // // ...,
- * // 'F': [{name: 'Eddy', score: 58}]
- * // }
- */
-
- var groupBy =
- /*#__PURE__*/
- _curry2(
- /*#__PURE__*/
- _checkForMethod('groupBy',
- /*#__PURE__*/
- reduceBy(function (acc, item) {
- if (acc == null) {
- acc = [];
- }
-
- acc.push(item);
- return acc;
- }, null)));
-
- export default groupBy;
|