forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectBy.js
47 lines (41 loc) · 1.57 KB
/
collectBy.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
38
39
40
41
42
43
44
45
46
47
var {all, compose , difference , equals , head , identity , is , isEmpty , length , uniq , unnest , collectBy} = require('../source/index.js');
var fc = require('fast-check');
var {spy} = require('sinon');
describe('collectBy', function() {
it('returns a list of lists', function() {
fc.assert(fc.property(fc.array(fc.nat()), function(xs) {
var check = all(is(Array));
var ys = collectBy(identity)(xs);
return check(ys);
}));
});
it('groups items but neither adds new ones nor removes any', function() {
fc.assert(fc.property(fc.array(fc.nat()), function(xs) {
var check = compose(isEmpty, difference(xs), unnest);
var ys = collectBy(identity)(xs);
return check(ys);
}));
});
it('groups related items together', function() {
fc.assert(fc.property(fc.array(fc.boolean()), function(xs) {
var ys = collectBy(identity)(xs);
var check = all(compose(equals(1), length, uniq));
return check(ys);
}));
});
it('invokes the tag function for each item in the list', function() {
fc.assert(fc.property(fc.array(fc.nat()), function(xs) {
var id = spy(x => 42);
collectBy(id)(xs);
var check = compose(isEmpty, difference(xs));
return check(id.getCalls().map(call => call.args[0]));
}));
});
it('groups items according to the tag value', function() {
fc.assert(fc.property(fc.array(fc.nat()), function(xs) {
var ys = collectBy(x => 42)(xs);
var check = compose(isEmpty, difference(xs), head);
return isEmpty(xs) && isEmpty(ys) ? true : check(ys);
}));
});
});