set.js
4.26 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict';
const Ref = require('./ref');
const internals = {};
internals.extendedCheckForValue = function (value, insensitive) {
const valueType = typeof value;
if (valueType === 'object') {
if (value instanceof Date) {
return (item) => {
return item instanceof Date && value.getTime() === item.getTime();
};
}
if (Buffer.isBuffer(value)) {
return (item) => {
return Buffer.isBuffer(item) && value.length === item.length && value.toString('binary') === item.toString('binary');
};
}
}
else if (insensitive && valueType === 'string') {
const lowercaseValue = value.toLowerCase();
return (item) => {
return typeof item === 'string' && lowercaseValue === item.toLowerCase();
};
}
return null;
};
module.exports = class InternalSet {
constructor(from) {
this._set = new Set(from);
this._hasRef = false;
}
add(value, refs) {
const isRef = Ref.isRef(value);
if (!isRef && this.has(value, null, null, false)) {
return this;
}
if (refs !== undefined) { // If it's a merge, we don't have any refs
Ref.push(refs, value);
}
this._set.add(value);
this._hasRef |= isRef;
return this;
}
merge(add, remove) {
for (const item of add._set) {
this.add(item);
}
for (const item of remove._set) {
this.remove(item);
}
return this;
}
remove(value) {
this._set.delete(value);
return this;
}
has(value, state, options, insensitive) {
return !!this.get(value, state, options, insensitive);
}
get(value, state, options, insensitive) {
if (!this._set.size) {
return false;
}
const hasValue = this._set.has(value);
if (hasValue) {
return { value };
}
const extendedCheck = internals.extendedCheckForValue(value, insensitive);
if (!extendedCheck) {
if (state && this._hasRef) {
for (let item of this._set) {
if (Ref.isRef(item)) {
item = [].concat(item(state.reference || state.parent, options));
const found = item.indexOf(value);
if (found >= 0) {
return { value: item[found] };
}
}
}
}
return false;
}
return this._has(value, state, options, extendedCheck);
}
_has(value, state, options, check) {
const checkRef = !!(state && this._hasRef);
const isReallyEqual = function (item) {
if (value === item) {
return true;
}
return check(item);
};
for (let item of this._set) {
if (checkRef && Ref.isRef(item)) { // Only resolve references if there is a state, otherwise it's a merge
item = item(state.reference || state.parent, options);
if (Array.isArray(item)) {
const found = item.findIndex(isReallyEqual);
if (found >= 0) {
return {
value: item[found]
};
}
continue;
}
}
if (isReallyEqual(item)) {
return {
value: item
};
}
}
return false;
}
values(options) {
if (options && options.stripUndefined) {
const values = [];
for (const item of this._set) {
if (item !== undefined) {
values.push(item);
}
}
return values;
}
return Array.from(this._set);
}
slice() {
const set = new InternalSet(this._set);
set._hasRef = this._hasRef;
return set;
}
concat(source) {
const set = new InternalSet([...this._set, ...source._set]);
set._hasRef = !!(this._hasRef | source._hasRef);
return set;
}
};