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,651 @@
|
|
|
1
|
+
import { Optional, Result } from '../functional/index.mjs';
|
|
2
|
+
import { asUint32 } from '../number/index.mjs';
|
|
3
|
+
import { tp } from '../others/index.mjs';
|
|
4
|
+
import { unknownToString } from '../others/unknown-to-string.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Interface for an immutable map with O(1) lookup performance and functional programming patterns.
|
|
8
|
+
*
|
|
9
|
+
* This interface defines all methods and properties available on IMap instances. All operations
|
|
10
|
+
* that modify the map return new IMap instances, preserving immutability. The underlying implementation
|
|
11
|
+
* uses JavaScript's native Map for O(1) average-case performance on get, set, has, and delete operations.
|
|
12
|
+
*
|
|
13
|
+
* **Immutability Guarantees:**
|
|
14
|
+
* - All mutation operations (set, delete, update) return new IMap instances
|
|
15
|
+
* - Original IMap instances are never modified
|
|
16
|
+
* - Safe for concurrent access and functional programming patterns
|
|
17
|
+
*
|
|
18
|
+
* **Performance Characteristics:**
|
|
19
|
+
* - get/has/delete: O(1) average case
|
|
20
|
+
* - set: O(1) average case
|
|
21
|
+
* - map/filter operations: O(n)
|
|
22
|
+
* - Iteration: O(n)
|
|
23
|
+
*
|
|
24
|
+
* @template K The type of the keys in the map. Must extend MapSetKeyType (string, number, boolean, etc.)
|
|
25
|
+
* @template V The type of the values in the map.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // This is a type alias describing an interface, so it's not directly instantiated.
|
|
30
|
+
* // See IMap.create for examples of creating IMap instances that conform to this interface.
|
|
31
|
+
*
|
|
32
|
+
* // Example of how you might use a variable that implements this structure:
|
|
33
|
+
* declare const userMap: IMap<string, User>;
|
|
34
|
+
*
|
|
35
|
+
* // Immutable operations - original map is never modified
|
|
36
|
+
* const hasUser = userMap.has("alice"); // O(1)
|
|
37
|
+
* const user = userMap.get("alice").unwrapOr(defaultUser); // O(1)
|
|
38
|
+
* const newMap = userMap.set("bob", newUser); // O(1) - returns new IMap
|
|
39
|
+
* const updated = userMap.update("alice", u => ({ ...u, active: true })); // O(1)
|
|
40
|
+
*
|
|
41
|
+
* // Functional transformations
|
|
42
|
+
* const activeUsers = userMap.map((user, id) => ({ ...user, lastSeen: Date.now() })); // O(n)
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
type IMapInterface<K extends MapSetKeyType, V> = Readonly<{
|
|
46
|
+
// Getting information
|
|
47
|
+
|
|
48
|
+
/** The number of elements in the map. */
|
|
49
|
+
size: SizeType.Arr;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Checks if a key exists in the map.
|
|
53
|
+
* Allows for wider literal types for keys during checking.
|
|
54
|
+
* @param key The key to check.
|
|
55
|
+
* @returns `true` if the key exists, `false` otherwise.
|
|
56
|
+
*/
|
|
57
|
+
has: (key: K | (WidenLiteral<K> & {})) => boolean;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Retrieves the value associated with a key.
|
|
61
|
+
* @param key The key to retrieve.
|
|
62
|
+
* @returns The value associated with the key wrapped with Optional.some, or `Optional.none` if the key does not exist.
|
|
63
|
+
*/
|
|
64
|
+
get: (key: K | (WidenLiteral<K> & {})) => Optional<V>;
|
|
65
|
+
|
|
66
|
+
// Reducing a value
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Checks if all elements in the map satisfy a predicate.
|
|
70
|
+
* @param predicate A function to test each key-value pair.
|
|
71
|
+
* @returns `true` if all elements satisfy the predicate, `false` otherwise.
|
|
72
|
+
*/
|
|
73
|
+
every: ((predicate: (value: V, key: K) => boolean) => boolean) &
|
|
74
|
+
/**
|
|
75
|
+
* Checks if all elements in the map satisfy a type predicate.
|
|
76
|
+
* Narrows the type of values in the map if the predicate returns true for all elements.
|
|
77
|
+
* @template W The narrowed type of the values.
|
|
78
|
+
* @param predicate A type predicate function.
|
|
79
|
+
* @returns `true` if all elements satisfy the predicate, `false` otherwise.
|
|
80
|
+
*/
|
|
81
|
+
(<W extends V>(
|
|
82
|
+
predicate: (value: V, key: K) => value is W,
|
|
83
|
+
) => this is IMap<K, W>);
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Checks if at least one element in the map satisfies a predicate.
|
|
87
|
+
* @param predicate A function to test each key-value pair.
|
|
88
|
+
* @returns `true` if at least one element satisfies the predicate, `false` otherwise.
|
|
89
|
+
*/
|
|
90
|
+
some: (predicate: (value: V, key: K) => boolean) => boolean;
|
|
91
|
+
|
|
92
|
+
// Mutation
|
|
93
|
+
/**
|
|
94
|
+
* Deletes a key-value pair from the map.
|
|
95
|
+
* @param key The key to delete.
|
|
96
|
+
* @returns A new IMap instance without the specified key.
|
|
97
|
+
*/
|
|
98
|
+
delete: (key: K) => IMap<K, V>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Sets a key-value pair in the map.
|
|
102
|
+
* @param key The key to set.
|
|
103
|
+
* @param value The value to associate with the key.
|
|
104
|
+
* @returns A new IMap instance with the specified key-value pair.
|
|
105
|
+
*/
|
|
106
|
+
set: (key: K, value: V) => IMap<K, V>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Updates the value associated with a key using an updater function.
|
|
110
|
+
* @param key The key whose value to update.
|
|
111
|
+
* @param updater A function that takes the current value and returns the new value.
|
|
112
|
+
* @returns A new IMap instance with the updated value.
|
|
113
|
+
*/
|
|
114
|
+
update: (key: K, updater: (value: V) => V) => IMap<K, V>;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Applies a series of mutations to the map.
|
|
118
|
+
* @param actions An array of mutation actions (delete, set, or update).
|
|
119
|
+
* @returns A new IMap instance with all mutations applied.
|
|
120
|
+
*/
|
|
121
|
+
withMutations: (
|
|
122
|
+
actions: readonly Readonly<
|
|
123
|
+
| { type: 'delete'; key: K }
|
|
124
|
+
| { type: 'set'; key: K; value: V }
|
|
125
|
+
| { type: 'update'; key: K; updater: (value: V) => V }
|
|
126
|
+
>[],
|
|
127
|
+
) => IMap<K, V>;
|
|
128
|
+
|
|
129
|
+
// Sequence algorithms
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Maps the values of the map to new values.
|
|
133
|
+
* @template V2 The type of the new values.
|
|
134
|
+
* @param mapFn A function that maps a value and key to a new value.
|
|
135
|
+
* @returns A new IMap instance with mapped values.
|
|
136
|
+
*/
|
|
137
|
+
map: <V2>(mapFn: (value: V, key: K) => V2) => IMap<K, V2>;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Maps the keys of the map to new keys.
|
|
141
|
+
* @template K2 The type of the new keys.
|
|
142
|
+
* @param mapFn A function that maps a key to a new key.
|
|
143
|
+
* @returns A new IMap instance with mapped keys and original values.
|
|
144
|
+
*/
|
|
145
|
+
mapKeys: <K2 extends MapSetKeyType>(mapFn: (key: K) => K2) => IMap<K2, V>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Maps the entries (key-value pairs) of the map to new entries.
|
|
149
|
+
* @template K2 The type of the new keys in the entries.
|
|
150
|
+
* @template V2 The type of the new values in the entries.
|
|
151
|
+
* @param mapFn A function that maps an entry to a new entry.
|
|
152
|
+
* @returns A new IMap instance with mapped entries.
|
|
153
|
+
*/
|
|
154
|
+
mapEntries: <K2 extends MapSetKeyType, V2>(
|
|
155
|
+
mapFn: (entry: readonly [K, V]) => readonly [K2, V2],
|
|
156
|
+
) => IMap<K2, V2>;
|
|
157
|
+
|
|
158
|
+
// Side effects
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Executes a callback function for each key-value pair in the map.
|
|
162
|
+
* @param callbackfn A function to execute for each element.
|
|
163
|
+
*/
|
|
164
|
+
forEach: (callbackfn: (value: V, key: K) => void) => void;
|
|
165
|
+
|
|
166
|
+
// Iterators
|
|
167
|
+
/**
|
|
168
|
+
* Returns an iterator for the keys in the map.
|
|
169
|
+
* @returns An iterable iterator of keys.
|
|
170
|
+
*/
|
|
171
|
+
keys: () => IterableIterator<K>;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Returns an iterator for the values in the map.
|
|
175
|
+
* @returns An iterable iterator of values.
|
|
176
|
+
*/
|
|
177
|
+
values: () => IterableIterator<V>;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Returns an iterator for the entries (key-value pairs) in the map.
|
|
181
|
+
* @returns An iterable iterator of entries.
|
|
182
|
+
*/
|
|
183
|
+
entries: () => IterableIterator<readonly [K, V]>;
|
|
184
|
+
|
|
185
|
+
// Conversion
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Converts the keys of the map to an array.
|
|
189
|
+
* @returns A readonly array of keys.
|
|
190
|
+
*/
|
|
191
|
+
toKeysArray: () => readonly K[];
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Converts the values of the map to an array.
|
|
195
|
+
* @returns A readonly array of values.
|
|
196
|
+
*/
|
|
197
|
+
toValuesArray: () => readonly V[];
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Converts the entries (key-value pairs) of the map to an array.
|
|
201
|
+
* @returns A readonly array of entries.
|
|
202
|
+
*/
|
|
203
|
+
toEntriesArray: () => readonly (readonly [K, V])[];
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Converts the map to an array of entries (key-value pairs).
|
|
207
|
+
* Alias for `toEntriesArray`.
|
|
208
|
+
* @returns A readonly array of entries.
|
|
209
|
+
*/
|
|
210
|
+
toArray: () => readonly (readonly [K, V])[];
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Returns the underlying readonly JavaScript Map.
|
|
214
|
+
* @returns The raw ReadonlyMap instance.
|
|
215
|
+
*/
|
|
216
|
+
toRawMap: () => ReadonlyMap<K, V>;
|
|
217
|
+
}>;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Represents an immutable map with high-performance operations and functional programming support.
|
|
221
|
+
*
|
|
222
|
+
* IMap is a persistent data structure that provides all the functionality of JavaScript's Map
|
|
223
|
+
* while maintaining immutability. All operations that would normally mutate the map instead
|
|
224
|
+
* return new IMap instances, making it safe for functional programming and concurrent access.
|
|
225
|
+
*
|
|
226
|
+
* **Key Features:**
|
|
227
|
+
* - **Immutable**: All mutation operations return new instances
|
|
228
|
+
* - **High Performance**: O(1) average-case for get/set/has/delete operations
|
|
229
|
+
* - **Type Safe**: Full TypeScript support with generic key/value types
|
|
230
|
+
* - **Iterable**: Implements standard JavaScript iteration protocols
|
|
231
|
+
* - **Functional**: Rich API for map, filter, reduce-style operations
|
|
232
|
+
*
|
|
233
|
+
* **When to Use:**
|
|
234
|
+
* - State management in functional applications
|
|
235
|
+
* - Caching with immutable guarantees
|
|
236
|
+
* - Data structures that need to be shared across components
|
|
237
|
+
* - When you need Map functionality but want immutability
|
|
238
|
+
*
|
|
239
|
+
* @template K The type of the keys in the map. Must extend MapSetKeyType.
|
|
240
|
+
* @template V The type of the values in the map.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* // Create an immutable map with initial data
|
|
245
|
+
* let userPreferences = IMap.create<string, UserPreference>([
|
|
246
|
+
* ["theme", { value: "dark", lastModified: Date.now() }],
|
|
247
|
+
* ["language", { value: "en", lastModified: Date.now() }]
|
|
248
|
+
* ]);
|
|
249
|
+
*
|
|
250
|
+
* console.log(userPreferences.get("theme").unwrapOr(defaultPreference));
|
|
251
|
+
* console.log(userPreferences.size); // Output: 2
|
|
252
|
+
*
|
|
253
|
+
* // All operations return new instances - original is unchanged
|
|
254
|
+
* const updated = userPreferences
|
|
255
|
+
* .set("notifications", { value: true, lastModified: Date.now() })
|
|
256
|
+
* .update("theme", pref => ({ ...pref, value: "light" }));
|
|
257
|
+
*
|
|
258
|
+
* console.log(userPreferences.has("notifications")); // false (original unchanged)
|
|
259
|
+
* console.log(updated.has("notifications")); // true (new instance)
|
|
260
|
+
*
|
|
261
|
+
* // Efficient iteration and transformation
|
|
262
|
+
* for (const [key, preference] of updated) {
|
|
263
|
+
* console.log(`${key}: ${preference.value}`);
|
|
264
|
+
* }
|
|
265
|
+
*
|
|
266
|
+
* // Functional transformations
|
|
267
|
+
* const withTimestamps = updated.map((pref, key) => ({
|
|
268
|
+
* ...pref,
|
|
269
|
+
* accessedAt: Date.now()
|
|
270
|
+
* }));
|
|
271
|
+
*
|
|
272
|
+
* // Type-safe operations with narrowing
|
|
273
|
+
* const stringKeys = IMap.create<number | string, any>([[1, "a"], ["b", 2]]);
|
|
274
|
+
* const onlyStringKeys = stringKeys.mapKeys(key =>
|
|
275
|
+
* typeof key === "string" ? key : key.toString()
|
|
276
|
+
* );
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
export type IMap<K extends MapSetKeyType, V> = Iterable<readonly [K, V]> &
|
|
280
|
+
IMapInterface<K, V>;
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Provides utility functions for IMap.
|
|
284
|
+
*/
|
|
285
|
+
export namespace IMap {
|
|
286
|
+
/**
|
|
287
|
+
* Creates a new IMap instance from an iterable of key-value pairs.
|
|
288
|
+
*
|
|
289
|
+
* This factory function accepts any iterable of [key, value] tuples, including arrays,
|
|
290
|
+
* JavaScript Maps, other IMaps, or custom iterables. The resulting IMap will contain
|
|
291
|
+
* all the entries from the input iterable.
|
|
292
|
+
*
|
|
293
|
+
* **Performance:** O(n) where n is the number of entries in the iterable.
|
|
294
|
+
*
|
|
295
|
+
* @template K The type of the keys. Must extend MapSetKeyType.
|
|
296
|
+
* @template V The type of the values.
|
|
297
|
+
* @param iterable An iterable of key-value pairs (e.g., Array, Map, IMap, etc.)
|
|
298
|
+
* @returns A new IMap instance containing all entries from the iterable.
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* ```typescript
|
|
302
|
+
* // From array of tuples
|
|
303
|
+
* const userScores = IMap.create<string, number>([
|
|
304
|
+
* ["alice", 95],
|
|
305
|
+
* ["bob", 87],
|
|
306
|
+
* ["charlie", 92]
|
|
307
|
+
* ]);
|
|
308
|
+
* console.log(userScores.get("alice").unwrap()); // Output: 95
|
|
309
|
+
*
|
|
310
|
+
* // From JavaScript Map
|
|
311
|
+
* const jsMap = new Map([["config", { debug: true }], ["env", "production"]]);
|
|
312
|
+
* const config = IMap.create(jsMap);
|
|
313
|
+
* console.log(config.get("env").unwrap()); // Output: "production"
|
|
314
|
+
*
|
|
315
|
+
* // From another IMap (creates a copy)
|
|
316
|
+
* const originalMap = IMap.create<string, boolean>([["enabled", true]]);
|
|
317
|
+
* const copiedMap = IMap.create(originalMap);
|
|
318
|
+
* console.log(copiedMap.get("enabled").unwrap()); // Output: true
|
|
319
|
+
*
|
|
320
|
+
* // Empty map
|
|
321
|
+
* const emptyMap = IMap.create<string, number>([]);
|
|
322
|
+
* console.log(emptyMap.size); // Output: 0
|
|
323
|
+
*
|
|
324
|
+
* // From custom iterable
|
|
325
|
+
* function* generateEntries(): Generator<[string, number]> {
|
|
326
|
+
* for (let i = 0; i < 3; i++) {
|
|
327
|
+
* yield [`item${i}`, i * 10];
|
|
328
|
+
* }
|
|
329
|
+
* }
|
|
330
|
+
* const generatedMap = IMap.create(generateEntries());
|
|
331
|
+
* console.log(generatedMap.size); // Output: 3
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
export const create = <K extends MapSetKeyType, V>(
|
|
335
|
+
iterable: Iterable<readonly [K, V]>,
|
|
336
|
+
): IMap<K, V> => new IMapClass<K, V>(iterable);
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Checks if two IMap instances are structurally equal.
|
|
340
|
+
*
|
|
341
|
+
* Two IMaps are considered equal if they have the same size and contain exactly the same
|
|
342
|
+
* key-value pairs. The order of entries does not matter for equality comparison.
|
|
343
|
+
* Values are compared using JavaScript's `===` operator.
|
|
344
|
+
*
|
|
345
|
+
* **Performance:** O(n) where n is the size of the smaller map.
|
|
346
|
+
*
|
|
347
|
+
* @template K The type of the keys.
|
|
348
|
+
* @template V The type of the values.
|
|
349
|
+
* @param a The first IMap instance to compare.
|
|
350
|
+
* @param b The second IMap instance to compare.
|
|
351
|
+
* @returns `true` if the maps contain exactly the same key-value pairs, `false` otherwise.
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```typescript
|
|
355
|
+
* // Basic equality comparison
|
|
356
|
+
* const preferences1 = IMap.create<string, boolean>([
|
|
357
|
+
* ["darkMode", true],
|
|
358
|
+
* ["notifications", false]
|
|
359
|
+
* ]);
|
|
360
|
+
* const preferences2 = IMap.create<string, boolean>([
|
|
361
|
+
* ["darkMode", true],
|
|
362
|
+
* ["notifications", false]
|
|
363
|
+
* ]);
|
|
364
|
+
* const preferences3 = IMap.create<string, boolean>([
|
|
365
|
+
* ["notifications", false],
|
|
366
|
+
* ["darkMode", true] // Order doesn't matter
|
|
367
|
+
* ]);
|
|
368
|
+
*
|
|
369
|
+
* console.log(IMap.equal(preferences1, preferences2)); // true
|
|
370
|
+
* console.log(IMap.equal(preferences1, preferences3)); // true (order doesn't matter)
|
|
371
|
+
*
|
|
372
|
+
* // Different values
|
|
373
|
+
* const preferences4 = IMap.create<string, boolean>([
|
|
374
|
+
* ["darkMode", false], // Different value
|
|
375
|
+
* ["notifications", false]
|
|
376
|
+
* ]);
|
|
377
|
+
* console.log(IMap.equal(preferences1, preferences4)); // false
|
|
378
|
+
*
|
|
379
|
+
* // Different keys
|
|
380
|
+
* const preferences5 = IMap.create<string, boolean>([
|
|
381
|
+
* ["darkMode", true],
|
|
382
|
+
* ["sounds", false] // Different key
|
|
383
|
+
* ]);
|
|
384
|
+
* console.log(IMap.equal(preferences1, preferences5)); // false
|
|
385
|
+
*
|
|
386
|
+
* // Empty maps
|
|
387
|
+
* const empty1 = IMap.create<string, number>([]);
|
|
388
|
+
* const empty2 = IMap.create<string, number>([]);
|
|
389
|
+
* console.log(IMap.equal(empty1, empty2)); // true
|
|
390
|
+
*
|
|
391
|
+
* // Note: For deep equality of object values, use a custom comparison
|
|
392
|
+
* const users1 = IMap.create<string, User>([["1", { name: "Alice" }]]);
|
|
393
|
+
* const users2 = IMap.create<string, User>([["1", { name: "Alice" }]]);
|
|
394
|
+
* console.log(IMap.equal(users1, users2)); // false (different object references)
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
export const equal = <K extends MapSetKeyType, V>(
|
|
398
|
+
a: IMap<K, V>,
|
|
399
|
+
b: IMap<K, V>,
|
|
400
|
+
): boolean => a.size === b.size && a.every((v, k) => b.get(k) === v);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Internal class implementation for IMap providing immutable map operations.
|
|
405
|
+
*
|
|
406
|
+
* This class implements the IMap interface using JavaScript's native Map as the underlying
|
|
407
|
+
* storage mechanism for optimal performance. All mutation operations create new instances
|
|
408
|
+
* rather than modifying the existing map, ensuring immutability.
|
|
409
|
+
*
|
|
410
|
+
* **Implementation Details:**
|
|
411
|
+
* - Uses ReadonlyMap<K, V> internally for type safety and performance
|
|
412
|
+
* - Implements copy-on-write semantics for efficiency
|
|
413
|
+
* - Provides optional debug messaging for development
|
|
414
|
+
*
|
|
415
|
+
* @template K The type of the keys. Must extend MapSetKeyType.
|
|
416
|
+
* @template V The type of the values.
|
|
417
|
+
* @implements IMap
|
|
418
|
+
* @implements Iterable
|
|
419
|
+
* @internal This class should not be used directly. Use IMap.create() instead.
|
|
420
|
+
*/
|
|
421
|
+
class IMapClass<K extends MapSetKeyType, V>
|
|
422
|
+
implements IMap<K, V>, Iterable<readonly [K, V]>
|
|
423
|
+
{
|
|
424
|
+
readonly #map: ReadonlyMap<K, V>;
|
|
425
|
+
readonly #showNotFoundMessage: boolean;
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Constructs an IMapClass instance with the given entries.
|
|
429
|
+
*
|
|
430
|
+
* @param iterable An iterable of key-value pairs to populate the map.
|
|
431
|
+
* @param showNotFoundMessage Whether to log warning messages when operations
|
|
432
|
+
* are performed on non-existent keys. Useful for debugging.
|
|
433
|
+
* Defaults to false for production use.
|
|
434
|
+
* @internal Use IMap.create() instead of calling this constructor directly.
|
|
435
|
+
*/
|
|
436
|
+
constructor(
|
|
437
|
+
iterable: Iterable<readonly [K, V]>,
|
|
438
|
+
showNotFoundMessage: boolean = false,
|
|
439
|
+
) {
|
|
440
|
+
this.#map = new Map(iterable);
|
|
441
|
+
this.#showNotFoundMessage = showNotFoundMessage;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** @inheritdoc */
|
|
445
|
+
get size(): SizeType.Arr {
|
|
446
|
+
return asUint32(this.#map.size);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** @inheritdoc */
|
|
450
|
+
has(key: K | (WidenLiteral<K> & {})): boolean {
|
|
451
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
|
|
452
|
+
return this.#map.has(key as K);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/** @inheritdoc */
|
|
456
|
+
get(key: K | (WidenLiteral<K> & {})): Optional<V> {
|
|
457
|
+
if (!this.has(key)) return Optional.none;
|
|
458
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion, @typescript-eslint/no-non-null-assertion
|
|
459
|
+
return Optional.some(this.#map.get(key as K)!);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** @inheritdoc */
|
|
463
|
+
every<W extends V>(
|
|
464
|
+
predicate: (value: V, key: K) => value is W,
|
|
465
|
+
): this is IMap<K, W>;
|
|
466
|
+
/** @inheritdoc */
|
|
467
|
+
every(predicate: (value: V, key: K) => boolean): boolean;
|
|
468
|
+
/** @inheritdoc */
|
|
469
|
+
every(predicate: (value: V, key: K) => boolean): boolean {
|
|
470
|
+
for (const [k, v] of this.entries()) {
|
|
471
|
+
if (!predicate(v, k)) return false;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** @inheritdoc */
|
|
478
|
+
some(predicate: (value: V, key: K) => boolean): boolean {
|
|
479
|
+
for (const [k, v] of this.entries()) {
|
|
480
|
+
if (predicate(v, k)) return true;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** @inheritdoc */
|
|
487
|
+
delete(key: K): IMap<K, V> {
|
|
488
|
+
if (!this.has(key)) {
|
|
489
|
+
if (this.#showNotFoundMessage) {
|
|
490
|
+
const keyStr = unknownToString(key);
|
|
491
|
+
console.warn(
|
|
492
|
+
`IMap.delete: key not found: ${Result.isOk(keyStr) ? keyStr.value : '<error converting key to string>'}`,
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
return this;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return IMap.create(
|
|
499
|
+
Array.from(this.#map).filter(([k]) => !Object.is(k, key)),
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/** @inheritdoc */
|
|
504
|
+
set(key: K, value: V): IMap<K, V> {
|
|
505
|
+
if (value === this.get(key)) return this; // has no changes
|
|
506
|
+
if (!this.has(key)) {
|
|
507
|
+
return IMap.create([...this.#map, tp(key, value)]);
|
|
508
|
+
} else {
|
|
509
|
+
return IMap.create(
|
|
510
|
+
Array.from(this.#map, ([k, v]) => tp(k, Object.is(k, key) ? value : v)),
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** @inheritdoc */
|
|
516
|
+
update(key: K, updater: (value: V) => V): IMap<K, V> {
|
|
517
|
+
const curr = this.get(key);
|
|
518
|
+
|
|
519
|
+
if (Optional.isNone(curr)) {
|
|
520
|
+
if (this.#showNotFoundMessage) {
|
|
521
|
+
const keyStr = unknownToString(key);
|
|
522
|
+
console.warn(
|
|
523
|
+
`IMap.update: key not found: ${Result.isOk(keyStr) ? keyStr.value : '<error converting key to string>'}`,
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
return this;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
return IMap.create(
|
|
530
|
+
Array.from(this.#map, ([k, v]) =>
|
|
531
|
+
tp(k, Object.is(k, key) ? updater(curr.value) : v),
|
|
532
|
+
),
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/** @inheritdoc */
|
|
537
|
+
withMutations(
|
|
538
|
+
actions: readonly Readonly<
|
|
539
|
+
| { type: 'delete'; key: K }
|
|
540
|
+
| { type: 'set'; key: K; value: V }
|
|
541
|
+
| { type: 'update'; key: K; updater: (value: V) => V }
|
|
542
|
+
>[],
|
|
543
|
+
): IMap<K, V> {
|
|
544
|
+
const mut_result = new Map<K, V>(this.#map);
|
|
545
|
+
|
|
546
|
+
for (const action of actions) {
|
|
547
|
+
switch (action.type) {
|
|
548
|
+
case 'delete':
|
|
549
|
+
mut_result.delete(action.key);
|
|
550
|
+
break;
|
|
551
|
+
|
|
552
|
+
case 'set':
|
|
553
|
+
mut_result.set(action.key, action.value);
|
|
554
|
+
break;
|
|
555
|
+
|
|
556
|
+
case 'update': {
|
|
557
|
+
const { key } = action;
|
|
558
|
+
|
|
559
|
+
const curr = mut_result.get(key);
|
|
560
|
+
|
|
561
|
+
if (!mut_result.has(key) || curr === undefined) {
|
|
562
|
+
if (this.#showNotFoundMessage) {
|
|
563
|
+
const keyStr = unknownToString(key);
|
|
564
|
+
console.warn(
|
|
565
|
+
`IMap.withMutations: key not found: ${Result.isOk(keyStr) ? keyStr.value : '<error converting key to string>'}`,
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
mut_result.set(key, action.updater(curr));
|
|
572
|
+
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
return IMap.create(mut_result);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** @inheritdoc */
|
|
582
|
+
map<V2>(mapFn: (value: V, key: K) => V2): IMap<K, V2> {
|
|
583
|
+
return IMap.create(this.toArray().map(([k, v]) => tp(k, mapFn(v, k))));
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** @inheritdoc */
|
|
587
|
+
mapKeys<K2 extends MapSetKeyType>(mapFn: (key: K) => K2): IMap<K2, V> {
|
|
588
|
+
return IMap.create(this.toArray().map(([k, v]) => tp(mapFn(k), v)));
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/** @inheritdoc */
|
|
592
|
+
mapEntries<K2 extends MapSetKeyType, V2>(
|
|
593
|
+
mapFn: (entry: readonly [K, V]) => readonly [K2, V2],
|
|
594
|
+
): IMap<K2, V2> {
|
|
595
|
+
return IMap.create(this.toArray().map(mapFn));
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** @inheritdoc */
|
|
599
|
+
forEach(callbackfn: (value: V, key: K) => void): void {
|
|
600
|
+
for (const [key, value] of this.#map.entries()) {
|
|
601
|
+
callbackfn(value, key);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* @inheritdoc
|
|
607
|
+
*/
|
|
608
|
+
[Symbol.iterator](): Iterator<readonly [K, V]> {
|
|
609
|
+
return this.#map[Symbol.iterator]();
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/** @inheritdoc */
|
|
613
|
+
keys(): IterableIterator<K> {
|
|
614
|
+
return this.#map.keys();
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/** @inheritdoc */
|
|
618
|
+
values(): IterableIterator<V> {
|
|
619
|
+
return this.#map.values();
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/** @inheritdoc */
|
|
623
|
+
entries(): IterableIterator<readonly [K, V]> {
|
|
624
|
+
return this.#map.entries();
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/** @inheritdoc */
|
|
628
|
+
toKeysArray(): readonly K[] {
|
|
629
|
+
return Array.from(this.keys());
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/** @inheritdoc */
|
|
633
|
+
toValuesArray(): readonly V[] {
|
|
634
|
+
return Array.from(this.values());
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/** @inheritdoc */
|
|
638
|
+
toEntriesArray(): readonly (readonly [K, V])[] {
|
|
639
|
+
return Array.from(this.entries());
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/** @inheritdoc */
|
|
643
|
+
toArray(): readonly (readonly [K, V])[] {
|
|
644
|
+
return Array.from(this.entries());
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/** @inheritdoc */
|
|
648
|
+
toRawMap(): ReadonlyMap<K, V> {
|
|
649
|
+
return this.#map;
|
|
650
|
+
}
|
|
651
|
+
}
|