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,889 @@
|
|
|
1
|
+
import { asUint32 } from '../number/index.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Interface for an immutable set with custom element mapping and O(1) membership testing.
|
|
5
|
+
*
|
|
6
|
+
* ISetMapped allows you to use complex objects as set elements by providing transformation functions
|
|
7
|
+
* that convert between your custom element type `K` and a primitive `MapSetKeyType` `KM` that can
|
|
8
|
+
* be efficiently stored in JavaScript's native Set. This enables high-performance set operations
|
|
9
|
+
* on complex elements while maintaining type safety and immutability.
|
|
10
|
+
*
|
|
11
|
+
* **Key Features:**
|
|
12
|
+
* - **Custom Element Types**: Use any type as set elements by providing `toKey`/`fromKey` functions
|
|
13
|
+
* - **O(1) Performance**: Maintains O(1) average-case performance for membership testing and mutations
|
|
14
|
+
* - **Immutable**: All operations return new instances, preserving immutability
|
|
15
|
+
* - **Set Operations**: Full support for union, intersection, difference, subset/superset checks
|
|
16
|
+
* - **Type Safe**: Full TypeScript support with generic element types
|
|
17
|
+
*
|
|
18
|
+
* **Performance Characteristics:**
|
|
19
|
+
* - has/add/delete: O(1) average case (plus element transformation overhead)
|
|
20
|
+
* - Set operations (union, intersection, difference): O(n)
|
|
21
|
+
* - map/filter operations: O(n)
|
|
22
|
+
* - Iteration: O(n) (plus element transformation overhead)
|
|
23
|
+
*
|
|
24
|
+
* @template K The type of the custom elements in the set.
|
|
25
|
+
* @template KM The type of the mapped primitive keys (string, number, etc.).
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // Example with complex object elements
|
|
30
|
+
* type User = { id: number; department: string; email: string };
|
|
31
|
+
*
|
|
32
|
+
* // Define transformation functions
|
|
33
|
+
* const userToKey = (user: User): string => `${user.department}:${user.id}`;
|
|
34
|
+
* const keyToUser = (key: string): User => {
|
|
35
|
+
* const [department, idStr] = key.split(':');
|
|
36
|
+
* // In practice, you might fetch from a cache or reconstruct more robustly
|
|
37
|
+
* return { id: Number(idStr), department, email: `user${idStr}@${department}.com` };
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* declare const activeUsers: ISetMapped<User, string>;
|
|
41
|
+
*
|
|
42
|
+
* // All operations work with the complex element type
|
|
43
|
+
* const user: User = { id: 123, department: "engineering", email: "alice@engineering.com" };
|
|
44
|
+
* const hasUser = activeUsers.has(user); // O(1)
|
|
45
|
+
* const withNewUser = activeUsers.add(user); // O(1) - returns new ISetMapped
|
|
46
|
+
* const withoutUser = activeUsers.delete(user); // O(1) - returns new ISetMapped
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
type ISetMappedInterface<K, KM extends MapSetKeyType> = Readonly<{
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new ISetMapped instance.
|
|
52
|
+
* @param iterable An iterable of elements.
|
|
53
|
+
* @param toKey A function that converts an element of type `K` to `KM`.
|
|
54
|
+
* @param fromKey A function that converts a mapped key of type `KM` back to `K`.
|
|
55
|
+
*/
|
|
56
|
+
new (iterable: Iterable<K>, toKey: (a: K) => KM, fromKey: (k: KM) => K): void;
|
|
57
|
+
|
|
58
|
+
// Getting information
|
|
59
|
+
/** The number of elements in the set. */
|
|
60
|
+
size: SizeType.Arr;
|
|
61
|
+
|
|
62
|
+
/** Checks if the set is empty. */
|
|
63
|
+
isEmpty: boolean;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Checks if an element exists in the set.
|
|
67
|
+
* @param key The element to check.
|
|
68
|
+
* @returns `true` if the element exists, `false` otherwise.
|
|
69
|
+
*/
|
|
70
|
+
has: (key: K) => boolean;
|
|
71
|
+
|
|
72
|
+
// Reducing a value
|
|
73
|
+
/**
|
|
74
|
+
* Checks if all elements in the set satisfy a predicate.
|
|
75
|
+
* @param predicate A function to test each element.
|
|
76
|
+
* @returns `true` if all elements satisfy the predicate, `false` otherwise.
|
|
77
|
+
*/
|
|
78
|
+
every: ((predicate: (key: K) => boolean) => boolean) &
|
|
79
|
+
/**
|
|
80
|
+
* Checks if all elements in the set satisfy a type predicate.
|
|
81
|
+
* Narrows the type of elements in the set if the predicate returns true for all elements.
|
|
82
|
+
* @template L The narrowed type of the elements.
|
|
83
|
+
* @param predicate A type predicate function.
|
|
84
|
+
* @returns `true` if all elements satisfy the predicate, `false` otherwise.
|
|
85
|
+
*/
|
|
86
|
+
(<L extends K>(
|
|
87
|
+
predicate: (key: K) => key is L,
|
|
88
|
+
) => this is ISetMapped<L, KM>);
|
|
89
|
+
/**
|
|
90
|
+
* Checks if at least one element in the set satisfies a predicate.
|
|
91
|
+
* @param predicate A function to test each element.
|
|
92
|
+
* @returns `true` if at least one element satisfies the predicate, `false` otherwise.
|
|
93
|
+
*/
|
|
94
|
+
some: (predicate: (key: K) => boolean) => boolean;
|
|
95
|
+
|
|
96
|
+
// Mutation
|
|
97
|
+
/**
|
|
98
|
+
* Adds an element to the set.
|
|
99
|
+
* @param key The element to add.
|
|
100
|
+
* @returns A new ISetMapped instance with the element added.
|
|
101
|
+
*/
|
|
102
|
+
add: (key: K) => ISetMapped<K, KM>;
|
|
103
|
+
/**
|
|
104
|
+
* Deletes an element from the set.
|
|
105
|
+
* @param key The element to delete.
|
|
106
|
+
* @returns A new ISetMapped instance without the specified element.
|
|
107
|
+
*/
|
|
108
|
+
delete: (key: K) => ISetMapped<K, KM>;
|
|
109
|
+
/**
|
|
110
|
+
* Applies a series of mutations to the set.
|
|
111
|
+
* @param actions An array of mutation actions (add or delete).
|
|
112
|
+
* @returns A new ISetMapped instance with all mutations applied.
|
|
113
|
+
*/
|
|
114
|
+
withMutations: (
|
|
115
|
+
actions: readonly Readonly<
|
|
116
|
+
{ type: 'add'; key: K } | { type: 'delete'; key: K }
|
|
117
|
+
>[],
|
|
118
|
+
) => ISetMapped<K, KM>;
|
|
119
|
+
|
|
120
|
+
// Sequence algorithms
|
|
121
|
+
/**
|
|
122
|
+
* Maps the elements of the set to new elements.
|
|
123
|
+
* Note: The element type `K` cannot be changed because `toKey` and `fromKey` would become unusable if the mapped type `KM` changes.
|
|
124
|
+
* @param mapFn A function that maps an element to a new element of the same type `K`.
|
|
125
|
+
* @returns A new ISetMapped instance with mapped elements.
|
|
126
|
+
*/
|
|
127
|
+
map: (mapFn: (key: K) => K) => ISetMapped<K, KM>;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Filters the elements of the set based on a predicate.
|
|
131
|
+
* @param predicate A function to test each element.
|
|
132
|
+
* @returns A new ISetMapped instance with elements that satisfy the predicate.
|
|
133
|
+
*/
|
|
134
|
+
filter: (predicate: (value: K) => boolean) => ISetMapped<K, KM>;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Filters the elements of the set by excluding elements for which the predicate returns true.
|
|
138
|
+
* @param predicate A function to test each element.
|
|
139
|
+
* @returns A new ISetMapped instance with elements for which the predicate returned `false`.
|
|
140
|
+
*/
|
|
141
|
+
filterNot: (predicate: (key: K) => boolean) => ISetMapped<K, KM>;
|
|
142
|
+
|
|
143
|
+
// Side effects
|
|
144
|
+
/**
|
|
145
|
+
* Executes a callback function for each element in the set.
|
|
146
|
+
* @param callbackfn A function to execute for each element.
|
|
147
|
+
*/
|
|
148
|
+
forEach: (callbackfn: (key: K) => void) => void;
|
|
149
|
+
|
|
150
|
+
// Comparison
|
|
151
|
+
/**
|
|
152
|
+
* Checks if this set is a subset of another set.
|
|
153
|
+
* @param set The other set.
|
|
154
|
+
* @returns `true` if this set is a subset of the other set, `false` otherwise.
|
|
155
|
+
*/
|
|
156
|
+
isSubsetOf: (set: ISetMapped<K, KM>) => boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Checks if this set is a superset of another set.
|
|
159
|
+
* @param set The other set.
|
|
160
|
+
* @returns `true` if this set is a superset of the other set, `false` otherwise.
|
|
161
|
+
*/
|
|
162
|
+
isSupersetOf: (set: ISetMapped<K, KM>) => boolean;
|
|
163
|
+
/**
|
|
164
|
+
* Returns a new set with elements that are in this set but not in another set.
|
|
165
|
+
* @param set The other set.
|
|
166
|
+
* @returns A new ISetMapped instance representing the set difference.
|
|
167
|
+
*/
|
|
168
|
+
subtract: (set: ISetMapped<K, KM>) => ISetMapped<K, KM>;
|
|
169
|
+
/**
|
|
170
|
+
* Returns a new set with elements that are common to both this set and another set.
|
|
171
|
+
* @param set The other set.
|
|
172
|
+
* @returns A new ISetMapped instance representing the set intersection.
|
|
173
|
+
*/
|
|
174
|
+
intersect: (set: ISetMapped<K, KM>) => ISetMapped<K, KM>;
|
|
175
|
+
/**
|
|
176
|
+
* Returns a new set with all elements from both this set and another set.
|
|
177
|
+
* @param set The other set.
|
|
178
|
+
* @returns A new ISetMapped instance representing the set union.
|
|
179
|
+
*/
|
|
180
|
+
union: (set: ISetMapped<K, KM>) => ISetMapped<K, KM>;
|
|
181
|
+
|
|
182
|
+
// Iterators
|
|
183
|
+
/**
|
|
184
|
+
* Returns an iterator for the elements in the set (alias for values).
|
|
185
|
+
* @returns An iterable iterator of elements.
|
|
186
|
+
*/
|
|
187
|
+
keys: () => IterableIterator<K>;
|
|
188
|
+
/**
|
|
189
|
+
* Returns an iterator for the elements in the set.
|
|
190
|
+
* @returns An iterable iterator of elements.
|
|
191
|
+
*/
|
|
192
|
+
values: () => IterableIterator<K>;
|
|
193
|
+
/**
|
|
194
|
+
* Returns an iterator for the entries (element-element pairs) in the set.
|
|
195
|
+
* @returns An iterable iterator of entries.
|
|
196
|
+
*/
|
|
197
|
+
entries: () => IterableIterator<readonly [K, K]>;
|
|
198
|
+
|
|
199
|
+
// Conversion
|
|
200
|
+
/**
|
|
201
|
+
* Converts the elements of the set to an array.
|
|
202
|
+
* @returns A readonly array of elements.
|
|
203
|
+
*/
|
|
204
|
+
toArray: () => readonly K[];
|
|
205
|
+
/**
|
|
206
|
+
* Returns the underlying readonly JavaScript Set of mapped keys.
|
|
207
|
+
* @returns The raw ReadonlySet instance.
|
|
208
|
+
*/
|
|
209
|
+
toRawSet: () => ReadonlySet<KM>;
|
|
210
|
+
}>;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Represents an immutable set with custom element transformation and high-performance operations.
|
|
214
|
+
*
|
|
215
|
+
* ISetMapped is a specialized persistent data structure that enables using complex objects as set elements
|
|
216
|
+
* while maintaining the performance benefits of JavaScript's native Set. It achieves this by requiring
|
|
217
|
+
* bidirectional transformation functions that convert between your custom element type and a primitive type
|
|
218
|
+
* that can be efficiently stored and compared for uniqueness.
|
|
219
|
+
*
|
|
220
|
+
* **Key Features:**
|
|
221
|
+
* - **Complex Elements**: Use objects, arrays, or any custom type as set elements
|
|
222
|
+
* - **High Performance**: O(1) operations through efficient element transformation
|
|
223
|
+
* - **Immutable**: All mutation operations return new instances
|
|
224
|
+
* - **Type Safe**: Full TypeScript support with compile-time element type checking
|
|
225
|
+
* - **Bidirectional**: Maintains ability to reconstruct original elements from mapped keys
|
|
226
|
+
* - **Set Algebra**: Complete support for mathematical set operations
|
|
227
|
+
*
|
|
228
|
+
* **Use Cases:**
|
|
229
|
+
* - Sets of entities with complex identifiers
|
|
230
|
+
* - Deduplication of objects based on specific properties
|
|
231
|
+
* - Performance-critical sets with non-primitive elements
|
|
232
|
+
* - Mathematical set operations on complex data structures
|
|
233
|
+
*
|
|
234
|
+
* @template K The type of the custom elements in the set.
|
|
235
|
+
* @template KM The type of the mapped primitive keys (string, number, etc.).
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```typescript
|
|
239
|
+
* // Example: User management with composite identity
|
|
240
|
+
* type User = { id: number; department: string; username: string; email: string };
|
|
241
|
+
*
|
|
242
|
+
* // Define bidirectional transformation functions
|
|
243
|
+
* const userToKey = (user: User): string => `${user.department}:${user.id}`;
|
|
244
|
+
* const keyToUser = (key: string): User => {
|
|
245
|
+
* const [department, idStr] = key.split(':');
|
|
246
|
+
* const id = Number(idStr);
|
|
247
|
+
* // In practice, this might fetch from a user service or cache
|
|
248
|
+
* return {
|
|
249
|
+
* id,
|
|
250
|
+
* department,
|
|
251
|
+
* username: `user${id}`,
|
|
252
|
+
* email: `user${id}@${department}.company.com`
|
|
253
|
+
* };
|
|
254
|
+
* };
|
|
255
|
+
*
|
|
256
|
+
* // Create a set with complex elements
|
|
257
|
+
* let activeUsers = ISetMapped.create<User, string>([], userToKey, keyToUser);
|
|
258
|
+
*
|
|
259
|
+
* // Use complex objects as elements naturally
|
|
260
|
+
* const alice: User = { id: 1, department: "engineering", username: "alice", email: "alice@engineering.company.com" };
|
|
261
|
+
* const bob: User = { id: 2, department: "marketing", username: "bob", email: "bob@marketing.company.com" };
|
|
262
|
+
* const charlie: User = { id: 3, department: "engineering", username: "charlie", email: "charlie@engineering.company.com" };
|
|
263
|
+
*
|
|
264
|
+
* activeUsers = activeUsers
|
|
265
|
+
* .add(alice)
|
|
266
|
+
* .add(bob)
|
|
267
|
+
* .add(charlie);
|
|
268
|
+
*
|
|
269
|
+
* // All operations work with the original element type
|
|
270
|
+
* console.log(activeUsers.has(alice)); // Output: true
|
|
271
|
+
* console.log(activeUsers.size); // Output: 3
|
|
272
|
+
*
|
|
273
|
+
* // Set operations preserve element types
|
|
274
|
+
* const engineeringUsers = ISetMapped.create<User, string>([alice, charlie], userToKey, keyToUser);
|
|
275
|
+
* const marketingUsers = ISetMapped.create<User, string>([bob], userToKey, keyToUser);
|
|
276
|
+
*
|
|
277
|
+
* const allUsers = ISetMapped.union(engineeringUsers, marketingUsers);
|
|
278
|
+
* const engineeringOnly = activeUsers.intersect(engineeringUsers);
|
|
279
|
+
*
|
|
280
|
+
* // Iteration preserves original element types
|
|
281
|
+
* for (const user of engineeringOnly) {
|
|
282
|
+
* console.log(`${user.username} works in ${user.department}`);
|
|
283
|
+
* }
|
|
284
|
+
* // Output:
|
|
285
|
+
* // alice works in engineering
|
|
286
|
+
* // charlie works in engineering
|
|
287
|
+
*
|
|
288
|
+
* // Functional transformations work seamlessly
|
|
289
|
+
* const updatedUsers = activeUsers.map(user => ({
|
|
290
|
+
* ...user,
|
|
291
|
+
* email: user.email.replace('.company.com', '.example.com')
|
|
292
|
+
* }));
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
export type ISetMapped<K, KM extends MapSetKeyType> = Iterable<K> &
|
|
296
|
+
Readonly<ISetMappedInterface<K, KM>>;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Provides utility functions for ISetMapped.
|
|
300
|
+
*/
|
|
301
|
+
export namespace ISetMapped {
|
|
302
|
+
/**
|
|
303
|
+
* Creates a new ISetMapped instance with custom element transformation functions.
|
|
304
|
+
*
|
|
305
|
+
* This factory function creates an immutable set that can use complex objects as elements
|
|
306
|
+
* by providing bidirectional transformation functions. The `toKey` function converts
|
|
307
|
+
* your custom element type to a primitive type that can be efficiently stored, while
|
|
308
|
+
* `fromKey` reconstructs the original element type for iteration and access.
|
|
309
|
+
*
|
|
310
|
+
* **Performance:** O(n) where n is the number of elements in the iterable.
|
|
311
|
+
*
|
|
312
|
+
* @template K The type of the custom elements.
|
|
313
|
+
* @template KM The type of the mapped primitive keys.
|
|
314
|
+
* @param iterable An iterable of elements using the custom element type.
|
|
315
|
+
* @param toKey A function that converts a custom element `K` to a primitive key `KM`.
|
|
316
|
+
* This function must be deterministic and produce unique values for unique elements.
|
|
317
|
+
* @param fromKey A function that converts a primitive key `KM` back to the custom element `K`.
|
|
318
|
+
* This should be the inverse of `toKey`.
|
|
319
|
+
* @returns A new ISetMapped instance containing all unique elements from the iterable.
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
* ```typescript
|
|
323
|
+
* // Example 1: Product catalog with SKU-based identity
|
|
324
|
+
* type Product = { sku: string; name: string; price: number; category: string };
|
|
325
|
+
*
|
|
326
|
+
* const productToKey = (product: Product): string => product.sku;
|
|
327
|
+
* const keyToProduct = (sku: string): Product => {
|
|
328
|
+
* // In practice, this might fetch from a product service or cache
|
|
329
|
+
* return {
|
|
330
|
+
* sku,
|
|
331
|
+
* name: `Product ${sku}`,
|
|
332
|
+
* price: 0,
|
|
333
|
+
* category: "unknown"
|
|
334
|
+
* };
|
|
335
|
+
* };
|
|
336
|
+
*
|
|
337
|
+
* const productSet = ISetMapped.create<Product, string>(
|
|
338
|
+
* [
|
|
339
|
+
* { sku: "LAPTOP-001", name: "Gaming Laptop", price: 1299, category: "electronics" },
|
|
340
|
+
* { sku: "MOUSE-002", name: "Wireless Mouse", price: 49, category: "accessories" },
|
|
341
|
+
* { sku: "LAPTOP-001", name: "Gaming Laptop", price: 1299, category: "electronics" } // Duplicate SKU
|
|
342
|
+
* ],
|
|
343
|
+
* productToKey,
|
|
344
|
+
* keyToProduct
|
|
345
|
+
* );
|
|
346
|
+
*
|
|
347
|
+
* console.log(productSet.size); // Output: 2 (duplicate removed)
|
|
348
|
+
* console.log(productSet.has({ sku: "LAPTOP-001", name: "Gaming Laptop", price: 1299, category: "electronics" })); // true
|
|
349
|
+
*
|
|
350
|
+
* // Example 2: Geographic locations with coordinate-based identity
|
|
351
|
+
* type Location = { name: string; lat: number; lng: number; type: string };
|
|
352
|
+
*
|
|
353
|
+
* const locationToKey = (loc: Location): string => `${loc.lat.toFixed(6)},${loc.lng.toFixed(6)}`;
|
|
354
|
+
* const keyToLocation = (key: string): Location => {
|
|
355
|
+
* const [latStr, lngStr] = key.split(',');
|
|
356
|
+
* return {
|
|
357
|
+
* name: "Unknown Location",
|
|
358
|
+
* lat: parseFloat(latStr),
|
|
359
|
+
* lng: parseFloat(lngStr),
|
|
360
|
+
* type: "point"
|
|
361
|
+
* };
|
|
362
|
+
* };
|
|
363
|
+
*
|
|
364
|
+
* const locationSet = ISetMapped.create<Location, string>(
|
|
365
|
+
* [
|
|
366
|
+
* { name: "Statue of Liberty", lat: 40.689247, lng: -74.044502, type: "monument" },
|
|
367
|
+
* { name: "Empire State Building", lat: 40.748817, lng: -73.985428, type: "building" }
|
|
368
|
+
* ],
|
|
369
|
+
* locationToKey,
|
|
370
|
+
* keyToLocation
|
|
371
|
+
* );
|
|
372
|
+
*
|
|
373
|
+
* // Example 3: User entities with multi-part identity
|
|
374
|
+
* type User = { id: number; tenant: string; email: string; active: boolean };
|
|
375
|
+
*
|
|
376
|
+
* const userToKey = (user: User): string => `${user.tenant}:${user.id}`;
|
|
377
|
+
* const keyToUser = (key: string): User => {
|
|
378
|
+
* const [tenant, idStr] = key.split(':');
|
|
379
|
+
* return {
|
|
380
|
+
* id: Number(idStr),
|
|
381
|
+
* tenant,
|
|
382
|
+
* email: `user${idStr}@${tenant}.com`,
|
|
383
|
+
* active: true
|
|
384
|
+
* };
|
|
385
|
+
* };
|
|
386
|
+
*
|
|
387
|
+
* const userSet = ISetMapped.create<User, string>(
|
|
388
|
+
* [],
|
|
389
|
+
* userToKey,
|
|
390
|
+
* keyToUser
|
|
391
|
+
* )
|
|
392
|
+
* .add({ id: 1, tenant: "acme", email: "alice@acme.com", active: true })
|
|
393
|
+
* .add({ id: 2, tenant: "acme", email: "bob@acme.com", active: false });
|
|
394
|
+
*
|
|
395
|
+
* console.log(userSet.size); // Output: 2
|
|
396
|
+
*
|
|
397
|
+
* // Example 4: Empty set with type specification
|
|
398
|
+
* const emptyProductSet = ISetMapped.create<Product, string>(
|
|
399
|
+
* [],
|
|
400
|
+
* productToKey,
|
|
401
|
+
* keyToProduct
|
|
402
|
+
* );
|
|
403
|
+
* console.log(emptyProductSet.isEmpty); // Output: true
|
|
404
|
+
* ```
|
|
405
|
+
*/
|
|
406
|
+
export const create = <K, KM extends MapSetKeyType>(
|
|
407
|
+
iterable: Iterable<K>,
|
|
408
|
+
toKey: (a: K) => KM,
|
|
409
|
+
fromKey: (k: KM) => K,
|
|
410
|
+
): ISetMapped<K, KM> => new ISetMappedClass<K, KM>(iterable, toKey, fromKey);
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Checks if two ISetMapped instances are structurally equal.
|
|
414
|
+
*
|
|
415
|
+
* Two ISetMapped instances are considered equal if they have the same size and contain
|
|
416
|
+
* exactly the same elements. The comparison is performed on the underlying mapped keys,
|
|
417
|
+
* so the transformation functions themselves don't need to be identical. Elements are
|
|
418
|
+
* compared based on their mapped key representations.
|
|
419
|
+
*
|
|
420
|
+
* **Performance:** O(n) where n is the size of the smaller set.
|
|
421
|
+
*
|
|
422
|
+
* @template K The type of the custom elements.
|
|
423
|
+
* @template KM The type of the mapped primitive keys.
|
|
424
|
+
* @param a The first ISetMapped instance to compare.
|
|
425
|
+
* @param b The second ISetMapped instance to compare.
|
|
426
|
+
* @returns `true` if the sets contain exactly the same elements, `false` otherwise.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```typescript
|
|
430
|
+
* // Example with coordinate-based elements
|
|
431
|
+
* type Point = { x: number; y: number; label?: string };
|
|
432
|
+
* const pointToKey = (p: Point): string => `${p.x},${p.y}`;
|
|
433
|
+
* const keyToPoint = (s: string): Point => {
|
|
434
|
+
* const [x, y] = s.split(',').map(Number);
|
|
435
|
+
* return { x, y };
|
|
436
|
+
* };
|
|
437
|
+
*
|
|
438
|
+
* const set1 = ISetMapped.create<Point, string>(
|
|
439
|
+
* [{ x: 1, y: 2, label: "A" }, { x: 3, y: 4, label: "B" }],
|
|
440
|
+
* pointToKey,
|
|
441
|
+
* keyToPoint
|
|
442
|
+
* );
|
|
443
|
+
*
|
|
444
|
+
* const set2 = ISetMapped.create<Point, string>(
|
|
445
|
+
* [{ x: 3, y: 4, label: "Different" }, { x: 1, y: 2, label: "Labels" }], // Order doesn't matter
|
|
446
|
+
* pointToKey,
|
|
447
|
+
* keyToPoint
|
|
448
|
+
* );
|
|
449
|
+
*
|
|
450
|
+
* const set3 = ISetMapped.create<Point, string>(
|
|
451
|
+
* [{ x: 1, y: 2 }, { x: 5, y: 6 }], // Different point
|
|
452
|
+
* pointToKey,
|
|
453
|
+
* keyToPoint
|
|
454
|
+
* );
|
|
455
|
+
*
|
|
456
|
+
* console.log(ISetMapped.equal(set1, set2)); // true (same coordinates, labels don't affect equality)
|
|
457
|
+
* console.log(ISetMapped.equal(set1, set3)); // false (different coordinates)
|
|
458
|
+
*
|
|
459
|
+
* // Example with user entities
|
|
460
|
+
* type User = { id: number; department: string; name: string };
|
|
461
|
+
* const userToKey = (u: User): string => `${u.department}:${u.id}`;
|
|
462
|
+
* const keyToUser = (k: string): User => {
|
|
463
|
+
* const [department, idStr] = k.split(':');
|
|
464
|
+
* return { id: Number(idStr), department, name: "" };
|
|
465
|
+
* };
|
|
466
|
+
*
|
|
467
|
+
* const users1 = ISetMapped.create<User, string>(
|
|
468
|
+
* [
|
|
469
|
+
* { id: 1, department: "eng", name: "Alice" },
|
|
470
|
+
* { id: 2, department: "sales", name: "Bob" }
|
|
471
|
+
* ],
|
|
472
|
+
* userToKey,
|
|
473
|
+
* keyToUser
|
|
474
|
+
* );
|
|
475
|
+
*
|
|
476
|
+
* const users2 = ISetMapped.create<User, string>(
|
|
477
|
+
* [
|
|
478
|
+
* { id: 2, department: "sales", name: "Robert" }, // Different name, same identity
|
|
479
|
+
* { id: 1, department: "eng", name: "Alicia" } // Different name, same identity
|
|
480
|
+
* ],
|
|
481
|
+
* userToKey,
|
|
482
|
+
* keyToUser
|
|
483
|
+
* );
|
|
484
|
+
*
|
|
485
|
+
* console.log(ISetMapped.equal(users1, users2)); // true (same department:id combinations)
|
|
486
|
+
*
|
|
487
|
+
* // Empty sets
|
|
488
|
+
* const empty1 = ISetMapped.create<Point, string>([], pointToKey, keyToPoint);
|
|
489
|
+
* const empty2 = ISetMapped.create<Point, string>([], pointToKey, keyToPoint);
|
|
490
|
+
* console.log(ISetMapped.equal(empty1, empty2)); // true
|
|
491
|
+
*
|
|
492
|
+
* // Sets with different transformation functions but same logical content
|
|
493
|
+
* const alternativePointToKey = (p: Point): string => `(${p.x},${p.y})`; // Different format
|
|
494
|
+
* const alternativeKeyToPoint = (s: string): Point => {
|
|
495
|
+
* const match = s.match(/\((\d+),(\d+)\)/)!;
|
|
496
|
+
* return { x: Number(match[1]), y: Number(match[2]) };
|
|
497
|
+
* };
|
|
498
|
+
*
|
|
499
|
+
* const set4 = ISetMapped.create<Point, string>(
|
|
500
|
+
* [{ x: 1, y: 2 }, { x: 3, y: 4 }],
|
|
501
|
+
* alternativePointToKey,
|
|
502
|
+
* alternativeKeyToPoint
|
|
503
|
+
* );
|
|
504
|
+
*
|
|
505
|
+
* // This would be false because the underlying mapped keys are different
|
|
506
|
+
* console.log(ISetMapped.equal(set1, set4)); // false
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
509
|
+
export const equal = <K, KM extends MapSetKeyType>(
|
|
510
|
+
a: ISetMapped<K, KM>,
|
|
511
|
+
b: ISetMapped<K, KM>,
|
|
512
|
+
): boolean => a.size === b.size && a.every((e) => b.has(e));
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Computes the difference between two ISetMapped instances.
|
|
516
|
+
* @template K The type of the elements.
|
|
517
|
+
* @template KM The type of the mapped keys.
|
|
518
|
+
* @param oldSet The original set.
|
|
519
|
+
* @param newSet The new set.
|
|
520
|
+
* @returns An object containing sets of added and deleted elements.
|
|
521
|
+
* @example
|
|
522
|
+
* ```typescript
|
|
523
|
+
* type Tag = { name: string };
|
|
524
|
+
* const tagToKey = (t: Tag): string => t.name;
|
|
525
|
+
* const keyToTag = (name: string): Tag => ({ name });
|
|
526
|
+
*
|
|
527
|
+
* const oldTags = ISetMapped.create<Tag, string>(
|
|
528
|
+
* [{ name: "typescript" }, { name: "javascript" }],
|
|
529
|
+
* tagToKey,
|
|
530
|
+
* keyToTag
|
|
531
|
+
* );
|
|
532
|
+
* const newTags = ISetMapped.create<Tag, string>(
|
|
533
|
+
* [{ name: "javascript" }, { name: "react" }, { name: "nextjs" }],
|
|
534
|
+
* tagToKey,
|
|
535
|
+
* keyToTag
|
|
536
|
+
* );
|
|
537
|
+
*
|
|
538
|
+
* const diffResult = ISetMapped.diff(oldTags, newTags);
|
|
539
|
+
*
|
|
540
|
+
* console.log("Deleted tags:", diffResult.deleted.toArray().map(t => t.name));
|
|
541
|
+
* // Output: Deleted tags: ["typescript"]
|
|
542
|
+
*
|
|
543
|
+
* console.log("Added tags:", diffResult.added.toArray().map(t => t.name));
|
|
544
|
+
* // Output: Added tags: ["react", "nextjs"]
|
|
545
|
+
* ```
|
|
546
|
+
*/
|
|
547
|
+
export const diff = <K, KM extends MapSetKeyType>(
|
|
548
|
+
oldSet: ISetMapped<K, KM>,
|
|
549
|
+
newSet: ISetMapped<K, KM>,
|
|
550
|
+
): ReadonlyRecord<'added' | 'deleted', ISetMapped<K, KM>> => ({
|
|
551
|
+
deleted: oldSet.subtract(newSet),
|
|
552
|
+
added: newSet.subtract(oldSet),
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* Computes the intersection of two ISetMapped instances.
|
|
557
|
+
* @template K The type of the elements.
|
|
558
|
+
* @template KM The type of the mapped keys.
|
|
559
|
+
* @param a The first set.
|
|
560
|
+
* @param b The second set.
|
|
561
|
+
* @returns A new ISetMapped instance representing the intersection.
|
|
562
|
+
* @example
|
|
563
|
+
* ```typescript
|
|
564
|
+
* type Permission = { id: string };
|
|
565
|
+
* const permToKey = (p: Permission): string => p.id;
|
|
566
|
+
* const keyToPerm = (id: string): Permission => ({ id });
|
|
567
|
+
*
|
|
568
|
+
* const userPermissions = ISetMapped.create<Permission, string>(
|
|
569
|
+
* [{ id: "read" }, { id: "write" }, { id: "delete" }],
|
|
570
|
+
* permToKey,
|
|
571
|
+
* keyToPerm
|
|
572
|
+
* );
|
|
573
|
+
* const rolePermissions = ISetMapped.create<Permission, string>(
|
|
574
|
+
* [{ id: "read" }, { id: "comment" }, { id: "write" }],
|
|
575
|
+
* permToKey,
|
|
576
|
+
* keyToPerm
|
|
577
|
+
* );
|
|
578
|
+
*
|
|
579
|
+
* const commonPermissions = ISetMapped.intersection(userPermissions, rolePermissions);
|
|
580
|
+
* console.log(commonPermissions.toArray().map(p => p.id)); // Output: ["read", "write"]
|
|
581
|
+
* ```
|
|
582
|
+
*/
|
|
583
|
+
export const intersection = <K, KM extends MapSetKeyType>(
|
|
584
|
+
a: ISetMapped<K, KM>,
|
|
585
|
+
b: ISetMapped<K, KM>,
|
|
586
|
+
): ISetMapped<K, KM> => a.intersect(b);
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Computes the union of two ISetMapped instances.
|
|
590
|
+
* @template K The type of the elements.
|
|
591
|
+
* @template KM The type of the mapped keys.
|
|
592
|
+
* @param a The first set.
|
|
593
|
+
* @param b The second set.
|
|
594
|
+
* @returns A new ISetMapped instance representing the union.
|
|
595
|
+
* @example
|
|
596
|
+
* ```typescript
|
|
597
|
+
* type FeatureFlag = { flagName: string };
|
|
598
|
+
* const flagToKey = (f: FeatureFlag): string => f.flagName;
|
|
599
|
+
* const keyToFlag = (name: string): FeatureFlag => ({ flagName: name });
|
|
600
|
+
*
|
|
601
|
+
* const setA = ISetMapped.create<FeatureFlag, string>(
|
|
602
|
+
* [{ flagName: "newUI" }, { flagName: "betaFeature" }],
|
|
603
|
+
* flagToKey,
|
|
604
|
+
* keyToFlag
|
|
605
|
+
* );
|
|
606
|
+
* const setB = ISetMapped.create<FeatureFlag, string>(
|
|
607
|
+
* [{ flagName: "betaFeature" }, { flagName: "darkMode" }],
|
|
608
|
+
* flagToKey,
|
|
609
|
+
* keyToFlag
|
|
610
|
+
* );
|
|
611
|
+
*
|
|
612
|
+
* const combinedFlags = ISetMapped.union(setA, setB);
|
|
613
|
+
* // The order might vary as sets are unordered internally.
|
|
614
|
+
* console.log(combinedFlags.toArray().map(f => f.flagName).sort());
|
|
615
|
+
* // Output: ["betaFeature", "darkMode", "newUI"]
|
|
616
|
+
* ```
|
|
617
|
+
*/
|
|
618
|
+
export const union = <K, KM extends MapSetKeyType>(
|
|
619
|
+
a: ISetMapped<K, KM>,
|
|
620
|
+
b: ISetMapped<K, KM>,
|
|
621
|
+
): ISetMapped<K, KM> => a.union(b);
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Internal class implementation for ISetMapped providing immutable set operations with element transformation.
|
|
626
|
+
*
|
|
627
|
+
* This class implements the ISetMapped interface by maintaining a JavaScript Set with primitive keys
|
|
628
|
+
* internally while exposing an API that works with custom element types. The transformation between
|
|
629
|
+
* custom and primitive elements is handled transparently through the provided `toKey` and `fromKey` functions.
|
|
630
|
+
*
|
|
631
|
+
* **Implementation Details:**
|
|
632
|
+
* - Uses ReadonlySet<KM> internally where KM is the primitive key type
|
|
633
|
+
* - Stores transformation functions for bidirectional element conversion
|
|
634
|
+
* - Implements copy-on-write semantics for efficiency
|
|
635
|
+
* - Provides optional debug messaging for development
|
|
636
|
+
*
|
|
637
|
+
* @template K The type of the custom elements.
|
|
638
|
+
* @template KM The type of the mapped primitive keys.
|
|
639
|
+
* @implements ISetMapped
|
|
640
|
+
* @implements Iterable
|
|
641
|
+
* @internal This class should not be used directly. Use ISetMapped.create() instead.
|
|
642
|
+
*/
|
|
643
|
+
class ISetMappedClass<K, KM extends MapSetKeyType>
|
|
644
|
+
implements ISetMapped<K, KM>, Iterable<K>
|
|
645
|
+
{
|
|
646
|
+
readonly #set: ReadonlySet<KM>;
|
|
647
|
+
readonly #toKey: (a: K) => KM;
|
|
648
|
+
readonly #fromKey: (k: KM) => K;
|
|
649
|
+
readonly #showNotFoundMessage: boolean;
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Constructs an ISetMappedClass instance with custom element transformation.
|
|
653
|
+
*
|
|
654
|
+
* @param iterable An iterable of elements using the custom element type K.
|
|
655
|
+
* @param toKey A function that converts a custom element K to a primitive key KM.
|
|
656
|
+
* Must be deterministic and produce unique values for unique elements.
|
|
657
|
+
* @param fromKey A function that converts a primitive key KM back to the custom element K.
|
|
658
|
+
* Should be the inverse of the toKey function.
|
|
659
|
+
* @param showNotFoundMessage Whether to log warning messages when operations
|
|
660
|
+
* are performed on non-existent elements. Useful for debugging.
|
|
661
|
+
* Defaults to false for production use.
|
|
662
|
+
* @internal Use ISetMapped.create() instead of calling this constructor directly.
|
|
663
|
+
*/
|
|
664
|
+
constructor(
|
|
665
|
+
iterable: Iterable<K>,
|
|
666
|
+
toKey: (a: K) => KM,
|
|
667
|
+
fromKey: (k: KM) => K,
|
|
668
|
+
showNotFoundMessage: boolean = false,
|
|
669
|
+
) {
|
|
670
|
+
this.#set = new Set(Array.from(iterable, toKey));
|
|
671
|
+
this.#toKey = toKey;
|
|
672
|
+
this.#fromKey = fromKey;
|
|
673
|
+
this.#showNotFoundMessage = showNotFoundMessage;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** @inheritdoc */
|
|
677
|
+
get size(): SizeType.Arr {
|
|
678
|
+
return asUint32(this.#set.size);
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** @inheritdoc */
|
|
682
|
+
get isEmpty(): boolean {
|
|
683
|
+
return this.size === 0;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/** @inheritdoc */
|
|
687
|
+
has(key: K): boolean {
|
|
688
|
+
return this.#set.has(this.#toKey(key));
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/** @inheritdoc */
|
|
692
|
+
every<L extends K>(
|
|
693
|
+
predicate: (key: K) => key is L,
|
|
694
|
+
): this is ISetMapped<L, KM>;
|
|
695
|
+
/** @inheritdoc */
|
|
696
|
+
every(predicate: (key: K) => boolean): boolean;
|
|
697
|
+
/** @inheritdoc */
|
|
698
|
+
every(predicate: (key: K) => boolean): boolean {
|
|
699
|
+
for (const key of this.values()) {
|
|
700
|
+
if (!predicate(key)) return false;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
return true;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/** @inheritdoc */
|
|
707
|
+
some(predicate: (key: K) => boolean): boolean {
|
|
708
|
+
for (const key of this.values()) {
|
|
709
|
+
if (predicate(key)) return true;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
return false;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/** @inheritdoc */
|
|
716
|
+
add(key: K): ISetMapped<K, KM> {
|
|
717
|
+
if (this.has(key)) return this;
|
|
718
|
+
|
|
719
|
+
return ISetMapped.create(
|
|
720
|
+
[...this.#set, this.#toKey(key)].map(this.#fromKey),
|
|
721
|
+
this.#toKey,
|
|
722
|
+
this.#fromKey,
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/** @inheritdoc */
|
|
727
|
+
delete(key: K): ISetMapped<K, KM> {
|
|
728
|
+
if (!this.has(key)) {
|
|
729
|
+
if (this.#showNotFoundMessage) {
|
|
730
|
+
console.warn(
|
|
731
|
+
`ISetMapped.delete: key not found: ${String(this.#toKey(key))}`,
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
return this;
|
|
735
|
+
}
|
|
736
|
+
const keyMapped = this.#toKey(key);
|
|
737
|
+
|
|
738
|
+
return ISetMapped.create(
|
|
739
|
+
Array.from(this.#set)
|
|
740
|
+
.filter((k) => !Object.is(k, keyMapped))
|
|
741
|
+
.map(this.#fromKey),
|
|
742
|
+
this.#toKey,
|
|
743
|
+
this.#fromKey,
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/** @inheritdoc */
|
|
748
|
+
withMutations(
|
|
749
|
+
actions: readonly Readonly<
|
|
750
|
+
{ type: 'add'; key: K } | { type: 'delete'; key: K }
|
|
751
|
+
>[],
|
|
752
|
+
): ISetMapped<K, KM> {
|
|
753
|
+
const mut_result = new Set<KM>(this.#set);
|
|
754
|
+
|
|
755
|
+
for (const action of actions) {
|
|
756
|
+
const key = this.#toKey(action.key);
|
|
757
|
+
|
|
758
|
+
switch (action.type) {
|
|
759
|
+
case 'delete':
|
|
760
|
+
mut_result.delete(key);
|
|
761
|
+
break;
|
|
762
|
+
|
|
763
|
+
case 'add':
|
|
764
|
+
mut_result.add(key);
|
|
765
|
+
break;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
return ISetMapped.create<K, KM>(
|
|
770
|
+
Array.from(mut_result, this.#fromKey),
|
|
771
|
+
this.#toKey,
|
|
772
|
+
this.#fromKey,
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/** @inheritdoc */
|
|
777
|
+
map(mapFn: (key: K) => K): ISetMapped<K, KM> {
|
|
778
|
+
return ISetMapped.create(
|
|
779
|
+
this.toArray().map(mapFn),
|
|
780
|
+
this.#toKey,
|
|
781
|
+
this.#fromKey,
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/** @inheritdoc */
|
|
786
|
+
filter(predicate: (key: K) => boolean): ISetMapped<K, KM> {
|
|
787
|
+
return ISetMapped.create(
|
|
788
|
+
this.toArray().filter(predicate),
|
|
789
|
+
this.#toKey,
|
|
790
|
+
this.#fromKey,
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/** @inheritdoc */
|
|
795
|
+
filterNot(predicate: (key: K) => boolean): ISetMapped<K, KM> {
|
|
796
|
+
return ISetMapped.create(
|
|
797
|
+
this.toArray().filter((k) => !predicate(k)),
|
|
798
|
+
this.#toKey,
|
|
799
|
+
this.#fromKey,
|
|
800
|
+
);
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
/** @inheritdoc */
|
|
804
|
+
forEach(callbackfn: (key: K) => void): void {
|
|
805
|
+
for (const km of this.#set) {
|
|
806
|
+
callbackfn(this.#fromKey(km));
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/** @inheritdoc */
|
|
811
|
+
isSubsetOf(set: ISetMapped<K, KM>): boolean {
|
|
812
|
+
return this.every((k) => set.has(k));
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/** @inheritdoc */
|
|
816
|
+
isSupersetOf(set: ISetMapped<K, KM>): boolean {
|
|
817
|
+
return set.every((k) => this.has(k));
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
/** @inheritdoc */
|
|
821
|
+
subtract(set: ISetMapped<K, KM>): ISetMapped<K, KM> {
|
|
822
|
+
return ISetMapped.create(
|
|
823
|
+
this.toArray().filter((k) => !set.has(k)),
|
|
824
|
+
this.#toKey,
|
|
825
|
+
this.#fromKey,
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/** @inheritdoc */
|
|
830
|
+
intersect(set: ISetMapped<K, KM>): ISetMapped<K, KM> {
|
|
831
|
+
return ISetMapped.create(
|
|
832
|
+
this.toArray().filter((k) => set.has(k)),
|
|
833
|
+
this.#toKey,
|
|
834
|
+
this.#fromKey,
|
|
835
|
+
);
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/** @inheritdoc */
|
|
839
|
+
union(set: ISetMapped<K, KM>): ISetMapped<K, KM> {
|
|
840
|
+
return ISetMapped.create(
|
|
841
|
+
[...this.values(), ...set.values()],
|
|
842
|
+
this.#toKey,
|
|
843
|
+
this.#fromKey,
|
|
844
|
+
);
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* @inheritdoc
|
|
849
|
+
*/
|
|
850
|
+
*[Symbol.iterator](): Iterator<K> {
|
|
851
|
+
for (const k of this.keys()) {
|
|
852
|
+
yield k;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/** @inheritdoc */
|
|
857
|
+
*keys(): IterableIterator<K> {
|
|
858
|
+
for (const km of this.#set.keys()) {
|
|
859
|
+
yield this.#fromKey(km);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/** @inheritdoc */
|
|
864
|
+
*values(): IterableIterator<K> {
|
|
865
|
+
for (const km of this.#set.keys()) {
|
|
866
|
+
// JavaScript Set's values() is an alias for keys()
|
|
867
|
+
yield this.#fromKey(km);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/** @inheritdoc */
|
|
872
|
+
*entries(): IterableIterator<readonly [K, K]> {
|
|
873
|
+
for (const km of this.#set.keys()) {
|
|
874
|
+
// JavaScript Set's entries() yields [value, value]
|
|
875
|
+
const a = this.#fromKey(km);
|
|
876
|
+
yield [a, a];
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/** @inheritdoc */
|
|
881
|
+
toArray(): readonly K[] {
|
|
882
|
+
return Array.from(this.values());
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
/** @inheritdoc */
|
|
886
|
+
toRawSet(): ReadonlySet<KM> {
|
|
887
|
+
return this.#set;
|
|
888
|
+
}
|
|
889
|
+
}
|