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