source: node_modules/ramda/es/collectBy.js

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

Initial commit

  • Property mode set to 100644
File size: 1.5 KB
RevLine 
[d24f17c]1import _curry2 from "./internal/_curry2.js";
2import _reduce from "./internal/_reduce.js";
3/**
4 * Splits a list into sub-lists, based on the result of calling a key-returning function on each element,
5 * and grouping the results according to values returned.
6 *
7 * @func
8 * @memberOf R
9 * @since v0.28.0
10 * @category List
11 * @typedefn Idx = String | Int | Symbol
12 * @sig Idx a => (b -> a) -> [b] -> [[b]]
13 * @param {Function} fn Function :: a -> Idx
14 * @param {Array} list The array to group
15 * @return {Array}
16 * An array of arrays where each sub-array contains items for which
17 * the String-returning function has returned the same value.
18 * @see R.groupBy, R.partition
19 * @example
20 * R.collectBy(R.prop('type'), [
21 * {type: 'breakfast', item: '☕️'},
22 * {type: 'lunch', item: '🌯'},
23 * {type: 'dinner', item: '🍝'},
24 * {type: 'breakfast', item: '🥐'},
25 * {type: 'lunch', item: '🍕'}
26 * ]);
27 *
28 * // [ [ {type: 'breakfast', item: '☕️'},
29 * // {type: 'breakfast', item: '🥐'} ],
30 * // [ {type: 'lunch', item: '🌯'},
31 * // {type: 'lunch', item: '🍕'} ],
32 * // [ {type: 'dinner', item: '🍝'} ] ]
33 */
34
35var collectBy =
36/*#__PURE__*/
37_curry2(function collectBy(fn, list) {
38 var group = _reduce(function (o, x) {
39 var tag = fn(x);
40
41 if (o[tag] === undefined) {
42 o[tag] = [];
43 }
44
45 o[tag].push(x);
46 return o;
47 }, {}, list);
48
49 var newList = [];
50
51 for (var tag in group) {
52 newList.push(group[tag]);
53 }
54
55 return newList;
56});
57
58export default collectBy;
Note: See TracBrowser for help on using the repository browser.