forked from tzeikob/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-operator.js
56 lines (41 loc) · 889 Bytes
/
set-operator.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
const Set = require('./set');
class SetOperator {
static union(setA, setB) {
let u = new Set();
setA.values().forEach(value => {
u.add(value);
});
setB.values().forEach(value => {
u.add(value);
});
return u;
}
static intersection(setA, setB) {
let i = new Set();
setA.values().forEach(value => {
if (setB.has(value)) {
i.add(value);
}
});
return i;
}
static difference(setA, setB) {
let d = new Set();
setA.values().forEach(value => {
if (!setB.has(value)) {
d.add(value);
}
});
return d;
}
static subset(setA, setB) {
if (setA.size() === 0 || setB.size() === 0) {
return false;
}
if (setA.size() > setB.size()) {
return false;
}
return setA.values().every(value => setB.has(value));
}
}
module.exports = SetOperator;