ts-data-forge 2.1.1 → 2.1.3
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/README.md +13 -15
- package/dist/array/array-utils.d.mts.map +1 -1
- package/dist/array/array-utils.mjs +9 -13
- package/dist/array/array-utils.mjs.map +1 -1
- package/dist/collections/imap.d.mts +1 -1
- package/dist/collections/imap.d.mts.map +1 -1
- package/dist/collections/imap.mjs +5 -5
- package/dist/collections/imap.mjs.map +1 -1
- package/dist/collections/iset-mapped.d.mts +1 -1
- package/dist/collections/iset-mapped.mjs +1 -1
- package/dist/collections/iset.d.mts +2 -2
- package/dist/collections/iset.mjs +4 -5
- package/dist/collections/iset.mjs.map +1 -1
- package/dist/collections/queue.d.mts +1 -1
- package/dist/collections/queue.d.mts.map +1 -1
- package/dist/collections/queue.mjs +7 -8
- package/dist/collections/queue.mjs.map +1 -1
- package/dist/collections/stack.d.mts +1 -1
- package/dist/collections/stack.d.mts.map +1 -1
- package/dist/collections/stack.mjs +6 -7
- package/dist/collections/stack.mjs.map +1 -1
- package/dist/entry-point.d.mts.map +1 -1
- package/dist/functional/match.d.mts.map +1 -1
- package/dist/functional/match.mjs +1 -6
- package/dist/functional/match.mjs.map +1 -1
- package/dist/functional/optional.d.mts.map +1 -1
- package/dist/functional/optional.mjs +6 -4
- package/dist/functional/optional.mjs.map +1 -1
- package/dist/functional/result.d.mts.map +1 -1
- package/dist/functional/result.mjs +1 -6
- package/dist/functional/result.mjs.map +1 -1
- package/dist/json/json.d.mts.map +1 -1
- package/dist/json/json.mjs +3 -3
- package/dist/json/json.mjs.map +1 -1
- package/dist/others/memoize-function.d.mts +1 -1
- package/dist/others/memoize-function.mjs +1 -1
- package/dist/others/unknown-to-string.d.mts +44 -77
- package/dist/others/unknown-to-string.d.mts.map +1 -1
- package/dist/others/unknown-to-string.mjs +51 -85
- package/dist/others/unknown-to-string.mjs.map +1 -1
- package/package.json +10 -1
- package/src/array/array-utils-creation.test.mts +9 -8
- package/src/array/array-utils-overload-type-error.test.mts +6 -2
- package/src/array/array-utils-search.test.mts +1 -1
- package/src/array/array-utils-slicing.test.mts +0 -42
- package/src/array/array-utils-transformation.test.mts +11 -186
- package/src/array/array-utils-validation.test.mts +27 -27
- package/src/array/array-utils.mts +8 -12
- package/src/collections/imap-mapped.test.mts +15 -11
- package/src/collections/imap.mts +6 -13
- package/src/collections/imap.test.mts +20 -19
- package/src/collections/iset-mapped.mts +1 -1
- package/src/collections/iset-mapped.test.mts +14 -104
- package/src/collections/iset.mts +5 -7
- package/src/collections/iset.test.mts +43 -34
- package/src/collections/queue.mts +12 -10
- package/src/collections/queue.test.mts +46 -44
- package/src/collections/stack.mts +10 -9
- package/src/collections/stack.test.mts +12 -10
- package/src/entry-point.mts +1 -0
- package/src/functional/match.mts +1 -5
- package/src/functional/optional.mts +6 -6
- package/src/functional/optional.test.mts +7 -7
- package/src/functional/result.mts +1 -5
- package/src/guard/is-non-empty-string.test.mts +1 -1
- package/src/guard/is-non-null-object.test.mts +3 -3
- package/src/guard/is-primitive.test.mts +6 -6
- package/src/guard/is-type.test.mts +8 -12
- package/src/iterator/range.test.mts +1 -1
- package/src/json/json.mts +4 -11
- package/src/json/json.test.mts +25 -28
- package/src/number/branded-types/finite-number.test.mts +2 -1
- package/src/number/branded-types/int.test.mts +2 -1
- package/src/number/branded-types/int16.test.mts +3 -2
- package/src/number/branded-types/int32.test.mts +3 -2
- package/src/number/branded-types/non-negative-finite-number.test.mts +3 -2
- package/src/number/branded-types/non-negative-int16.test.mts +3 -2
- package/src/number/branded-types/non-negative-int32.test.mts +3 -2
- package/src/number/branded-types/non-zero-finite-number.test.mts +4 -3
- package/src/number/branded-types/non-zero-int.test.mts +4 -3
- package/src/number/branded-types/non-zero-int16.test.mts +3 -2
- package/src/number/branded-types/non-zero-int32.test.mts +3 -2
- package/src/number/branded-types/non-zero-safe-int.test.mts +4 -3
- package/src/number/branded-types/non-zero-uint16.test.mts +3 -2
- package/src/number/branded-types/non-zero-uint32.test.mts +3 -2
- package/src/number/branded-types/positive-finite-number.test.mts +3 -2
- package/src/number/branded-types/positive-int.test.mts +3 -2
- package/src/number/branded-types/positive-int16.test.mts +3 -2
- package/src/number/branded-types/positive-int32.test.mts +3 -2
- package/src/number/branded-types/positive-safe-int.test.mts +3 -2
- package/src/number/branded-types/positive-uint16.test.mts +3 -2
- package/src/number/branded-types/positive-uint32.test.mts +3 -2
- package/src/number/branded-types/safe-int.test.mts +3 -2
- package/src/number/branded-types/safe-uint.test.mts +3 -2
- package/src/number/branded-types/uint.test.mts +3 -2
- package/src/number/branded-types/uint16.test.mts +3 -2
- package/src/number/branded-types/uint32.test.mts +3 -2
- package/src/object/object.test.mts +10 -10
- package/src/others/cast-readonly.test.mts +8 -8
- package/src/others/memoize-function.mts +1 -1
- package/src/others/memoize-function.test.mts +2 -2
- package/src/others/unknown-to-string.mts +52 -87
- package/src/others/unknown-to-string.test.mts +26 -58
|
@@ -25,15 +25,15 @@ describe('IMap[Symbol.iterator]', () => {
|
|
|
25
25
|
['a', 1],
|
|
26
26
|
['b', 2],
|
|
27
27
|
]);
|
|
28
|
-
const
|
|
28
|
+
const mut_collected: (readonly [string, number])[] = [];
|
|
29
29
|
|
|
30
30
|
for (const entry of map) {
|
|
31
|
-
|
|
31
|
+
mut_collected.push(entry);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
expect(
|
|
35
|
-
expect(
|
|
36
|
-
expect(
|
|
34
|
+
expect(mut_collected).toHaveLength(2);
|
|
35
|
+
expect(mut_collected).toContainEqual(['a', 1]);
|
|
36
|
+
expect(mut_collected).toContainEqual(['b', 2]);
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
test('should work with spread operator', () => {
|
|
@@ -41,7 +41,7 @@ describe('IMap[Symbol.iterator]', () => {
|
|
|
41
41
|
['a', 1],
|
|
42
42
|
['b', 2],
|
|
43
43
|
]);
|
|
44
|
-
const entries =
|
|
44
|
+
const entries = Array.from(map);
|
|
45
45
|
|
|
46
46
|
expect(entries).toHaveLength(2);
|
|
47
47
|
expect(entries).toContainEqual(['a', 1]);
|
|
@@ -282,8 +282,8 @@ describe('IMap.get', () => {
|
|
|
282
282
|
['undef', undefined],
|
|
283
283
|
['null', null],
|
|
284
284
|
]);
|
|
285
|
-
expect(Optional.unwrap(map.get('undef'))).
|
|
286
|
-
expect(Optional.unwrap(map.get('null'))).
|
|
285
|
+
expect(Optional.unwrap(map.get('undef'))).toBeUndefined();
|
|
286
|
+
expect(Optional.unwrap(map.get('null'))).toBeNull();
|
|
287
287
|
});
|
|
288
288
|
});
|
|
289
289
|
|
|
@@ -781,27 +781,28 @@ describe('IMap.forEach', () => {
|
|
|
781
781
|
['b', 2],
|
|
782
782
|
['c', 3],
|
|
783
783
|
]);
|
|
784
|
-
const
|
|
784
|
+
const mut_collected: [string, number][] = [];
|
|
785
785
|
|
|
786
|
-
map.
|
|
787
|
-
|
|
788
|
-
}
|
|
786
|
+
for (const [key, value] of map.entries()) {
|
|
787
|
+
mut_collected.push([key, value]);
|
|
788
|
+
}
|
|
789
789
|
|
|
790
|
-
expect(
|
|
791
|
-
expect(
|
|
792
|
-
expect(
|
|
793
|
-
expect(
|
|
790
|
+
expect(mut_collected).toHaveLength(3);
|
|
791
|
+
expect(mut_collected).toContainEqual(['a', 1]);
|
|
792
|
+
expect(mut_collected).toContainEqual(['b', 2]);
|
|
793
|
+
expect(mut_collected).toContainEqual(['c', 3]);
|
|
794
794
|
});
|
|
795
795
|
|
|
796
796
|
test('should work with empty map', () => {
|
|
797
797
|
const map = IMap.create<string, number>([]);
|
|
798
|
-
let
|
|
798
|
+
let mut_called = false;
|
|
799
799
|
|
|
800
|
+
// eslint-disable-next-line unicorn/no-array-for-each
|
|
800
801
|
map.forEach(() => {
|
|
801
|
-
|
|
802
|
+
mut_called = true;
|
|
802
803
|
});
|
|
803
804
|
|
|
804
|
-
expect(
|
|
805
|
+
expect(mut_called).toBe(false);
|
|
805
806
|
});
|
|
806
807
|
});
|
|
807
808
|
|
|
@@ -611,7 +611,7 @@ export namespace ISetMapped {
|
|
|
611
611
|
*
|
|
612
612
|
* const combinedFlags = ISetMapped.union(setA, setB);
|
|
613
613
|
* // The order might vary as sets are unordered internally.
|
|
614
|
-
* console.log(combinedFlags.toArray().map(f => f.flagName).
|
|
614
|
+
* console.log(combinedFlags.toArray().map(f => f.flagName).toSorted());
|
|
615
615
|
* // Output: ["betaFeature", "darkMode", "newUI"]
|
|
616
616
|
* ```
|
|
617
617
|
*/
|
|
@@ -212,96 +212,6 @@ describe('ISetMapped.entries', () => {
|
|
|
212
212
|
});
|
|
213
213
|
});
|
|
214
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
215
|
describe('ISetMapped.subtract', () => {
|
|
306
216
|
test('case 1', () => {
|
|
307
217
|
const s0 = ISetMapped.create(
|
|
@@ -901,15 +811,15 @@ describe('ISetMapped additional functionality with complex types', () => {
|
|
|
901
811
|
testElementToString,
|
|
902
812
|
stringToTestElement,
|
|
903
813
|
);
|
|
904
|
-
const
|
|
814
|
+
const mut_collected: TestElement[] = [];
|
|
905
815
|
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
}
|
|
816
|
+
for (const el of set) {
|
|
817
|
+
mut_collected.push(el);
|
|
818
|
+
}
|
|
909
819
|
|
|
910
|
-
expect(
|
|
911
|
-
expect(
|
|
912
|
-
expect(
|
|
820
|
+
expect(mut_collected).toHaveLength(2);
|
|
821
|
+
expect(mut_collected).toContainEqual({ id: 1, type: 'user' });
|
|
822
|
+
expect(mut_collected).toContainEqual({ id: 2, type: 'admin' });
|
|
913
823
|
});
|
|
914
824
|
});
|
|
915
825
|
|
|
@@ -1125,9 +1035,9 @@ describe('ISetMapped additional functionality with complex types', () => {
|
|
|
1125
1035
|
const entries = Array.from(set.entries());
|
|
1126
1036
|
|
|
1127
1037
|
expect(entries).toHaveLength(2);
|
|
1128
|
-
|
|
1038
|
+
for (const [key, value] of entries) {
|
|
1129
1039
|
expect(key).toStrictEqual(value);
|
|
1130
|
-
}
|
|
1040
|
+
}
|
|
1131
1041
|
});
|
|
1132
1042
|
|
|
1133
1043
|
test('should work with for-of loop', () => {
|
|
@@ -1139,15 +1049,15 @@ describe('ISetMapped additional functionality with complex types', () => {
|
|
|
1139
1049
|
testElementToString,
|
|
1140
1050
|
stringToTestElement,
|
|
1141
1051
|
);
|
|
1142
|
-
const
|
|
1052
|
+
const mut_collected: TestElement[] = [];
|
|
1143
1053
|
|
|
1144
1054
|
for (const element of set) {
|
|
1145
|
-
|
|
1055
|
+
mut_collected.push(element);
|
|
1146
1056
|
}
|
|
1147
1057
|
|
|
1148
|
-
expect(
|
|
1149
|
-
expect(
|
|
1150
|
-
expect(
|
|
1058
|
+
expect(mut_collected).toHaveLength(2);
|
|
1059
|
+
expect(mut_collected).toContainEqual({ id: 1, type: 'user' });
|
|
1060
|
+
expect(mut_collected).toContainEqual({ id: 2, type: 'admin' });
|
|
1151
1061
|
});
|
|
1152
1062
|
});
|
|
1153
1063
|
|
package/src/collections/iset.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
// No imports from functional needed anymore
|
|
2
2
|
import { asUint32 } from '../number/index.mjs';
|
|
3
|
-
import { unknownToString } from '../others/
|
|
3
|
+
import { unknownToString } from '../others/index.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Interface for an immutable set with O(1) lookup performance and set operation support.
|
|
@@ -258,7 +258,7 @@ export namespace ISet {
|
|
|
258
258
|
* .filter(x => x % 2 === 0) // Keep even numbers: 2, 4
|
|
259
259
|
* .add(6) // Add 6: 2, 4, 6
|
|
260
260
|
* .delete(2); // Remove 2: 4, 6
|
|
261
|
-
* console.log(processedNumbers.toArray().
|
|
261
|
+
* console.log(processedNumbers.toArray().toSorted()); // Output: [4, 6]
|
|
262
262
|
*
|
|
263
263
|
* // From generator function
|
|
264
264
|
* function* generatePrimes(): Generator<number> {
|
|
@@ -445,7 +445,7 @@ export namespace ISet {
|
|
|
445
445
|
* const rolePermissions = ISet.create(["write", "execute", "admin"]);
|
|
446
446
|
*
|
|
447
447
|
* const allPermissions = ISet.union(userPermissions, rolePermissions);
|
|
448
|
-
* console.log(allPermissions.toArray().
|
|
448
|
+
* console.log(allPermissions.toArray().toSorted());
|
|
449
449
|
* // Output: ["admin", "execute", "read", "write"]
|
|
450
450
|
*
|
|
451
451
|
* // Union with different types (type widening)
|
|
@@ -560,9 +560,7 @@ class ISetClass<K extends MapSetKeyType> implements ISet<K>, Iterable<K> {
|
|
|
560
560
|
if (!this.has(key)) {
|
|
561
561
|
if (this.#showNotFoundMessage) {
|
|
562
562
|
const keyStr = unknownToString(key);
|
|
563
|
-
console.warn(
|
|
564
|
-
`ISet.delete: key not found: ${Result.isOk(keyStr) ? keyStr.value : '<error converting key to string>'}`,
|
|
565
|
-
);
|
|
563
|
+
console.warn(`ISet.delete: key not found: ${keyStr}`);
|
|
566
564
|
}
|
|
567
565
|
return this;
|
|
568
566
|
}
|
|
@@ -325,7 +325,7 @@ describe('ISet.map', () => {
|
|
|
325
325
|
const set = ISet.create([1, 2, 3]);
|
|
326
326
|
const doubled = set.map((x) => x * 2);
|
|
327
327
|
|
|
328
|
-
expect(
|
|
328
|
+
expect(doubled.toArray().toSorted((a, b) => a - b)).toStrictEqual([
|
|
329
329
|
2, 4, 6,
|
|
330
330
|
]);
|
|
331
331
|
});
|
|
@@ -711,34 +711,35 @@ describe('ISet.forEach', () => {
|
|
|
711
711
|
(callback: (value: 1 | 2 | 3) => void) => void
|
|
712
712
|
>('<=');
|
|
713
713
|
|
|
714
|
-
const
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
}
|
|
714
|
+
const mut_result: (1 | 2 | 3)[] = [];
|
|
715
|
+
for (const x of s0) {
|
|
716
|
+
mut_result.push(x);
|
|
717
|
+
}
|
|
718
718
|
|
|
719
|
-
expect(
|
|
719
|
+
expect(mut_result.toSorted((a, b) => a - b)).toStrictEqual([1, 2, 3]);
|
|
720
720
|
});
|
|
721
721
|
|
|
722
722
|
test('should execute callback for each element', () => {
|
|
723
723
|
const set = ISet.create([1, 2, 3]);
|
|
724
|
-
const
|
|
724
|
+
const mut_collected: number[] = [];
|
|
725
725
|
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
}
|
|
726
|
+
for (const value of set) {
|
|
727
|
+
mut_collected.push(value);
|
|
728
|
+
}
|
|
729
729
|
|
|
730
|
-
expect(
|
|
730
|
+
expect(mut_collected.toSorted((a, b) => a - b)).toStrictEqual([1, 2, 3]);
|
|
731
731
|
});
|
|
732
732
|
|
|
733
733
|
test('should not call callback for empty set', () => {
|
|
734
734
|
const set = ISet.create<number>([]);
|
|
735
|
-
let
|
|
735
|
+
let mut_callCount = 0;
|
|
736
736
|
|
|
737
|
+
// eslint-disable-next-line unicorn/no-array-for-each
|
|
737
738
|
set.forEach(() => {
|
|
738
|
-
|
|
739
|
+
mut_callCount += 1;
|
|
739
740
|
});
|
|
740
741
|
|
|
741
|
-
expect(
|
|
742
|
+
expect(mut_callCount).toBe(0);
|
|
742
743
|
});
|
|
743
744
|
});
|
|
744
745
|
|
|
@@ -748,12 +749,12 @@ describe('ISet.keys', () => {
|
|
|
748
749
|
|
|
749
750
|
expectType<typeof s0.keys, () => IterableIterator<1 | 2 | 3>>('<=');
|
|
750
751
|
|
|
751
|
-
const
|
|
752
|
+
const mut_result: (1 | 2 | 3)[] = [];
|
|
752
753
|
for (const x of s0.keys()) {
|
|
753
|
-
|
|
754
|
+
mut_result.push(x);
|
|
754
755
|
}
|
|
755
756
|
|
|
756
|
-
expect(
|
|
757
|
+
expect(mut_result.toSorted((a, b) => a - b)).toStrictEqual([1, 2, 3]);
|
|
757
758
|
});
|
|
758
759
|
});
|
|
759
760
|
|
|
@@ -763,12 +764,12 @@ describe('ISet.values', () => {
|
|
|
763
764
|
|
|
764
765
|
expectType<typeof s0.values, () => IterableIterator<1 | 2 | 3>>('<=');
|
|
765
766
|
|
|
766
|
-
const
|
|
767
|
+
const mut_result: (1 | 2 | 3)[] = [];
|
|
767
768
|
for (const x of s0.values()) {
|
|
768
|
-
|
|
769
|
+
mut_result.push(x);
|
|
769
770
|
}
|
|
770
771
|
|
|
771
|
-
expect(
|
|
772
|
+
expect(mut_result.toSorted((a, b) => a - b)).toStrictEqual([1, 2, 3]);
|
|
772
773
|
});
|
|
773
774
|
});
|
|
774
775
|
|
|
@@ -781,12 +782,12 @@ describe('ISet.entries', () => {
|
|
|
781
782
|
() => IterableIterator<readonly [1 | 2 | 3, 1 | 2 | 3]>
|
|
782
783
|
>('<=');
|
|
783
784
|
|
|
784
|
-
const
|
|
785
|
+
const mut_result: [1 | 2 | 3, 1 | 2 | 3][] = [];
|
|
785
786
|
for (const x of s0.entries()) {
|
|
786
|
-
|
|
787
|
+
mut_result.push([x[0], x[1]]);
|
|
787
788
|
}
|
|
788
789
|
|
|
789
|
-
expect(
|
|
790
|
+
expect(mut_result.toSorted((a, b) => a[0] - b[0])).toStrictEqual([
|
|
790
791
|
[1, 1],
|
|
791
792
|
[2, 2],
|
|
792
793
|
[3, 3],
|
|
@@ -799,7 +800,9 @@ describe('ISet.toArray', () => {
|
|
|
799
800
|
const s0 = ISet.create([1, 2, 3] as const);
|
|
800
801
|
|
|
801
802
|
expectType<typeof s0.toArray, () => readonly (1 | 2 | 3)[]>('<=');
|
|
802
|
-
expect(
|
|
803
|
+
expect(Array.from(s0.toArray()).toSorted((a, b) => a - b)).toStrictEqual([
|
|
804
|
+
1, 2, 3,
|
|
805
|
+
]);
|
|
803
806
|
});
|
|
804
807
|
|
|
805
808
|
test('case 2', () => {
|
|
@@ -812,8 +815,10 @@ describe('ISet.toArray', () => {
|
|
|
812
815
|
const set = ISet.create([1, 3, 2]);
|
|
813
816
|
const array = set.toArray();
|
|
814
817
|
|
|
815
|
-
expect(array
|
|
816
|
-
expect(
|
|
818
|
+
expect(array).toHaveLength(3);
|
|
819
|
+
expect(Array.from(array).toSorted((a, b) => a - b)).toStrictEqual([
|
|
820
|
+
1, 2, 3,
|
|
821
|
+
]);
|
|
817
822
|
});
|
|
818
823
|
});
|
|
819
824
|
|
|
@@ -961,34 +966,38 @@ describe('ISet.withMutations', () => {
|
|
|
961
966
|
describe('iterable functionality', () => {
|
|
962
967
|
test('should work with for-of loops', () => {
|
|
963
968
|
const set = ISet.create([1, 2, 3]);
|
|
964
|
-
const
|
|
969
|
+
const mut_collected: number[] = [];
|
|
965
970
|
|
|
966
971
|
for (const value of set) {
|
|
967
|
-
|
|
972
|
+
mut_collected.push(value);
|
|
968
973
|
}
|
|
969
974
|
|
|
970
|
-
expect(
|
|
975
|
+
expect(mut_collected.toSorted((a, b) => a - b)).toStrictEqual([1, 2, 3]);
|
|
971
976
|
});
|
|
972
977
|
|
|
973
978
|
test('should work with spread operator', () => {
|
|
974
979
|
const set = ISet.create([1, 2, 3]);
|
|
975
|
-
const array =
|
|
980
|
+
const array = Array.from(set);
|
|
976
981
|
|
|
977
|
-
expect(
|
|
982
|
+
expect(Array.from(array).toSorted((a, b) => a - b)).toStrictEqual([
|
|
983
|
+
1, 2, 3,
|
|
984
|
+
]);
|
|
978
985
|
});
|
|
979
986
|
|
|
980
987
|
test('should work with Array.from', () => {
|
|
981
988
|
const set = ISet.create([1, 2, 3]);
|
|
982
989
|
const array = Array.from(set);
|
|
983
990
|
|
|
984
|
-
expect(
|
|
991
|
+
expect(Array.from(array).toSorted((a, b) => a - b)).toStrictEqual([
|
|
992
|
+
1, 2, 3,
|
|
993
|
+
]);
|
|
985
994
|
});
|
|
986
995
|
|
|
987
996
|
test('should work with destructuring', () => {
|
|
988
997
|
const set = ISet.create([1, 2]);
|
|
989
|
-
const values =
|
|
998
|
+
const values = Array.from(set);
|
|
990
999
|
|
|
991
|
-
expect(values.
|
|
1000
|
+
expect(values.toSorted((a, b) => a - b)).toStrictEqual([1, 2]);
|
|
992
1001
|
});
|
|
993
1002
|
});
|
|
994
1003
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Arr } from '../array/index.mjs';
|
|
2
1
|
import { Optional } from '../functional/index.mjs';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { range } from '../iterator/index.mjs';
|
|
3
|
+
import { asSafeUint, asUint32 } from '../number/index.mjs';
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Interface for a high-performance queue with FIFO (First-In, First-Out) behavior.
|
|
@@ -123,9 +122,11 @@ class QueueClass<T> implements Queue<T> {
|
|
|
123
122
|
Math.max(QueueClass.#INITIAL_CAPACITY, initialValues.length * 2),
|
|
124
123
|
);
|
|
125
124
|
|
|
126
|
-
this.#buffer =
|
|
127
|
-
|
|
125
|
+
this.#buffer = Array.from<unknown, T | undefined>(
|
|
126
|
+
{ length: initialCapacity },
|
|
127
|
+
() => undefined,
|
|
128
128
|
);
|
|
129
|
+
|
|
129
130
|
this.#head = 0;
|
|
130
131
|
this.#tail = 0;
|
|
131
132
|
this.#mut_size = 0;
|
|
@@ -234,7 +235,7 @@ class QueueClass<T> implements Queue<T> {
|
|
|
234
235
|
* // High-volume enqueueing (demonstrates amortized O(1) performance)
|
|
235
236
|
* const dataQueue = createQueue<number>();
|
|
236
237
|
*
|
|
237
|
-
* for (
|
|
238
|
+
* for (const i of range(1000000)) {
|
|
238
239
|
* dataQueue.enqueue(i); // Each operation is O(1) amortized
|
|
239
240
|
* }
|
|
240
241
|
*
|
|
@@ -259,12 +260,13 @@ class QueueClass<T> implements Queue<T> {
|
|
|
259
260
|
*/
|
|
260
261
|
#resize(): void {
|
|
261
262
|
const newCapacity = asUint32(this.#capacity * 2);
|
|
262
|
-
const newBuffer =
|
|
263
|
-
|
|
263
|
+
const newBuffer = Array.from<unknown, T | undefined>(
|
|
264
|
+
{ length: newCapacity },
|
|
265
|
+
() => undefined,
|
|
264
266
|
);
|
|
265
267
|
|
|
266
268
|
// Copy elements in order from head to tail
|
|
267
|
-
for (
|
|
269
|
+
for (const i of range(asSafeUint(this.#mut_size))) {
|
|
268
270
|
const sourceIndex = (this.#head + i) % this.#capacity;
|
|
269
271
|
newBuffer[i] = this.#buffer[sourceIndex];
|
|
270
272
|
}
|
|
@@ -329,7 +331,7 @@ class QueueClass<T> implements Queue<T> {
|
|
|
329
331
|
* const eventQueue = createQueue<Event>();
|
|
330
332
|
*
|
|
331
333
|
* // Simulate high-volume event ingestion
|
|
332
|
-
* for (
|
|
334
|
+
* for (const i of range(10000)) {
|
|
333
335
|
* eventQueue.enqueue({
|
|
334
336
|
* timestamp: Date.now(),
|
|
335
337
|
* type: `event-${i % 5}`,
|