source: node_modules/ramda/es/groupWith.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 
1import _curry2 from "./internal/_curry2.js";
2/**
3 * Takes a list and returns a list of lists where each sublist's elements are
4 * all satisfied pairwise comparison according to the provided function.
5 * Only adjacent elements are passed to the comparison function.
6 *
7 * @func
8 * @memberOf R
9 * @since v0.21.0
10 * @category List
11 * @sig ((a, a) → Boolean) → [a] → [[a]]
12 * @param {Function} fn Function for determining whether two given (adjacent)
13 * elements should be in the same group
14 * @param {Array} list The array to group. Also accepts a string, which will be
15 * treated as a list of characters.
16 * @return {List} A list that contains sublists of elements,
17 * whose concatenations are equal to the original list.
18 * @example
19 *
20 * R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21])
21 * //=> [[0], [1, 1], [2], [3], [5], [8], [13], [21]]
22 *
23 * R.groupWith((a, b) => a + 1 === b, [0, 1, 1, 2, 3, 5, 8, 13, 21])
24 * //=> [[0, 1], [1, 2, 3], [5], [8], [13], [21]]
25 *
26 * R.groupWith((a, b) => a % 2 === b % 2, [0, 1, 1, 2, 3, 5, 8, 13, 21])
27 * //=> [[0], [1, 1], [2], [3, 5], [8], [13, 21]]
28 *
29 * const isVowel = R.test(/^[aeiou]$/i);
30 * R.groupWith(R.eqBy(isVowel), 'aestiou')
31 * //=> ['ae', 'st', 'iou']
32 */
33
34var groupWith =
35/*#__PURE__*/
36_curry2(function (fn, list) {
37 var res = [];
38 var idx = 0;
39 var len = list.length;
40
41 while (idx < len) {
42 var nextidx = idx + 1;
43
44 while (nextidx < len && fn(list[nextidx - 1], list[nextidx])) {
45 nextidx += 1;
46 }
47
48 res.push(list.slice(idx, nextidx));
49 idx = nextidx;
50 }
51
52 return res;
53});
54
55export default groupWith;
Note: See TracBrowser for help on using the repository browser.