set.js
2.56 KB
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
101
102
103
104
105
106
107
108
109
110
111
'use strict';
const Ref = require('./ref');
module.exports = class Set {
constructor() {
this._set = [];
}
add(value, refs) {
if (!Ref.isRef(value) && this.has(value, null, null, false)) {
return;
}
if (refs !== undefined) { // If it's a merge, we don't have any refs
Ref.push(refs, value);
}
this._set.push(value);
return this;
}
merge(add, remove) {
for (let i = 0; i < add._set.length; ++i) {
this.add(add._set[i]);
}
for (let i = 0; i < remove._set.length; ++i) {
this.remove(remove._set[i]);
}
return this;
}
remove(value) {
this._set = this._set.filter((item) => value !== item);
return this;
}
has(value, state, options, insensitive) {
for (let i = 0; i < this._set.length; ++i) {
let items = this._set[i];
if (state && Ref.isRef(items)) { // Only resolve references if there is a state, otherwise it's a merge
items = items(state.reference || state.parent, options);
}
if (!Array.isArray(items)) {
items = [items];
}
for (let j = 0; j < items.length; ++j) {
const item = items[j];
if (typeof value !== typeof item) {
continue;
}
if (value === item ||
(value instanceof Date && item instanceof Date && value.getTime() === item.getTime()) ||
(insensitive && typeof value === 'string' && value.toLowerCase() === item.toLowerCase()) ||
(Buffer.isBuffer(value) && Buffer.isBuffer(item) && value.length === item.length && value.toString('binary') === item.toString('binary'))) {
return true;
}
}
}
return false;
}
values(options) {
if (options && options.stripUndefined) {
const values = [];
for (let i = 0; i < this._set.length; ++i) {
const item = this._set[i];
if (item !== undefined) {
values.push(item);
}
}
return values;
}
return this._set.slice();
}
slice() {
const newSet = new Set();
newSet._set = this._set.slice();
return newSet;
}
concat(source) {
const newSet = new Set();
newSet._set = this._set.concat(source._set);
return newSet;
}
};