forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupWith.js
37 lines (30 loc) · 1.43 KB
/
groupWith.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
describe('groupWith', function() {
it('splits the list into groups according to the grouping function', function() {
eq(R.groupWith(R.equals, [1, 2, 2, 3]), [[1], [2, 2], [3]]);
eq(R.groupWith(R.equals, [1, 1, 1, 1]), [[1, 1, 1, 1]]);
eq(R.groupWith(R.equals, [1, 2, 3, 4]), [[1], [2], [3], [4]]);
});
it('splits the list into "streaks" testing adjacent elements', function() {
var isConsecutive = function(a, b) { return a + 1 === b; };
eq(R.groupWith(isConsecutive, []), []);
eq(R.groupWith(isConsecutive, [4, 3, 2, 1]), [[4], [3], [2], [1]]);
eq(R.groupWith(isConsecutive, [1, 2, 3, 4]), [[1, 2, 3, 4]]);
eq(R.groupWith(isConsecutive, [1, 2, 2, 3]), [[1, 2], [2, 3]]);
eq(R.groupWith(isConsecutive, [1, 2, 9, 3, 4]), [[1, 2], [9], [3, 4]]);
});
it('returns an empty array if given an empty array', function() {
eq(R.groupWith(R.equals, []), []);
});
it('can be turned into the original list through concatenation', function() {
var list = [1, 1, 2, 3, 4, 4, 5, 5];
eq(R.unnest(R.groupWith(R.equals, list)), list);
eq(R.unnest(R.groupWith(R.complement(R.equals), list)), list);
eq(R.unnest(R.groupWith(R.T, list)), list);
eq(R.unnest(R.groupWith(R.F, list)), list);
});
it('also works on strings', function() {
eq(R.groupWith(R.equals)('Mississippi'), ['M','i','ss','i','ss','i','pp','i']);
});
});