-
Notifications
You must be signed in to change notification settings - Fork 269
/
index.js
77 lines (62 loc) · 1.35 KB
/
index.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
// XSet because ES6 already has a Set class
class XSet {
constructor() {
this.data = this.getStore();
}
add(element) {
this.data.push(element);
}
remove(element) {
this.data.pop(element);
}
has(element) {
return this.data.contains(element);
}
values() {
return this.data.val();
}
union(givenSet) {
const result = new XSet();
const firstSetValues = this.values();
const givenSetValues = givenSet.values();
// eslint-disable-next-line no-restricted-syntax
for (const e of firstSetValues) result.add(e);
// eslint-disable-next-line no-restricted-syntax
for (const e of givenSetValues) result.add(e);
return result;
}
// eslint-disable-next-line class-methods-use-this
getStore() {
const store = {};
return {
push(el) {
if (!store[el]) {
store[el] = true;
}
},
pop(el) {
if (store[el]) {
delete store[el];
}
},
contains(el) {
return !!store[el];
},
val() {
return Object.keys(store);
},
};
}
}
// const s = new XSet();
// s.add(10);
// s.add(20);
// s.add(90);
// console.log(s.has(1));
// console.log(s.has(10));
// console.log(s.has(90));
// console.log(s.values());
// s.remove(90);
// console.log(s.has(90));
// console.log(s.data);
module.exports = XSet;