forked from montagejs/collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-set.js
59 lines (48 loc) · 1.41 KB
/
generic-set.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
module.exports = GenericSet;
function GenericSet() {
throw new Error("Can't construct. GenericSet is a mixin.");
}
GenericSet.prototype.union = function (that) {
var union = this.constructClone(this);
union.addEach(that);
return union;
};
GenericSet.prototype.intersection = function (that) {
return this.constructClone(this.filter(function (value) {
return that.has(value);
}));
};
GenericSet.prototype.difference = function (that) {
var union = this.constructClone(this);
union.deleteEach(that);
return union;
};
GenericSet.prototype.symmetricDifference = function (that) {
var union = this.union(that);
var intersection = this.intersection(that);
return union.difference(intersection);
};
GenericSet.prototype.equals = function (that, equals) {
var self = this;
return (
Object.can(that, "reduce") &&
this.length === that.length &&
that.reduce(function (equal, value) {
return equal && self.has(value, equals);
}, true)
);
};
// W3C DOMTokenList API overlap (does not handle variadic arguments)
GenericSet.prototype.contains = function (value) {
return this.has(value);
};
GenericSet.prototype.remove = function (value) {
return this["delete"](value);
};
GenericSet.prototype.toggle = function (value) {
if (this.has(value)) {
this["delete"](value);
} else {
this.add(value);
}
};