forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymmetricDifference.js
100 lines (85 loc) · 3.83 KB
/
symmetricDifference.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var fc = require('fast-check');
var {Just} = require('./shared/Maybe.js');
describe('symmetricDifference', function() {
var M = [1, 2, 3, 4];
var M2 = [1, 2, 3, 4, 1, 2, 3, 4];
var N = [3, 4, 5, 6];
var N2 = [3, 3, 4, 4, 5, 5, 6, 6];
var Z = [3, 4, 5, 6, 10];
var Z2 = [1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8];
it('finds the set of all elements in the first or second list but not both', function() {
eq(R.symmetricDifference(M, N), [1, 2, 5, 6]);
});
it('does not allow duplicates in the output even if the input lists had duplicates', function() {
eq(R.symmetricDifference(M2, N2), [1, 2, 5, 6]);
});
it('has R.equals semantics', function() {
eq(R.symmetricDifference([0], [-0]).length, 2);
eq(R.symmetricDifference([-0], [0]).length, 2);
eq(R.symmetricDifference([NaN], [NaN]).length, 0);
eq(R.symmetricDifference([new Just([42])], [new Just([42])]).length, 0);
});
it('works for arrays of different lengths', function() {
eq(R.symmetricDifference(Z, Z2), [10, 1, 2, 7, 8]);
eq(R.symmetricDifference(Z2, Z), [1, 2, 7, 8, 10]);
});
it('will not create a "sparse" array', function() {
eq(R.symmetricDifference(M2, [3]).length, 3);
});
it('returns an empty array if there are no different elements', function() {
eq(R.symmetricDifference(M2, M), []);
eq(R.symmetricDifference(M, M2), []);
});
// Arbitrary producing arrays of unique values (with respect to R.equals)
var compatibleREquals = fc.array(fc.anything({
maxDepth: 0,
withBoxedValues: true,
withNullPrototype: true,
withObjectString: true
})).map(array => R.uniq(array));
it('returns empty arrays when receiving twice the same array', function() {
fc.assert(fc.property(fc.clone(compatibleREquals, 2), function(arrays) {
var A1 = arrays[0];
var A2 = arrays[1];
eq(R.symmetricDifference(A1, A2), []);
}));
});
it('returns empty arrays when receiving an array and a permutation of it', function() {
fc.assert(fc.property(fc.clone(compatibleREquals, 2).chain(function(arrays) {
return fc.tuple(fc.constant(arrays[0]), fc.shuffledSubarray(arrays[1], {minLength: arrays[1].length, maxLength: arrays[1].length}));
}), function(arrays) {
var A1 = arrays[0];
var A2 = arrays[1];
eq(R.symmetricDifference(A1, A2), []);
}));
});
it('returns missing items when receiving an array and a permuted subset of it', function() {
fc.assert(fc.property(fc.clone(compatibleREquals, 2).chain(function(arrays) {
return fc.tuple(fc.constant(arrays[0]), fc.shuffledSubarray(arrays[1]));
}), function(arrays) {
var A1 = arrays[0];
var A2 = arrays[1];
eq(R.symmetricDifference(A1, A2).length, A1.length - A2.length);
}));
});
it('returns an array not containing too many items', function() {
fc.assert(fc.property(compatibleREquals, compatibleREquals, compatibleREquals, compatibleREquals, compatibleREquals, function(A1, A2, B, C1, C2) {
var M = R.uniq(A1.concat(B).concat(C1));
var N = R.uniq(A2.concat(B).concat(C2));
var difference = R.symmetricDifference(M, N);
var upperBoundDifferenceLength = A1.length + A2.length + C1.length + C2.length;
eq(difference.length <= upperBoundDifferenceLength, true);
}));
});
it('returns an array containing only items coming from one of the sources', function() {
fc.assert(fc.property(compatibleREquals, compatibleREquals, compatibleREquals, compatibleREquals, compatibleREquals, function(A1, A2, B, C1, C2) {
var M = R.uniq(A1.concat(B).concat(C1));
var N = R.uniq(A2.concat(B).concat(C2));
var MN = R.uniq(M.concat(N));
var difference = R.symmetricDifference(M, N);
eq(R.symmetricDifference(difference, MN).length, MN.length - difference.length);
}));
});
});