[d24f17c] | 1 | var _clone =
|
---|
| 2 | /*#__PURE__*/
|
---|
| 3 | require("./internal/_clone.js");
|
---|
| 4 |
|
---|
| 5 | var _curryN =
|
---|
| 6 | /*#__PURE__*/
|
---|
| 7 | require("./internal/_curryN.js");
|
---|
| 8 |
|
---|
| 9 | var _dispatchable =
|
---|
| 10 | /*#__PURE__*/
|
---|
| 11 | require("./internal/_dispatchable.js");
|
---|
| 12 |
|
---|
| 13 | var _has =
|
---|
| 14 | /*#__PURE__*/
|
---|
| 15 | require("./internal/_has.js");
|
---|
| 16 |
|
---|
| 17 | var _reduced =
|
---|
| 18 | /*#__PURE__*/
|
---|
| 19 | require("./internal/_reduced.js");
|
---|
| 20 |
|
---|
| 21 | var _xReduce =
|
---|
| 22 | /*#__PURE__*/
|
---|
| 23 | require("./internal/_xReduce.js");
|
---|
| 24 |
|
---|
| 25 | var _xreduceBy =
|
---|
| 26 | /*#__PURE__*/
|
---|
| 27 | require("./internal/_xreduceBy.js");
|
---|
| 28 |
|
---|
| 29 | var _xwrap =
|
---|
| 30 | /*#__PURE__*/
|
---|
| 31 | require("./internal/_xwrap.js");
|
---|
| 32 | /**
|
---|
| 33 | * Groups the elements of the list according to the result of calling
|
---|
| 34 | * the String-returning function `keyFn` on each element and reduces the elements
|
---|
| 35 | * of each group to a single value via the reducer function `valueFn`.
|
---|
| 36 | *
|
---|
| 37 | * The value function receives two values: *(acc, value)*. It may use
|
---|
| 38 | * [`R.reduced`](#reduced) to short circuit the iteration.
|
---|
| 39 | *
|
---|
| 40 | * This function is basically a more general [`groupBy`](#groupBy) function.
|
---|
| 41 | *
|
---|
| 42 | * Acts as a transducer if a transformer is given in list position.
|
---|
| 43 | *
|
---|
| 44 | * @func
|
---|
| 45 | * @memberOf R
|
---|
| 46 | * @since v0.20.0
|
---|
| 47 | * @category List
|
---|
| 48 | * @sig ((a, b) -> a) -> a -> (b -> String) -> [b] -> {String: a}
|
---|
| 49 | * @param {Function} valueFn The function that reduces the elements of each group to a single
|
---|
| 50 | * value. Receives two values, accumulator for a particular group and the current element.
|
---|
| 51 | * @param {*} acc The (initial) accumulator value for each group.
|
---|
| 52 | * @param {Function} keyFn The function that maps the list's element into a key.
|
---|
| 53 | * @param {Array} list The array to group.
|
---|
| 54 | * @return {Object} An object with the output of `keyFn` for keys, mapped to the output of
|
---|
| 55 | * `valueFn` for elements which produced that key when passed to `keyFn`.
|
---|
| 56 | * @see R.groupBy, R.reduce, R.reduced
|
---|
| 57 | * @example
|
---|
| 58 | *
|
---|
| 59 | * const groupNames = (acc, {name}) => acc.concat(name)
|
---|
| 60 | * const toGrade = ({score}) =>
|
---|
| 61 | * score < 65 ? 'F' :
|
---|
| 62 | * score < 70 ? 'D' :
|
---|
| 63 | * score < 80 ? 'C' :
|
---|
| 64 | * score < 90 ? 'B' : 'A'
|
---|
| 65 | *
|
---|
| 66 | * var students = [
|
---|
| 67 | * {name: 'Abby', score: 83},
|
---|
| 68 | * {name: 'Bart', score: 62},
|
---|
| 69 | * {name: 'Curt', score: 88},
|
---|
| 70 | * {name: 'Dora', score: 92},
|
---|
| 71 | * ]
|
---|
| 72 | *
|
---|
| 73 | * reduceBy(groupNames, [], toGrade, students)
|
---|
| 74 | * //=> {"A": ["Dora"], "B": ["Abby", "Curt"], "F": ["Bart"]}
|
---|
| 75 | */
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | var reduceBy =
|
---|
| 79 | /*#__PURE__*/
|
---|
| 80 | _curryN(4, [],
|
---|
| 81 | /*#__PURE__*/
|
---|
| 82 | _dispatchable([], _xreduceBy, function reduceBy(valueFn, valueAcc, keyFn, list) {
|
---|
| 83 | var xf = _xwrap(function (acc, elt) {
|
---|
| 84 | var key = keyFn(elt);
|
---|
| 85 | var value = valueFn(_has(key, acc) ? acc[key] : _clone(valueAcc, false), elt);
|
---|
| 86 |
|
---|
| 87 | if (value && value['@@transducer/reduced']) {
|
---|
| 88 | return _reduced(acc);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | acc[key] = value;
|
---|
| 92 | return acc;
|
---|
| 93 | });
|
---|
| 94 |
|
---|
| 95 | return _xReduce(xf, {}, list);
|
---|
| 96 | }));
|
---|
| 97 |
|
---|
| 98 | module.exports = reduceBy; |
---|