ts-data-forge 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +534 -0
- package/package.json +101 -0
- package/src/array/array-utils-creation.test.mts +443 -0
- package/src/array/array-utils-modification.test.mts +197 -0
- package/src/array/array-utils-overload-type-error.test.mts +149 -0
- package/src/array/array-utils-reducing-value.test.mts +425 -0
- package/src/array/array-utils-search.test.mts +169 -0
- package/src/array/array-utils-set-op.test.mts +335 -0
- package/src/array/array-utils-slice-clamped.test.mts +113 -0
- package/src/array/array-utils-slicing.test.mts +316 -0
- package/src/array/array-utils-transformation.test.mts +790 -0
- package/src/array/array-utils-validation.test.mts +492 -0
- package/src/array/array-utils.mts +4000 -0
- package/src/array/array.test.mts +146 -0
- package/src/array/index.mts +2 -0
- package/src/array/tuple-utils.mts +519 -0
- package/src/array/tuple-utils.test.mts +518 -0
- package/src/collections/imap-mapped.mts +801 -0
- package/src/collections/imap-mapped.test.mts +860 -0
- package/src/collections/imap.mts +651 -0
- package/src/collections/imap.test.mts +932 -0
- package/src/collections/index.mts +6 -0
- package/src/collections/iset-mapped.mts +889 -0
- package/src/collections/iset-mapped.test.mts +1187 -0
- package/src/collections/iset.mts +682 -0
- package/src/collections/iset.test.mts +1084 -0
- package/src/collections/queue.mts +390 -0
- package/src/collections/queue.test.mts +282 -0
- package/src/collections/stack.mts +423 -0
- package/src/collections/stack.test.mts +225 -0
- package/src/expect-type.mts +206 -0
- package/src/functional/index.mts +4 -0
- package/src/functional/match.mts +300 -0
- package/src/functional/match.test.mts +177 -0
- package/src/functional/optional.mts +733 -0
- package/src/functional/optional.test.mts +619 -0
- package/src/functional/pipe.mts +212 -0
- package/src/functional/pipe.test.mts +85 -0
- package/src/functional/result.mts +1134 -0
- package/src/functional/result.test.mts +777 -0
- package/src/globals.d.mts +38 -0
- package/src/guard/has-key.mts +119 -0
- package/src/guard/has-key.test.mts +219 -0
- package/src/guard/index.mts +7 -0
- package/src/guard/is-non-empty-string.mts +108 -0
- package/src/guard/is-non-empty-string.test.mts +91 -0
- package/src/guard/is-non-null-object.mts +106 -0
- package/src/guard/is-non-null-object.test.mts +90 -0
- package/src/guard/is-primitive.mts +165 -0
- package/src/guard/is-primitive.test.mts +102 -0
- package/src/guard/is-record.mts +153 -0
- package/src/guard/is-record.test.mts +112 -0
- package/src/guard/is-type.mts +450 -0
- package/src/guard/is-type.test.mts +496 -0
- package/src/guard/key-is-in.mts +163 -0
- package/src/guard/key-is-in.test.mts +19 -0
- package/src/index.mts +10 -0
- package/src/iterator/index.mts +1 -0
- package/src/iterator/range.mts +120 -0
- package/src/iterator/range.test.mts +33 -0
- package/src/json/index.mts +1 -0
- package/src/json/json.mts +711 -0
- package/src/json/json.test.mts +628 -0
- package/src/number/branded-types/finite-number.mts +354 -0
- package/src/number/branded-types/finite-number.test.mts +135 -0
- package/src/number/branded-types/index.mts +26 -0
- package/src/number/branded-types/int.mts +278 -0
- package/src/number/branded-types/int.test.mts +140 -0
- package/src/number/branded-types/int16.mts +192 -0
- package/src/number/branded-types/int16.test.mts +170 -0
- package/src/number/branded-types/int32.mts +193 -0
- package/src/number/branded-types/int32.test.mts +170 -0
- package/src/number/branded-types/non-negative-finite-number.mts +223 -0
- package/src/number/branded-types/non-negative-finite-number.test.mts +188 -0
- package/src/number/branded-types/non-negative-int16.mts +187 -0
- package/src/number/branded-types/non-negative-int16.test.mts +201 -0
- package/src/number/branded-types/non-negative-int32.mts +187 -0
- package/src/number/branded-types/non-negative-int32.test.mts +204 -0
- package/src/number/branded-types/non-zero-finite-number.mts +229 -0
- package/src/number/branded-types/non-zero-finite-number.test.mts +198 -0
- package/src/number/branded-types/non-zero-int.mts +167 -0
- package/src/number/branded-types/non-zero-int.test.mts +177 -0
- package/src/number/branded-types/non-zero-int16.mts +196 -0
- package/src/number/branded-types/non-zero-int16.test.mts +195 -0
- package/src/number/branded-types/non-zero-int32.mts +196 -0
- package/src/number/branded-types/non-zero-int32.test.mts +197 -0
- package/src/number/branded-types/non-zero-safe-int.mts +196 -0
- package/src/number/branded-types/non-zero-safe-int.test.mts +232 -0
- package/src/number/branded-types/non-zero-uint16.mts +189 -0
- package/src/number/branded-types/non-zero-uint16.test.mts +199 -0
- package/src/number/branded-types/non-zero-uint32.mts +189 -0
- package/src/number/branded-types/non-zero-uint32.test.mts +199 -0
- package/src/number/branded-types/positive-finite-number.mts +241 -0
- package/src/number/branded-types/positive-finite-number.test.mts +204 -0
- package/src/number/branded-types/positive-int.mts +304 -0
- package/src/number/branded-types/positive-int.test.mts +176 -0
- package/src/number/branded-types/positive-int16.mts +188 -0
- package/src/number/branded-types/positive-int16.test.mts +197 -0
- package/src/number/branded-types/positive-int32.mts +188 -0
- package/src/number/branded-types/positive-int32.test.mts +197 -0
- package/src/number/branded-types/positive-safe-int.mts +187 -0
- package/src/number/branded-types/positive-safe-int.test.mts +210 -0
- package/src/number/branded-types/positive-uint16.mts +188 -0
- package/src/number/branded-types/positive-uint16.test.mts +203 -0
- package/src/number/branded-types/positive-uint32.mts +188 -0
- package/src/number/branded-types/positive-uint32.test.mts +203 -0
- package/src/number/branded-types/safe-int.mts +291 -0
- package/src/number/branded-types/safe-int.test.mts +170 -0
- package/src/number/branded-types/safe-uint.mts +187 -0
- package/src/number/branded-types/safe-uint.test.mts +176 -0
- package/src/number/branded-types/uint.mts +179 -0
- package/src/number/branded-types/uint.test.mts +158 -0
- package/src/number/branded-types/uint16.mts +186 -0
- package/src/number/branded-types/uint16.test.mts +170 -0
- package/src/number/branded-types/uint32.mts +218 -0
- package/src/number/branded-types/uint32.test.mts +170 -0
- package/src/number/enum/index.mts +2 -0
- package/src/number/enum/int8.mts +344 -0
- package/src/number/enum/int8.test.mts +180 -0
- package/src/number/enum/uint8.mts +293 -0
- package/src/number/enum/uint8.test.mts +164 -0
- package/src/number/index.mts +4 -0
- package/src/number/num.mts +604 -0
- package/src/number/num.test.mts +242 -0
- package/src/number/refined-number-utils.mts +566 -0
- package/src/object/index.mts +1 -0
- package/src/object/object.mts +447 -0
- package/src/object/object.test.mts +124 -0
- package/src/others/cast-mutable.mts +113 -0
- package/src/others/cast-readonly.mts +192 -0
- package/src/others/cast-readonly.test.mts +89 -0
- package/src/others/if-then.mts +98 -0
- package/src/others/if-then.test.mts +75 -0
- package/src/others/index.mts +7 -0
- package/src/others/map-nullable.mts +172 -0
- package/src/others/map-nullable.test.mts +297 -0
- package/src/others/memoize-function.mts +196 -0
- package/src/others/memoize-function.test.mts +168 -0
- package/src/others/tuple.mts +160 -0
- package/src/others/tuple.test.mts +11 -0
- package/src/others/unknown-to-string.mts +215 -0
- package/src/others/unknown-to-string.test.mts +114 -0
|
@@ -0,0 +1,1187 @@
|
|
|
1
|
+
import { ISetMapped } from './iset-mapped.mjs';
|
|
2
|
+
|
|
3
|
+
const toKey = (a: Readonly<{ v: number }>): number => a.v;
|
|
4
|
+
const fromKey = (k: number): Readonly<{ v: number }> => ({ v: k });
|
|
5
|
+
|
|
6
|
+
// Test types for additional functionality
|
|
7
|
+
type TestElement = { id: number; type: string };
|
|
8
|
+
const testElementToString = (el: Readonly<TestElement>): string =>
|
|
9
|
+
`${el.type}_${el.id}`;
|
|
10
|
+
const stringToTestElement = (str: string): TestElement => {
|
|
11
|
+
const [type, idStr] = str.split('_');
|
|
12
|
+
return { type: type ?? '', id: Number(idStr ?? '0') };
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
describe('ISetMapped[Symbol.iterator]', () => {
|
|
16
|
+
test('case 1', () => {
|
|
17
|
+
const s0 = ISetMapped.create(
|
|
18
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
19
|
+
toKey,
|
|
20
|
+
fromKey,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
expect(s0).toStrictEqual(
|
|
24
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('ISetMapped.size', () => {
|
|
30
|
+
test('case 1', () => {
|
|
31
|
+
const s0 = ISetMapped.create(
|
|
32
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
33
|
+
toKey,
|
|
34
|
+
fromKey,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
expect(s0.size).toBe(3);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('ISetMapped.has', () => {
|
|
42
|
+
test('case 1', () => {
|
|
43
|
+
const s0 = ISetMapped.create(
|
|
44
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
45
|
+
toKey,
|
|
46
|
+
fromKey,
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
expect(s0.has({ v: 3 })).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
test('case 2', () => {
|
|
52
|
+
const s0 = ISetMapped.create(
|
|
53
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
54
|
+
toKey,
|
|
55
|
+
fromKey,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
expect(s0.has({ v: 4 })).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
test('case 3', () => {
|
|
61
|
+
const s0 = ISetMapped.create<Readonly<{ v: number }>, number>(
|
|
62
|
+
[],
|
|
63
|
+
toKey,
|
|
64
|
+
fromKey,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
expect(s0.has({ v: 3 })).toBe(false);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('ISetMapped.add', () => {
|
|
72
|
+
test('case 1', () => {
|
|
73
|
+
const s0 = ISetMapped.create(
|
|
74
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
75
|
+
toKey,
|
|
76
|
+
fromKey,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
expect(s0.add({ v: 5 })).toStrictEqual(
|
|
80
|
+
ISetMapped.create(
|
|
81
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }, { v: 5 }],
|
|
82
|
+
toKey,
|
|
83
|
+
fromKey,
|
|
84
|
+
),
|
|
85
|
+
);
|
|
86
|
+
expect(s0).toStrictEqual(
|
|
87
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
test('case 2', () => {
|
|
91
|
+
const s0 = ISetMapped.create(
|
|
92
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
93
|
+
toKey,
|
|
94
|
+
fromKey,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
expect(s0.add({ v: 3 })).toStrictEqual(
|
|
98
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
99
|
+
);
|
|
100
|
+
expect(s0).toStrictEqual(
|
|
101
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
102
|
+
);
|
|
103
|
+
});
|
|
104
|
+
test('case 3', () => {
|
|
105
|
+
const s0 = ISetMapped.create([], toKey, fromKey);
|
|
106
|
+
|
|
107
|
+
expect(s0.add({ v: 1 })).toStrictEqual(
|
|
108
|
+
ISetMapped.create([{ v: 1 }], toKey, fromKey),
|
|
109
|
+
);
|
|
110
|
+
expect(s0).toStrictEqual(ISetMapped.create([], toKey, fromKey));
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('ISetMapped.delete', () => {
|
|
115
|
+
test('case 1', () => {
|
|
116
|
+
const s0 = ISetMapped.create(
|
|
117
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
118
|
+
toKey,
|
|
119
|
+
fromKey,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
expect(s0.delete({ v: 10 })).toStrictEqual(
|
|
123
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
124
|
+
);
|
|
125
|
+
expect(s0).toStrictEqual(
|
|
126
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
test('case 2', () => {
|
|
130
|
+
const s0 = ISetMapped.create(
|
|
131
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
132
|
+
toKey,
|
|
133
|
+
fromKey,
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
expect(s0.delete({ v: 3 })).toStrictEqual(
|
|
137
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }], toKey, fromKey),
|
|
138
|
+
);
|
|
139
|
+
expect(s0).toStrictEqual(
|
|
140
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
test('case 3', () => {
|
|
144
|
+
const s0 = ISetMapped.create([], toKey, fromKey);
|
|
145
|
+
|
|
146
|
+
expect(s0.delete({ v: 1 })).toStrictEqual(
|
|
147
|
+
ISetMapped.create([], toKey, fromKey),
|
|
148
|
+
);
|
|
149
|
+
expect(s0).toStrictEqual(ISetMapped.create([], toKey, fromKey));
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('ISetMapped.forEach', () => {
|
|
154
|
+
test('case 1', () => {
|
|
155
|
+
const s0 = ISetMapped.create(
|
|
156
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
157
|
+
toKey,
|
|
158
|
+
fromKey,
|
|
159
|
+
);
|
|
160
|
+
const elements = [{ v: 1 }, { v: 2 }, { v: 3 }];
|
|
161
|
+
|
|
162
|
+
for (const el of s0) {
|
|
163
|
+
expect(elements).toContainEqual(el);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe('ISetMapped.keys', () => {
|
|
169
|
+
test('case 1', () => {
|
|
170
|
+
const s0 = ISetMapped.create(
|
|
171
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
172
|
+
toKey,
|
|
173
|
+
fromKey,
|
|
174
|
+
);
|
|
175
|
+
const elements = [{ v: 1 }, { v: 2 }, { v: 3 }];
|
|
176
|
+
|
|
177
|
+
for (const k of s0.keys()) {
|
|
178
|
+
expect(elements).toContainEqual(k);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe('ISetMapped.values', () => {
|
|
184
|
+
test('case 1', () => {
|
|
185
|
+
const s0 = ISetMapped.create(
|
|
186
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
187
|
+
toKey,
|
|
188
|
+
fromKey,
|
|
189
|
+
);
|
|
190
|
+
const elements = [{ v: 1 }, { v: 2 }, { v: 3 }];
|
|
191
|
+
|
|
192
|
+
for (const v of s0.values()) {
|
|
193
|
+
expect(elements).toContainEqual(v);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe('ISetMapped.entries', () => {
|
|
199
|
+
test('case 1', () => {
|
|
200
|
+
const s0 = ISetMapped.create(
|
|
201
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
202
|
+
toKey,
|
|
203
|
+
fromKey,
|
|
204
|
+
);
|
|
205
|
+
const elements = [{ v: 1 }, { v: 2 }, { v: 3 }];
|
|
206
|
+
|
|
207
|
+
for (const [k, v] of s0.entries()) {
|
|
208
|
+
expect(elements).toContainEqual(k);
|
|
209
|
+
expect(elements).toContainEqual(v);
|
|
210
|
+
expect(k).toStrictEqual(v);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
describe('ISetMapped.union', () => {
|
|
216
|
+
test('case 1', () => {
|
|
217
|
+
const s0 = ISetMapped.create(
|
|
218
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
219
|
+
toKey,
|
|
220
|
+
fromKey,
|
|
221
|
+
);
|
|
222
|
+
const s1 = ISetMapped.create(
|
|
223
|
+
[{ v: 3 }, { v: 4 }, { v: 5 }],
|
|
224
|
+
toKey,
|
|
225
|
+
fromKey,
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
expect(s0.union(s1)).toStrictEqual(
|
|
229
|
+
ISetMapped.create(
|
|
230
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }, { v: 4 }, { v: 5 }],
|
|
231
|
+
toKey,
|
|
232
|
+
fromKey,
|
|
233
|
+
),
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
test('case 2', () => {
|
|
237
|
+
const s0 = ISetMapped.create(
|
|
238
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
239
|
+
toKey,
|
|
240
|
+
fromKey,
|
|
241
|
+
);
|
|
242
|
+
const s1 = ISetMapped.create([], toKey, fromKey);
|
|
243
|
+
|
|
244
|
+
expect(s0.union(s1)).toStrictEqual(
|
|
245
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
246
|
+
);
|
|
247
|
+
});
|
|
248
|
+
test('case 3', () => {
|
|
249
|
+
const s0 = ISetMapped.create([], toKey, fromKey);
|
|
250
|
+
const s1 = ISetMapped.create(
|
|
251
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
252
|
+
toKey,
|
|
253
|
+
fromKey,
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
expect(s0.union(s1)).toStrictEqual(
|
|
257
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
258
|
+
);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
describe('ISetMapped.intersect', () => {
|
|
263
|
+
test('case 1', () => {
|
|
264
|
+
const s0 = ISetMapped.create(
|
|
265
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
266
|
+
toKey,
|
|
267
|
+
fromKey,
|
|
268
|
+
);
|
|
269
|
+
const s1 = ISetMapped.create(
|
|
270
|
+
[{ v: 2 }, { v: 3 }, { v: 4 }],
|
|
271
|
+
toKey,
|
|
272
|
+
fromKey,
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
expect(s0.intersect(s1)).toStrictEqual(
|
|
276
|
+
ISetMapped.create([{ v: 2 }, { v: 3 }], toKey, fromKey),
|
|
277
|
+
);
|
|
278
|
+
});
|
|
279
|
+
test('case 2', () => {
|
|
280
|
+
const s0 = ISetMapped.create(
|
|
281
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
282
|
+
toKey,
|
|
283
|
+
fromKey,
|
|
284
|
+
);
|
|
285
|
+
const s1 = ISetMapped.create([], toKey, fromKey);
|
|
286
|
+
|
|
287
|
+
expect(s0.intersect(s1)).toStrictEqual(
|
|
288
|
+
ISetMapped.create([], toKey, fromKey),
|
|
289
|
+
);
|
|
290
|
+
});
|
|
291
|
+
test('case 3', () => {
|
|
292
|
+
const s0 = ISetMapped.create([], toKey, fromKey);
|
|
293
|
+
const s1 = ISetMapped.create(
|
|
294
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
295
|
+
toKey,
|
|
296
|
+
fromKey,
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
expect(s0.intersect(s1)).toStrictEqual(
|
|
300
|
+
ISetMapped.create([], toKey, fromKey),
|
|
301
|
+
);
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
describe('ISetMapped.subtract', () => {
|
|
306
|
+
test('case 1', () => {
|
|
307
|
+
const s0 = ISetMapped.create(
|
|
308
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
309
|
+
toKey,
|
|
310
|
+
fromKey,
|
|
311
|
+
);
|
|
312
|
+
const s1 = ISetMapped.create([{ v: 2 }, { v: 4 }], toKey, fromKey);
|
|
313
|
+
|
|
314
|
+
expect(s0.subtract(s1)).toStrictEqual(
|
|
315
|
+
ISetMapped.create([{ v: 1 }, { v: 3 }], toKey, fromKey),
|
|
316
|
+
);
|
|
317
|
+
});
|
|
318
|
+
test('case 2', () => {
|
|
319
|
+
const s0 = ISetMapped.create(
|
|
320
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
321
|
+
toKey,
|
|
322
|
+
fromKey,
|
|
323
|
+
);
|
|
324
|
+
const s1 = ISetMapped.create([], toKey, fromKey);
|
|
325
|
+
|
|
326
|
+
expect(s0.subtract(s1)).toStrictEqual(
|
|
327
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
328
|
+
);
|
|
329
|
+
});
|
|
330
|
+
test('case 3', () => {
|
|
331
|
+
const s0 = ISetMapped.create([], toKey, fromKey);
|
|
332
|
+
const s1 = ISetMapped.create(
|
|
333
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
334
|
+
toKey,
|
|
335
|
+
fromKey,
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
expect(s0.subtract(s1)).toStrictEqual(
|
|
339
|
+
ISetMapped.create([], toKey, fromKey),
|
|
340
|
+
);
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
describe('ISetMapped.diff', () => {
|
|
345
|
+
test('case 1', () => {
|
|
346
|
+
const s0 = ISetMapped.create(
|
|
347
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
348
|
+
toKey,
|
|
349
|
+
fromKey,
|
|
350
|
+
);
|
|
351
|
+
const s1 = ISetMapped.create(
|
|
352
|
+
[{ v: 2 }, { v: 3 }, { v: 4 }],
|
|
353
|
+
toKey,
|
|
354
|
+
fromKey,
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
const diff = ISetMapped.diff(s0, s1);
|
|
358
|
+
|
|
359
|
+
expect(diff.deleted).toStrictEqual(
|
|
360
|
+
ISetMapped.create([{ v: 1 }], toKey, fromKey),
|
|
361
|
+
);
|
|
362
|
+
expect(diff.added).toStrictEqual(
|
|
363
|
+
ISetMapped.create([{ v: 4 }], toKey, fromKey),
|
|
364
|
+
);
|
|
365
|
+
});
|
|
366
|
+
test('case 2', () => {
|
|
367
|
+
const s0 = ISetMapped.create(
|
|
368
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
369
|
+
toKey,
|
|
370
|
+
fromKey,
|
|
371
|
+
);
|
|
372
|
+
const s1 = ISetMapped.create([], toKey, fromKey);
|
|
373
|
+
|
|
374
|
+
const diff = ISetMapped.diff(s0, s1);
|
|
375
|
+
|
|
376
|
+
expect(diff.deleted).toStrictEqual(
|
|
377
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
378
|
+
);
|
|
379
|
+
expect(diff.added).toStrictEqual(ISetMapped.create([], toKey, fromKey));
|
|
380
|
+
});
|
|
381
|
+
test('case 3', () => {
|
|
382
|
+
const s0 = ISetMapped.create([], toKey, fromKey);
|
|
383
|
+
const s1 = ISetMapped.create(
|
|
384
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
385
|
+
toKey,
|
|
386
|
+
fromKey,
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
const diff = ISetMapped.diff(s0, s1);
|
|
390
|
+
|
|
391
|
+
expect(diff.deleted).toStrictEqual(ISetMapped.create([], toKey, fromKey));
|
|
392
|
+
expect(diff.added).toStrictEqual(
|
|
393
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
394
|
+
);
|
|
395
|
+
});
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
describe('ISetMapped.union', () => {
|
|
399
|
+
test('case 1', () => {
|
|
400
|
+
const s0 = ISetMapped.create(
|
|
401
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
402
|
+
toKey,
|
|
403
|
+
fromKey,
|
|
404
|
+
);
|
|
405
|
+
const s1 = ISetMapped.create(
|
|
406
|
+
[{ v: 3 }, { v: 4 }, { v: 5 }],
|
|
407
|
+
toKey,
|
|
408
|
+
fromKey,
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
expect(ISetMapped.union(s0, s1)).toStrictEqual(
|
|
412
|
+
ISetMapped.create(
|
|
413
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }, { v: 4 }, { v: 5 }],
|
|
414
|
+
toKey,
|
|
415
|
+
fromKey,
|
|
416
|
+
),
|
|
417
|
+
);
|
|
418
|
+
});
|
|
419
|
+
test('case 2', () => {
|
|
420
|
+
const s0 = ISetMapped.create(
|
|
421
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
422
|
+
toKey,
|
|
423
|
+
fromKey,
|
|
424
|
+
);
|
|
425
|
+
const s1 = ISetMapped.create([], toKey, fromKey);
|
|
426
|
+
|
|
427
|
+
expect(ISetMapped.union(s0, s1)).toStrictEqual(
|
|
428
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
429
|
+
);
|
|
430
|
+
});
|
|
431
|
+
test('case 3', () => {
|
|
432
|
+
const s0 = ISetMapped.create([], toKey, fromKey);
|
|
433
|
+
const s1 = ISetMapped.create(
|
|
434
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
435
|
+
toKey,
|
|
436
|
+
fromKey,
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
expect(ISetMapped.union(s0, s1)).toStrictEqual(
|
|
440
|
+
ISetMapped.create([{ v: 1 }, { v: 2 }, { v: 3 }], toKey, fromKey),
|
|
441
|
+
);
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
describe('ISetMapped.intersection', () => {
|
|
446
|
+
test('case 1', () => {
|
|
447
|
+
const s0 = ISetMapped.create(
|
|
448
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
449
|
+
toKey,
|
|
450
|
+
fromKey,
|
|
451
|
+
);
|
|
452
|
+
const s1 = ISetMapped.create(
|
|
453
|
+
[{ v: 2 }, { v: 3 }, { v: 4 }],
|
|
454
|
+
toKey,
|
|
455
|
+
fromKey,
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
expect(ISetMapped.intersection(s0, s1)).toStrictEqual(
|
|
459
|
+
ISetMapped.create([{ v: 2 }, { v: 3 }], toKey, fromKey),
|
|
460
|
+
);
|
|
461
|
+
});
|
|
462
|
+
test('case 2', () => {
|
|
463
|
+
const s0 = ISetMapped.create(
|
|
464
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
465
|
+
toKey,
|
|
466
|
+
fromKey,
|
|
467
|
+
);
|
|
468
|
+
const s1 = ISetMapped.create([], toKey, fromKey);
|
|
469
|
+
|
|
470
|
+
expect(ISetMapped.intersection(s0, s1)).toStrictEqual(
|
|
471
|
+
ISetMapped.create([], toKey, fromKey),
|
|
472
|
+
);
|
|
473
|
+
});
|
|
474
|
+
test('case 3', () => {
|
|
475
|
+
const s0 = ISetMapped.create([], toKey, fromKey);
|
|
476
|
+
const s1 = ISetMapped.create(
|
|
477
|
+
[{ v: 1 }, { v: 2 }, { v: 3 }],
|
|
478
|
+
toKey,
|
|
479
|
+
fromKey,
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
expect(ISetMapped.intersection(s0, s1)).toStrictEqual(
|
|
483
|
+
ISetMapped.create([], toKey, fromKey),
|
|
484
|
+
);
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
describe('ISetMapped additional functionality with complex types', () => {
|
|
489
|
+
describe('ISetMapped.create', () => {
|
|
490
|
+
test('should create empty set', () => {
|
|
491
|
+
const set = ISetMapped.create<TestElement, string>(
|
|
492
|
+
[],
|
|
493
|
+
testElementToString,
|
|
494
|
+
stringToTestElement,
|
|
495
|
+
);
|
|
496
|
+
expect(set.size).toBe(0);
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
test('should create set with initial elements', () => {
|
|
500
|
+
const set = ISetMapped.create(
|
|
501
|
+
[
|
|
502
|
+
{ id: 1, type: 'user' },
|
|
503
|
+
{ id: 2, type: 'admin' },
|
|
504
|
+
],
|
|
505
|
+
testElementToString,
|
|
506
|
+
stringToTestElement,
|
|
507
|
+
);
|
|
508
|
+
expect(set.size).toBe(2);
|
|
509
|
+
expect(set.has({ id: 1, type: 'user' })).toBe(true);
|
|
510
|
+
expect(set.has({ id: 2, type: 'admin' })).toBe(true);
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
test('should handle duplicate elements based on mapping', () => {
|
|
514
|
+
const set = ISetMapped.create(
|
|
515
|
+
[
|
|
516
|
+
{ id: 1, type: 'user' },
|
|
517
|
+
{ id: 1, type: 'user' }, // Duplicate
|
|
518
|
+
{ id: 2, type: 'admin' },
|
|
519
|
+
],
|
|
520
|
+
testElementToString,
|
|
521
|
+
stringToTestElement,
|
|
522
|
+
);
|
|
523
|
+
expect(set.size).toBe(2);
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
describe('ISetMapped.equal', () => {
|
|
528
|
+
test('should return true for equal sets', () => {
|
|
529
|
+
const set1 = ISetMapped.create(
|
|
530
|
+
[
|
|
531
|
+
{ id: 1, type: 'user' },
|
|
532
|
+
{ id: 2, type: 'admin' },
|
|
533
|
+
],
|
|
534
|
+
testElementToString,
|
|
535
|
+
stringToTestElement,
|
|
536
|
+
);
|
|
537
|
+
const set2 = ISetMapped.create(
|
|
538
|
+
[
|
|
539
|
+
{ id: 2, type: 'admin' },
|
|
540
|
+
{ id: 1, type: 'user' },
|
|
541
|
+
],
|
|
542
|
+
testElementToString,
|
|
543
|
+
stringToTestElement,
|
|
544
|
+
);
|
|
545
|
+
expect(ISetMapped.equal(set1, set2)).toBe(true);
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
test('should return false for sets with different elements', () => {
|
|
549
|
+
const set1 = ISetMapped.create(
|
|
550
|
+
[
|
|
551
|
+
{ id: 1, type: 'user' },
|
|
552
|
+
{ id: 2, type: 'admin' },
|
|
553
|
+
],
|
|
554
|
+
testElementToString,
|
|
555
|
+
stringToTestElement,
|
|
556
|
+
);
|
|
557
|
+
const set2 = ISetMapped.create(
|
|
558
|
+
[
|
|
559
|
+
{ id: 1, type: 'user' },
|
|
560
|
+
{ id: 3, type: 'guest' },
|
|
561
|
+
],
|
|
562
|
+
testElementToString,
|
|
563
|
+
stringToTestElement,
|
|
564
|
+
);
|
|
565
|
+
expect(ISetMapped.equal(set1, set2)).toBe(false);
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
test('should return true for empty sets', () => {
|
|
569
|
+
const set1 = ISetMapped.create<TestElement, string>(
|
|
570
|
+
[],
|
|
571
|
+
testElementToString,
|
|
572
|
+
stringToTestElement,
|
|
573
|
+
);
|
|
574
|
+
const set2 = ISetMapped.create<TestElement, string>(
|
|
575
|
+
[],
|
|
576
|
+
testElementToString,
|
|
577
|
+
stringToTestElement,
|
|
578
|
+
);
|
|
579
|
+
expect(ISetMapped.equal(set1, set2)).toBe(true);
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
describe('diff method with complex types', () => {
|
|
584
|
+
test('should compute differences correctly', () => {
|
|
585
|
+
const oldSet = ISetMapped.create(
|
|
586
|
+
[
|
|
587
|
+
{ id: 1, type: 'user' },
|
|
588
|
+
{ id: 2, type: 'admin' },
|
|
589
|
+
{ id: 3, type: 'guest' },
|
|
590
|
+
],
|
|
591
|
+
testElementToString,
|
|
592
|
+
stringToTestElement,
|
|
593
|
+
);
|
|
594
|
+
const newSet = ISetMapped.create(
|
|
595
|
+
[
|
|
596
|
+
{ id: 2, type: 'admin' },
|
|
597
|
+
{ id: 3, type: 'guest' },
|
|
598
|
+
{ id: 4, type: 'user' },
|
|
599
|
+
],
|
|
600
|
+
testElementToString,
|
|
601
|
+
stringToTestElement,
|
|
602
|
+
);
|
|
603
|
+
|
|
604
|
+
const diff = ISetMapped.diff(oldSet, newSet);
|
|
605
|
+
|
|
606
|
+
expect(diff.deleted.size).toBe(1);
|
|
607
|
+
expect(diff.deleted.has({ id: 1, type: 'user' })).toBe(true);
|
|
608
|
+
|
|
609
|
+
expect(diff.added.size).toBe(1);
|
|
610
|
+
expect(diff.added.has({ id: 4, type: 'user' })).toBe(true);
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
test('should handle no changes', () => {
|
|
614
|
+
const set1 = ISetMapped.create(
|
|
615
|
+
[
|
|
616
|
+
{ id: 1, type: 'user' },
|
|
617
|
+
{ id: 2, type: 'admin' },
|
|
618
|
+
],
|
|
619
|
+
testElementToString,
|
|
620
|
+
stringToTestElement,
|
|
621
|
+
);
|
|
622
|
+
const set2 = ISetMapped.create(
|
|
623
|
+
[
|
|
624
|
+
{ id: 1, type: 'user' },
|
|
625
|
+
{ id: 2, type: 'admin' },
|
|
626
|
+
],
|
|
627
|
+
testElementToString,
|
|
628
|
+
stringToTestElement,
|
|
629
|
+
);
|
|
630
|
+
|
|
631
|
+
const diff = ISetMapped.diff(set1, set2);
|
|
632
|
+
|
|
633
|
+
expect(diff.deleted.size).toBe(0);
|
|
634
|
+
expect(diff.added.size).toBe(0);
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
describe('intersection and union with complex types', () => {
|
|
639
|
+
test('should compute intersection correctly', () => {
|
|
640
|
+
const set1 = ISetMapped.create(
|
|
641
|
+
[
|
|
642
|
+
{ id: 1, type: 'user' },
|
|
643
|
+
{ id: 2, type: 'admin' },
|
|
644
|
+
{ id: 3, type: 'guest' },
|
|
645
|
+
],
|
|
646
|
+
testElementToString,
|
|
647
|
+
stringToTestElement,
|
|
648
|
+
);
|
|
649
|
+
const set2 = ISetMapped.create(
|
|
650
|
+
[
|
|
651
|
+
{ id: 2, type: 'admin' },
|
|
652
|
+
{ id: 3, type: 'guest' },
|
|
653
|
+
{ id: 4, type: 'user' },
|
|
654
|
+
],
|
|
655
|
+
testElementToString,
|
|
656
|
+
stringToTestElement,
|
|
657
|
+
);
|
|
658
|
+
|
|
659
|
+
const intersection = ISetMapped.intersection(set1, set2);
|
|
660
|
+
|
|
661
|
+
expect(intersection.size).toBe(2);
|
|
662
|
+
expect(intersection.has({ id: 2, type: 'admin' })).toBe(true);
|
|
663
|
+
expect(intersection.has({ id: 3, type: 'guest' })).toBe(true);
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
test('should compute union correctly', () => {
|
|
667
|
+
const set1 = ISetMapped.create(
|
|
668
|
+
[
|
|
669
|
+
{ id: 1, type: 'user' },
|
|
670
|
+
{ id: 2, type: 'admin' },
|
|
671
|
+
],
|
|
672
|
+
testElementToString,
|
|
673
|
+
stringToTestElement,
|
|
674
|
+
);
|
|
675
|
+
const set2 = ISetMapped.create(
|
|
676
|
+
[
|
|
677
|
+
{ id: 3, type: 'guest' },
|
|
678
|
+
{ id: 4, type: 'user' },
|
|
679
|
+
],
|
|
680
|
+
testElementToString,
|
|
681
|
+
stringToTestElement,
|
|
682
|
+
);
|
|
683
|
+
|
|
684
|
+
const union = ISetMapped.union(set1, set2);
|
|
685
|
+
|
|
686
|
+
expect(union.size).toBe(4);
|
|
687
|
+
expect(union.has({ id: 1, type: 'user' })).toBe(true);
|
|
688
|
+
expect(union.has({ id: 2, type: 'admin' })).toBe(true);
|
|
689
|
+
expect(union.has({ id: 3, type: 'guest' })).toBe(true);
|
|
690
|
+
expect(union.has({ id: 4, type: 'user' })).toBe(true);
|
|
691
|
+
});
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
describe('every method', () => {
|
|
695
|
+
test('should return true when all elements satisfy predicate', () => {
|
|
696
|
+
const set = ISetMapped.create(
|
|
697
|
+
[
|
|
698
|
+
{ id: 1, type: 'user' },
|
|
699
|
+
{ id: 2, type: 'user' },
|
|
700
|
+
],
|
|
701
|
+
testElementToString,
|
|
702
|
+
stringToTestElement,
|
|
703
|
+
);
|
|
704
|
+
expect(set.every((el) => el.type === 'user')).toBe(true);
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
test('should return false when some elements do not satisfy predicate', () => {
|
|
708
|
+
const set = ISetMapped.create(
|
|
709
|
+
[
|
|
710
|
+
{ id: 1, type: 'user' },
|
|
711
|
+
{ id: 2, type: 'admin' },
|
|
712
|
+
],
|
|
713
|
+
testElementToString,
|
|
714
|
+
stringToTestElement,
|
|
715
|
+
);
|
|
716
|
+
expect(set.every((el) => el.type === 'user')).toBe(false);
|
|
717
|
+
});
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
describe('some method', () => {
|
|
721
|
+
test('should return true when at least one element satisfies predicate', () => {
|
|
722
|
+
const set = ISetMapped.create(
|
|
723
|
+
[
|
|
724
|
+
{ id: 1, type: 'user' },
|
|
725
|
+
{ id: 2, type: 'admin' },
|
|
726
|
+
],
|
|
727
|
+
testElementToString,
|
|
728
|
+
stringToTestElement,
|
|
729
|
+
);
|
|
730
|
+
expect(set.some((el) => el.type === 'admin')).toBe(true);
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
test('should return false when no elements satisfy predicate', () => {
|
|
734
|
+
const set = ISetMapped.create(
|
|
735
|
+
[
|
|
736
|
+
{ id: 1, type: 'user' },
|
|
737
|
+
{ id: 2, type: 'user' },
|
|
738
|
+
],
|
|
739
|
+
testElementToString,
|
|
740
|
+
stringToTestElement,
|
|
741
|
+
);
|
|
742
|
+
expect(set.some((el) => el.type === 'admin')).toBe(false);
|
|
743
|
+
});
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
describe('add method', () => {
|
|
747
|
+
test('should add new element', () => {
|
|
748
|
+
const set = ISetMapped.create(
|
|
749
|
+
[{ id: 1, type: 'user' }],
|
|
750
|
+
testElementToString,
|
|
751
|
+
stringToTestElement,
|
|
752
|
+
);
|
|
753
|
+
const updated = set.add({ id: 2, type: 'admin' });
|
|
754
|
+
|
|
755
|
+
expect(updated.size).toBe(2);
|
|
756
|
+
expect(updated.has({ id: 1, type: 'user' })).toBe(true);
|
|
757
|
+
expect(updated.has({ id: 2, type: 'admin' })).toBe(true);
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
test('should return same instance when adding existing element', () => {
|
|
761
|
+
const set = ISetMapped.create(
|
|
762
|
+
[{ id: 1, type: 'user' }],
|
|
763
|
+
testElementToString,
|
|
764
|
+
stringToTestElement,
|
|
765
|
+
);
|
|
766
|
+
const updated = set.add({ id: 1, type: 'user' });
|
|
767
|
+
|
|
768
|
+
expect(updated).toBe(set);
|
|
769
|
+
});
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
describe('delete method', () => {
|
|
773
|
+
test('should delete existing element', () => {
|
|
774
|
+
const set = ISetMapped.create(
|
|
775
|
+
[
|
|
776
|
+
{ id: 1, type: 'user' },
|
|
777
|
+
{ id: 2, type: 'admin' },
|
|
778
|
+
],
|
|
779
|
+
testElementToString,
|
|
780
|
+
stringToTestElement,
|
|
781
|
+
);
|
|
782
|
+
const updated = set.delete({ id: 1, type: 'user' });
|
|
783
|
+
|
|
784
|
+
expect(updated.size).toBe(1);
|
|
785
|
+
expect(updated.has({ id: 1, type: 'user' })).toBe(false);
|
|
786
|
+
expect(updated.has({ id: 2, type: 'admin' })).toBe(true);
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
test('should return same instance when deleting non-existent element', () => {
|
|
790
|
+
const set = ISetMapped.create(
|
|
791
|
+
[{ id: 1, type: 'user' }],
|
|
792
|
+
testElementToString,
|
|
793
|
+
stringToTestElement,
|
|
794
|
+
);
|
|
795
|
+
const updated = set.delete({ id: 2, type: 'admin' });
|
|
796
|
+
|
|
797
|
+
expect(updated).toBe(set);
|
|
798
|
+
});
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
describe('withMutations method', () => {
|
|
802
|
+
test('should apply multiple mutations', () => {
|
|
803
|
+
const set = ISetMapped.create<TestElement, string>(
|
|
804
|
+
[{ id: 1, type: 'user' }],
|
|
805
|
+
testElementToString,
|
|
806
|
+
stringToTestElement,
|
|
807
|
+
);
|
|
808
|
+
|
|
809
|
+
const updated = set.withMutations([
|
|
810
|
+
{ type: 'add', key: { id: 2, type: 'admin' } },
|
|
811
|
+
{ type: 'delete', key: { id: 1, type: 'user' } },
|
|
812
|
+
{ type: 'add', key: { id: 3, type: 'guest' } },
|
|
813
|
+
]);
|
|
814
|
+
|
|
815
|
+
expect(updated.size).toBe(2);
|
|
816
|
+
expect(updated.has({ id: 1, type: 'user' })).toBe(false);
|
|
817
|
+
expect(updated.has({ id: 2, type: 'admin' })).toBe(true);
|
|
818
|
+
expect(updated.has({ id: 3, type: 'guest' })).toBe(true);
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
test('should handle empty mutations array', () => {
|
|
822
|
+
const set = ISetMapped.create(
|
|
823
|
+
[{ id: 1, type: 'user' }],
|
|
824
|
+
testElementToString,
|
|
825
|
+
stringToTestElement,
|
|
826
|
+
);
|
|
827
|
+
const updated = set.withMutations([]);
|
|
828
|
+
expect(updated.size).toBe(1);
|
|
829
|
+
expect(ISetMapped.equal(set, updated)).toBe(true);
|
|
830
|
+
});
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
describe('map method', () => {
|
|
834
|
+
test('should transform elements', () => {
|
|
835
|
+
const set = ISetMapped.create(
|
|
836
|
+
[
|
|
837
|
+
{ id: 1, type: 'user' },
|
|
838
|
+
{ id: 2, type: 'admin' },
|
|
839
|
+
],
|
|
840
|
+
testElementToString,
|
|
841
|
+
stringToTestElement,
|
|
842
|
+
);
|
|
843
|
+
const transformed = set.map((el) => ({
|
|
844
|
+
...el,
|
|
845
|
+
type: el.type.toUpperCase(),
|
|
846
|
+
}));
|
|
847
|
+
|
|
848
|
+
expect(transformed.size).toBe(2);
|
|
849
|
+
expect(transformed.has({ id: 1, type: 'USER' })).toBe(true);
|
|
850
|
+
expect(transformed.has({ id: 2, type: 'ADMIN' })).toBe(true);
|
|
851
|
+
});
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
describe('filter method', () => {
|
|
855
|
+
test('should filter elements', () => {
|
|
856
|
+
const set = ISetMapped.create(
|
|
857
|
+
[
|
|
858
|
+
{ id: 1, type: 'user' },
|
|
859
|
+
{ id: 2, type: 'admin' },
|
|
860
|
+
{ id: 3, type: 'user' },
|
|
861
|
+
],
|
|
862
|
+
testElementToString,
|
|
863
|
+
stringToTestElement,
|
|
864
|
+
);
|
|
865
|
+
const filtered = set.filter((el) => el.type === 'user');
|
|
866
|
+
|
|
867
|
+
expect(filtered.size).toBe(2);
|
|
868
|
+
expect(filtered.has({ id: 1, type: 'user' })).toBe(true);
|
|
869
|
+
expect(filtered.has({ id: 3, type: 'user' })).toBe(true);
|
|
870
|
+
expect(filtered.has({ id: 2, type: 'admin' })).toBe(false);
|
|
871
|
+
});
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
describe('filterNot method', () => {
|
|
875
|
+
test('should filter out elements that satisfy predicate', () => {
|
|
876
|
+
const set = ISetMapped.create(
|
|
877
|
+
[
|
|
878
|
+
{ id: 1, type: 'user' },
|
|
879
|
+
{ id: 2, type: 'admin' },
|
|
880
|
+
{ id: 3, type: 'user' },
|
|
881
|
+
],
|
|
882
|
+
testElementToString,
|
|
883
|
+
stringToTestElement,
|
|
884
|
+
);
|
|
885
|
+
const filtered = set.filterNot((el) => el.type === 'user');
|
|
886
|
+
|
|
887
|
+
expect(filtered.size).toBe(1);
|
|
888
|
+
expect(filtered.has({ id: 2, type: 'admin' })).toBe(true);
|
|
889
|
+
expect(filtered.has({ id: 1, type: 'user' })).toBe(false);
|
|
890
|
+
expect(filtered.has({ id: 3, type: 'user' })).toBe(false);
|
|
891
|
+
});
|
|
892
|
+
});
|
|
893
|
+
|
|
894
|
+
describe('forEach method', () => {
|
|
895
|
+
test('should execute callback for each element', () => {
|
|
896
|
+
const set = ISetMapped.create(
|
|
897
|
+
[
|
|
898
|
+
{ id: 1, type: 'user' },
|
|
899
|
+
{ id: 2, type: 'admin' },
|
|
900
|
+
],
|
|
901
|
+
testElementToString,
|
|
902
|
+
stringToTestElement,
|
|
903
|
+
);
|
|
904
|
+
const collected: TestElement[] = [];
|
|
905
|
+
|
|
906
|
+
set.forEach((el) => {
|
|
907
|
+
collected.push(el);
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
expect(collected).toHaveLength(2);
|
|
911
|
+
expect(collected).toContainEqual({ id: 1, type: 'user' });
|
|
912
|
+
expect(collected).toContainEqual({ id: 2, type: 'admin' });
|
|
913
|
+
});
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
describe('isSubsetOf method', () => {
|
|
917
|
+
test('should return true for subset', () => {
|
|
918
|
+
const subset = ISetMapped.create(
|
|
919
|
+
[{ id: 1, type: 'user' }],
|
|
920
|
+
testElementToString,
|
|
921
|
+
stringToTestElement,
|
|
922
|
+
);
|
|
923
|
+
const superset = ISetMapped.create(
|
|
924
|
+
[
|
|
925
|
+
{ id: 1, type: 'user' },
|
|
926
|
+
{ id: 2, type: 'admin' },
|
|
927
|
+
],
|
|
928
|
+
testElementToString,
|
|
929
|
+
stringToTestElement,
|
|
930
|
+
);
|
|
931
|
+
|
|
932
|
+
expect(subset.isSubsetOf(superset)).toBe(true);
|
|
933
|
+
});
|
|
934
|
+
|
|
935
|
+
test('should return false for non-subset', () => {
|
|
936
|
+
const set1 = ISetMapped.create(
|
|
937
|
+
[
|
|
938
|
+
{ id: 1, type: 'user' },
|
|
939
|
+
{ id: 3, type: 'guest' },
|
|
940
|
+
],
|
|
941
|
+
testElementToString,
|
|
942
|
+
stringToTestElement,
|
|
943
|
+
);
|
|
944
|
+
const set2 = ISetMapped.create(
|
|
945
|
+
[
|
|
946
|
+
{ id: 1, type: 'user' },
|
|
947
|
+
{ id: 2, type: 'admin' },
|
|
948
|
+
],
|
|
949
|
+
testElementToString,
|
|
950
|
+
stringToTestElement,
|
|
951
|
+
);
|
|
952
|
+
|
|
953
|
+
expect(set1.isSubsetOf(set2)).toBe(false);
|
|
954
|
+
});
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
describe('isSupersetOf method', () => {
|
|
958
|
+
test('should return true for superset', () => {
|
|
959
|
+
const superset = ISetMapped.create(
|
|
960
|
+
[
|
|
961
|
+
{ id: 1, type: 'user' },
|
|
962
|
+
{ id: 2, type: 'admin' },
|
|
963
|
+
],
|
|
964
|
+
testElementToString,
|
|
965
|
+
stringToTestElement,
|
|
966
|
+
);
|
|
967
|
+
const subset = ISetMapped.create(
|
|
968
|
+
[{ id: 1, type: 'user' }],
|
|
969
|
+
testElementToString,
|
|
970
|
+
stringToTestElement,
|
|
971
|
+
);
|
|
972
|
+
|
|
973
|
+
expect(superset.isSupersetOf(subset)).toBe(true);
|
|
974
|
+
});
|
|
975
|
+
|
|
976
|
+
test('should return false for non-superset', () => {
|
|
977
|
+
const set1 = ISetMapped.create(
|
|
978
|
+
[
|
|
979
|
+
{ id: 1, type: 'user' },
|
|
980
|
+
{ id: 2, type: 'admin' },
|
|
981
|
+
],
|
|
982
|
+
testElementToString,
|
|
983
|
+
stringToTestElement,
|
|
984
|
+
);
|
|
985
|
+
const set2 = ISetMapped.create(
|
|
986
|
+
[
|
|
987
|
+
{ id: 1, type: 'user' },
|
|
988
|
+
{ id: 3, type: 'guest' },
|
|
989
|
+
],
|
|
990
|
+
testElementToString,
|
|
991
|
+
stringToTestElement,
|
|
992
|
+
);
|
|
993
|
+
|
|
994
|
+
expect(set1.isSupersetOf(set2)).toBe(false);
|
|
995
|
+
});
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
describe('subtract method', () => {
|
|
999
|
+
test('should return elements in first set but not in second', () => {
|
|
1000
|
+
const set1 = ISetMapped.create(
|
|
1001
|
+
[
|
|
1002
|
+
{ id: 1, type: 'user' },
|
|
1003
|
+
{ id: 2, type: 'admin' },
|
|
1004
|
+
{ id: 3, type: 'guest' },
|
|
1005
|
+
],
|
|
1006
|
+
testElementToString,
|
|
1007
|
+
stringToTestElement,
|
|
1008
|
+
);
|
|
1009
|
+
const set2 = ISetMapped.create(
|
|
1010
|
+
[
|
|
1011
|
+
{ id: 2, type: 'admin' },
|
|
1012
|
+
{ id: 4, type: 'user' },
|
|
1013
|
+
],
|
|
1014
|
+
testElementToString,
|
|
1015
|
+
stringToTestElement,
|
|
1016
|
+
);
|
|
1017
|
+
const result = set1.subtract(set2);
|
|
1018
|
+
|
|
1019
|
+
expect(result.size).toBe(2);
|
|
1020
|
+
expect(result.has({ id: 1, type: 'user' })).toBe(true);
|
|
1021
|
+
expect(result.has({ id: 3, type: 'guest' })).toBe(true);
|
|
1022
|
+
expect(result.has({ id: 2, type: 'admin' })).toBe(false);
|
|
1023
|
+
});
|
|
1024
|
+
});
|
|
1025
|
+
|
|
1026
|
+
describe('intersect method', () => {
|
|
1027
|
+
test('should return common elements', () => {
|
|
1028
|
+
const set1 = ISetMapped.create(
|
|
1029
|
+
[
|
|
1030
|
+
{ id: 1, type: 'user' },
|
|
1031
|
+
{ id: 2, type: 'admin' },
|
|
1032
|
+
{ id: 3, type: 'guest' },
|
|
1033
|
+
],
|
|
1034
|
+
testElementToString,
|
|
1035
|
+
stringToTestElement,
|
|
1036
|
+
);
|
|
1037
|
+
const set2 = ISetMapped.create(
|
|
1038
|
+
[
|
|
1039
|
+
{ id: 2, type: 'admin' },
|
|
1040
|
+
{ id: 3, type: 'guest' },
|
|
1041
|
+
{ id: 4, type: 'user' },
|
|
1042
|
+
],
|
|
1043
|
+
testElementToString,
|
|
1044
|
+
stringToTestElement,
|
|
1045
|
+
);
|
|
1046
|
+
const result = set1.intersect(set2);
|
|
1047
|
+
|
|
1048
|
+
expect(result.size).toBe(2);
|
|
1049
|
+
expect(result.has({ id: 2, type: 'admin' })).toBe(true);
|
|
1050
|
+
expect(result.has({ id: 3, type: 'guest' })).toBe(true);
|
|
1051
|
+
expect(result.has({ id: 1, type: 'user' })).toBe(false);
|
|
1052
|
+
});
|
|
1053
|
+
});
|
|
1054
|
+
|
|
1055
|
+
describe('union method', () => {
|
|
1056
|
+
test('should return combined elements', () => {
|
|
1057
|
+
const set1 = ISetMapped.create(
|
|
1058
|
+
[
|
|
1059
|
+
{ id: 1, type: 'user' },
|
|
1060
|
+
{ id: 2, type: 'admin' },
|
|
1061
|
+
],
|
|
1062
|
+
testElementToString,
|
|
1063
|
+
stringToTestElement,
|
|
1064
|
+
);
|
|
1065
|
+
const set2 = ISetMapped.create(
|
|
1066
|
+
[
|
|
1067
|
+
{ id: 3, type: 'guest' },
|
|
1068
|
+
{ id: 4, type: 'user' },
|
|
1069
|
+
],
|
|
1070
|
+
testElementToString,
|
|
1071
|
+
stringToTestElement,
|
|
1072
|
+
);
|
|
1073
|
+
const result = set1.union(set2);
|
|
1074
|
+
|
|
1075
|
+
expect(result.size).toBe(4);
|
|
1076
|
+
expect(result.has({ id: 1, type: 'user' })).toBe(true);
|
|
1077
|
+
expect(result.has({ id: 2, type: 'admin' })).toBe(true);
|
|
1078
|
+
expect(result.has({ id: 3, type: 'guest' })).toBe(true);
|
|
1079
|
+
expect(result.has({ id: 4, type: 'user' })).toBe(true);
|
|
1080
|
+
});
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
describe('iteration methods', () => {
|
|
1084
|
+
test('should work with keys()', () => {
|
|
1085
|
+
const set = ISetMapped.create(
|
|
1086
|
+
[
|
|
1087
|
+
{ id: 1, type: 'user' },
|
|
1088
|
+
{ id: 2, type: 'admin' },
|
|
1089
|
+
],
|
|
1090
|
+
testElementToString,
|
|
1091
|
+
stringToTestElement,
|
|
1092
|
+
);
|
|
1093
|
+
const keys = Array.from(set.keys());
|
|
1094
|
+
|
|
1095
|
+
expect(keys).toHaveLength(2);
|
|
1096
|
+
expect(keys).toContainEqual({ id: 1, type: 'user' });
|
|
1097
|
+
expect(keys).toContainEqual({ id: 2, type: 'admin' });
|
|
1098
|
+
});
|
|
1099
|
+
|
|
1100
|
+
test('should work with values()', () => {
|
|
1101
|
+
const set = ISetMapped.create(
|
|
1102
|
+
[
|
|
1103
|
+
{ id: 1, type: 'user' },
|
|
1104
|
+
{ id: 2, type: 'admin' },
|
|
1105
|
+
],
|
|
1106
|
+
testElementToString,
|
|
1107
|
+
stringToTestElement,
|
|
1108
|
+
);
|
|
1109
|
+
const values = Array.from(set.values());
|
|
1110
|
+
|
|
1111
|
+
expect(values).toHaveLength(2);
|
|
1112
|
+
expect(values).toContainEqual({ id: 1, type: 'user' });
|
|
1113
|
+
expect(values).toContainEqual({ id: 2, type: 'admin' });
|
|
1114
|
+
});
|
|
1115
|
+
|
|
1116
|
+
test('should work with entries()', () => {
|
|
1117
|
+
const set = ISetMapped.create(
|
|
1118
|
+
[
|
|
1119
|
+
{ id: 1, type: 'user' },
|
|
1120
|
+
{ id: 2, type: 'admin' },
|
|
1121
|
+
],
|
|
1122
|
+
testElementToString,
|
|
1123
|
+
stringToTestElement,
|
|
1124
|
+
);
|
|
1125
|
+
const entries = Array.from(set.entries());
|
|
1126
|
+
|
|
1127
|
+
expect(entries).toHaveLength(2);
|
|
1128
|
+
entries.forEach(([key, value]) => {
|
|
1129
|
+
expect(key).toStrictEqual(value);
|
|
1130
|
+
});
|
|
1131
|
+
});
|
|
1132
|
+
|
|
1133
|
+
test('should work with for-of loop', () => {
|
|
1134
|
+
const set = ISetMapped.create(
|
|
1135
|
+
[
|
|
1136
|
+
{ id: 1, type: 'user' },
|
|
1137
|
+
{ id: 2, type: 'admin' },
|
|
1138
|
+
],
|
|
1139
|
+
testElementToString,
|
|
1140
|
+
stringToTestElement,
|
|
1141
|
+
);
|
|
1142
|
+
const collected: TestElement[] = [];
|
|
1143
|
+
|
|
1144
|
+
for (const element of set) {
|
|
1145
|
+
collected.push(element);
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
expect(collected).toHaveLength(2);
|
|
1149
|
+
expect(collected).toContainEqual({ id: 1, type: 'user' });
|
|
1150
|
+
expect(collected).toContainEqual({ id: 2, type: 'admin' });
|
|
1151
|
+
});
|
|
1152
|
+
});
|
|
1153
|
+
|
|
1154
|
+
describe('conversion methods', () => {
|
|
1155
|
+
test('should convert to array', () => {
|
|
1156
|
+
const set = ISetMapped.create(
|
|
1157
|
+
[
|
|
1158
|
+
{ id: 1, type: 'user' },
|
|
1159
|
+
{ id: 2, type: 'admin' },
|
|
1160
|
+
],
|
|
1161
|
+
testElementToString,
|
|
1162
|
+
stringToTestElement,
|
|
1163
|
+
);
|
|
1164
|
+
const array = set.toArray();
|
|
1165
|
+
|
|
1166
|
+
expect(array).toHaveLength(2);
|
|
1167
|
+
expect(array).toContainEqual({ id: 1, type: 'user' });
|
|
1168
|
+
expect(array).toContainEqual({ id: 2, type: 'admin' });
|
|
1169
|
+
});
|
|
1170
|
+
|
|
1171
|
+
test('should convert to raw set', () => {
|
|
1172
|
+
const set = ISetMapped.create(
|
|
1173
|
+
[
|
|
1174
|
+
{ id: 1, type: 'user' },
|
|
1175
|
+
{ id: 2, type: 'admin' },
|
|
1176
|
+
],
|
|
1177
|
+
testElementToString,
|
|
1178
|
+
stringToTestElement,
|
|
1179
|
+
);
|
|
1180
|
+
const rawSet = set.toRawSet();
|
|
1181
|
+
|
|
1182
|
+
expect(rawSet.size).toBe(2);
|
|
1183
|
+
expect(rawSet.has('user_1')).toBe(true);
|
|
1184
|
+
expect(rawSet.has('admin_2')).toBe(true);
|
|
1185
|
+
});
|
|
1186
|
+
});
|
|
1187
|
+
});
|