mirror of
https://github.com/swc-project/swc.git
synced 2024-12-22 05:01:42 +03:00
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
// Loaded from https://deno.land/x/ramda@v0.27.2/source/groupBy.js
|
|
|
|
|
|
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 key-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
|
|
* @typedefn Idx = String | Int | Symbol
|
|
* @sig Idx a => (b -> a) -> [b] -> {a: [b]}
|
|
* @param {Function} fn Function :: a -> Idx
|
|
* @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, R.indexBy
|
|
* @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 = _curry2(_checkForMethod('groupBy', reduceBy(function(acc, item) {
|
|
if (acc == null) {
|
|
acc = [];
|
|
}
|
|
acc.push(item);
|
|
return acc;
|
|
}, null)));
|
|
export default groupBy;
|