@bemoje/map 1.0.1 → 1.2.1

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.
Files changed (66) hide show
  1. package/lib/ExtMap.cjs +354 -0
  2. package/lib/ExtMap.d.cts +110 -0
  3. package/lib/ExtMap.d.ts +110 -0
  4. package/lib/ExtMap.js +352 -0
  5. package/lib/TimeoutWeakMap.cjs +122 -0
  6. package/lib/TimeoutWeakMap.d.cts +75 -0
  7. package/lib/TimeoutWeakMap.d.ts +75 -0
  8. package/lib/TimeoutWeakMap.js +120 -0
  9. package/lib/countUniques.cjs +363 -0
  10. package/lib/countUniques.d.cts +9 -0
  11. package/lib/countUniques.d.ts +9 -0
  12. package/lib/countUniques.js +361 -0
  13. package/lib/entriesArray.cjs +12 -0
  14. package/lib/entriesArray.d.cts +19 -0
  15. package/lib/entriesArray.d.ts +19 -0
  16. package/lib/entriesArray.js +10 -0
  17. package/lib/index.cjs +505 -0
  18. package/lib/index.d.cts +17 -0
  19. package/lib/index.d.ts +17 -0
  20. package/{index.mjs → lib/index.js} +104 -90
  21. package/lib/isGenericMap.cjs +18 -0
  22. package/lib/isGenericMap.d.cts +6 -0
  23. package/lib/isGenericMap.d.ts +6 -0
  24. package/lib/isGenericMap.js +16 -0
  25. package/lib/keysArray.cjs +12 -0
  26. package/lib/keysArray.d.cts +19 -0
  27. package/lib/keysArray.d.ts +19 -0
  28. package/lib/keysArray.js +10 -0
  29. package/lib/mapGetOrDefault.cjs +18 -0
  30. package/lib/mapGetOrDefault.d.cts +8 -0
  31. package/lib/mapGetOrDefault.d.ts +8 -0
  32. package/lib/mapGetOrDefault.js +16 -0
  33. package/lib/mapLoad.cjs +15 -0
  34. package/lib/mapLoad.d.cts +11 -0
  35. package/lib/mapLoad.d.ts +11 -0
  36. package/lib/mapLoad.js +13 -0
  37. package/lib/mapReverse.cjs +24 -0
  38. package/lib/mapReverse.d.cts +15 -0
  39. package/lib/mapReverse.d.ts +15 -0
  40. package/lib/mapReverse.js +22 -0
  41. package/lib/mapUpdate.cjs +14 -0
  42. package/lib/mapUpdate.d.cts +13 -0
  43. package/lib/mapUpdate.d.ts +13 -0
  44. package/lib/mapUpdate.js +12 -0
  45. package/lib/sort.cjs +17 -0
  46. package/lib/sort.d.cts +25 -0
  47. package/lib/sort.d.ts +25 -0
  48. package/lib/sort.js +15 -0
  49. package/lib/sortByKeys.cjs +19 -0
  50. package/lib/sortByKeys.d.cts +24 -0
  51. package/lib/sortByKeys.d.ts +24 -0
  52. package/lib/sortByKeys.js +17 -0
  53. package/lib/sortByValues.cjs +19 -0
  54. package/lib/sortByValues.d.cts +25 -0
  55. package/lib/sortByValues.d.ts +25 -0
  56. package/lib/sortByValues.js +17 -0
  57. package/lib/toMap.cjs +12 -0
  58. package/lib/toMap.d.cts +20 -0
  59. package/lib/toMap.d.ts +20 -0
  60. package/lib/toMap.js +10 -0
  61. package/lib/valuesArray.cjs +12 -0
  62. package/lib/valuesArray.d.cts +19 -0
  63. package/lib/valuesArray.d.ts +19 -0
  64. package/lib/valuesArray.js +10 -0
  65. package/package.json +248 -18
  66. package/index.d.ts +0 -364
package/index.d.ts DELETED
@@ -1,364 +0,0 @@
1
- import { inspect, InspectOptions } from 'node:util';
2
-
3
- /**
4
- * Interface for a View that wraps a target object.
5
- * View methods are redirected to the wrapped object which acts as
6
- * a surrogate 'this'.
7
- *
8
- * To create a method that redirects to 'target', @see {@link defineProxifiedPrototypeProperty}
9
- */
10
- interface IView<T extends object> {
11
- readonly target: T;
12
- }
13
-
14
- declare const TARGET_SYMBOL: unique symbol;
15
- /**
16
- * Base class providing view functionality over a target object using the Composition pattern.
17
- */
18
- declare class View<Target extends object> implements IView<Target> {
19
- private [TARGET_SYMBOL];
20
- constructor(target: Target);
21
- get [Symbol.toStringTag](): string;
22
- get target(): Target;
23
- }
24
-
25
- type MapKeys = keyof Map<any, any>;
26
- type GenericMap<K, V, P extends MapKeys> = {
27
- [Method in P]: Method extends 'set' ? (key: K, value: V) => any : Method extends 'get' ? (key: K) => V | undefined : Method extends 'has' ? (key: K) => boolean : Method extends 'delete' ? (key: K) => boolean : Method extends 'clear' ? () => void : Method extends 'forEach' ? (callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any) => void : Method extends 'entries' ? () => IterableIterator<[K, V]> : Method extends 'keys' ? () => IterableIterator<K> : Method extends 'values' ? () => IterableIterator<V> : Method extends typeof Symbol.iterator ? () => IterableIterator<[K, V]> : Method extends 'size' ? number : Method extends typeof Symbol.toStringTag ? string : any;
28
- };
29
-
30
- declare module './ExtMap' {
31
- interface ExtMap<K = any, V = any> extends Map<K, V> {
32
- load(entries: Iterable<[K, V]>): this;
33
- sort(compare: (a: [K, V], b: [K, V]) => number): this;
34
- sortByKeys(compare: (a: K, b: K) => number): this;
35
- sortByValues(compare: (a: V, b: V) => number): this;
36
- reverse(): this;
37
- update(key: K, update: (value: V | undefined, key: K, map: this) => V): this;
38
- getOrDefault(key: K, factory: (key: K, map: this) => V): V;
39
- keysArray(): K[];
40
- valuesArray(): V[];
41
- entriesArray(): [K, V][];
42
- toMap(): Map<K, V>;
43
- toObject<K extends string, V>(): Record<K, V>;
44
- toObject<K, V>(): Record<Extract<K, PropertyKey> | string, V>;
45
- reduce<R>(reducer: (accumulator: R, value: V, key: K) => R, initialValue: R): R;
46
- map<NewK, NewV>(mapper: (value: V, key: K) => [NewK, NewV]): ExtMap<NewK, NewV>;
47
- mapKeys<NewK>(mapper: (key: K, value: V) => NewK): ExtMap<NewK, V>;
48
- mapValues<NewV>(mapper: (value: V, key: K) => NewV): ExtMap<K, NewV>;
49
- filter(predicate: (value: V, key: K) => boolean): ExtMap<K, V>;
50
- }
51
- }
52
- /**
53
- * Minimal Extended Map class focused only on Map-specific utilities.
54
- *
55
- * @template K - The type of keys in the map
56
- * @template V - The type of values in the map
57
- *
58
- * @example
59
- * ```typescript
60
- * import { map as iterMap, filter } from 'iter-tools'
61
- *
62
- * const extMap = new ExtMap<string, number>()
63
- * .load([['a', 1], ['b', 2], ['c', 3]])
64
- * .update('a', v => (v ?? 0) + 10)
65
- * .sortByValues((a, b) => a - b)
66
- *
67
- * // Use iter-tools for transformations
68
- * const doubled = new ExtMap<string, number>()
69
- * .load(iterMap(([k, v]) => [k, v * 2], extMap))
70
- *
71
- * // Use iter-tools for filtering
72
- * const filtered = new ExtMap<string, number>()
73
- * .load(filter(([k, v]) => v > 5, extMap))
74
- * ```
75
- */
76
- declare class ExtMap<K = any, V = any> extends View<Map<K, V>> implements Map<K, V> {
77
- constructor(iterable?: Iterable<readonly [K, V]> | null | undefined);
78
- constructor(obj?: Record<Extract<K, PropertyKey>, V>);
79
- /**
80
- * Creates a new ExtMap instance from a regular object.
81
- *
82
- * @param obj - An object whose key-value pairs will be used to initialize the map
83
- * @returns A new ExtMap instance containing the entries from the object
84
- */
85
- static fromObject<K extends PropertyKey, V>(obj: Record<K, V>): ExtMap<K, V>;
86
- /**
87
- * Creates a new ExtMap instance from an iterable of key-value pairs.
88
- *
89
- * @param entries - An iterable of key-value pairs to initialize the map
90
- * @returns A new ExtMap instance containing the provided entries
91
- */
92
- static fromIterable<K, V>(entries: Iterable<readonly [K, V]>): ExtMap<K, V>;
93
- /**
94
- * Creates a shallow copy of the map.
95
- *
96
- * @returns A new ExtMap instance with the same entries
97
- */
98
- clone(): ExtMap<K, V>;
99
- /**
100
- * Converts the map to a JSON-serializable format.
101
- *
102
- * @returns Array of key-value pairs suitable for JSON serialization
103
- */
104
- toJSON(): [K, V][];
105
- /**
106
- * Custom inspection method for better debugging output.
107
- *
108
- * @param _depth - The depth of inspection
109
- * @param _options - Options for inspection
110
- * @returns A string representation of the map
111
- */
112
- [inspect.custom](_depth: number, _options: InspectOptions): string;
113
- }
114
-
115
- /**
116
- * A WeakMap with automatic timeout-based expiry for entries.
117
- *
118
- * Entries are automatically removed after a specified timeout period.
119
- * Accessing an entry refreshes its timeout, extending its lifetime.
120
- * This is useful for caching scenarios where you want automatic cleanup
121
- * of unused entries while keeping frequently accessed ones alive.
122
- */
123
- declare class TimeoutWeakMap<K extends object, V> {
124
- readonly timeoutMs: number;
125
- protected readonly wmap: WeakMap<K, [V, NodeJS.Timeout]>;
126
- /**
127
- * @param timeoutMs Default timeout in milliseconds for entries
128
- */
129
- constructor(timeoutMs: number);
130
- /**
131
- * Retrieves the value for the given key and refreshes its timeout.
132
- *
133
- * @param key The key to retrieve
134
- * @returns The value if found, undefined otherwise
135
- */
136
- get(key: K): V | undefined;
137
- /**
138
- * Sets a key-value pair with an optional custom timeout.
139
- *
140
- * @param key The key to set
141
- * @param value The value to associate with the key
142
- * @param timeoutMs Optional timeout override for this entry
143
- * @returns This instance for chaining
144
- */
145
- set(key: K, value: V, timeoutMs?: number): this;
146
- /**
147
- * Removes a key-value pair and clears its timeout.
148
- *
149
- * @param key The key to delete
150
- * @returns True if the key existed and was deleted, false otherwise
151
- */
152
- delete(key: K): boolean;
153
- /**
154
- * Checks if a key exists in the map.
155
- * Does not refresh the timeout.
156
- *
157
- * @param key The key to check
158
- * @returns True if the key exists, false otherwise
159
- */
160
- has(key: K): boolean;
161
- /**
162
- * Loads multiple key-value pairs into the map.
163
- *
164
- * @param entries Iterable of key-value pairs to load
165
- * @param timeoutMs Optional timeout override for all entries
166
- * @returns This instance for chaining
167
- */
168
- load(entries: Iterable<[K, V]>, timeoutMs?: number): this;
169
- /**
170
- * Updates an existing value or creates a new one using an update function.
171
- *
172
- * @param key The key to update
173
- * @param update Function that receives the current value and returns the new value
174
- * @returns This instance for chaining
175
- */
176
- update(key: K, update: (value: V | undefined, key: K, map: this) => V): this;
177
- /**
178
- * Gets a value or creates it using a factory function if it doesn't exist.
179
- * Refreshes timeout if the value exists.
180
- *
181
- * @param key The key to get or create
182
- * @param factory Function to create the value if it doesn't exist
183
- * @param timeoutMs Optional timeout override for new entries
184
- * @returns The existing or newly created value
185
- */
186
- getOrDefault(key: K, factory: (key: K, map: this) => V, timeoutMs?: number): V;
187
- }
188
-
189
- /**
190
- * Count unique occurrences of values in an iterable, returning a sorted map by count descending.
191
- */
192
- declare function countUniques<V>(input: Iterable<V>): ExtMap<V, number>;
193
-
194
- /**
195
- * Returns an array of all key-value pairs in the map.
196
- * Convenience method that converts the entries iterator to an array.
197
- *
198
- * @returns Array of all key-value pairs
199
- *
200
- * @example
201
- * ```typescript
202
- * const map = new ExtMap<string, number>()
203
- * .load([['a', 1], ['b', 2], ['c', 3]])
204
- *
205
- * console.log(entriesArray(map)) // [['a', 1], ['b', 2], ['c', 3]]
206
- * ```
207
- */
208
- declare function entriesArray<K, V>(ins: GenericMap<K, V, typeof Symbol.iterator>): [K, V][];
209
-
210
- /**
211
- * Checks if the provided value implements the Map interface with the specified required properties.
212
- */
213
- declare function isGenericMap<Props extends (keyof Map<K, V>)[], K, V>(target: unknown, requiredProps?: Props): target is Pick<Map<K, V>, Props[number]>;
214
-
215
- /**
216
- * Returns an array of all keys in the map.
217
- * Convenience method that converts the keys iterator to an array.
218
- *
219
- * @returns Array of all keys
220
- *
221
- * @example
222
- * ```typescript
223
- * const map = new ExtMap<string, number>()
224
- * .load([['a', 1], ['b', 2], ['c', 3]])
225
- *
226
- * console.log(keysArray(map)) // ['a', 'b', 'c']
227
- * ```
228
- */
229
- declare function keysArray<K, V>(ins: GenericMap<K, V, 'keys'>): K[];
230
-
231
- /**
232
- * Gets a value from a map or creates it using a factory function if it doesn't exist.
233
- */
234
- declare function mapGetOrDefault<K, V, T extends GenericMap<K, V, 'get' | 'has' | 'set'>>(map: T, key: K, factory: (key: K, map: T) => V): V;
235
-
236
- /**
237
- * Loads multiple entries into the map from an iterable.
238
- *
239
- * @param entries - An iterable of key-value pairs to load into the map
240
- * @returns The map instance for method chaining
241
- */
242
- declare function mapLoad<T extends GenericMap<K, V, 'set'>, K, V>(ins: T, entries: Iterable<[K, V]>): T;
243
-
244
- /**
245
- * Reverses the order of entries in a Map.
246
- * @remarks This function creates a new Map with the entries of the original Map in reverse order.
247
- * The original Map is not modified.
248
- * @example ```ts
249
- * mapReverse(new Map([['a', 1], ['b', 2], ['c', 3]]));
250
- * //=> [['c', 3], ['b', 2], ['a', 1]]
251
- * ```
252
- */
253
- declare function mapReverse<T extends GenericMap<K, V, 'set' | 'clear' | typeof Symbol.iterator>, K, V>(ins: T): T;
254
- declare function mapReverse<K, V>(ins: Map<K, V>): Map<K, V>;
255
-
256
- /**
257
- * Updates a value in the map using an update function.
258
- *
259
- * @param key - The key to update
260
- * @param update - Function that receives the current value and returns the new value
261
- * @returns The map instance for method chaining
262
- */
263
- declare function mapUpdate<T extends GenericMap<K, V, 'set' | 'get'>, K, V>(ins: T, key: K, update: (value: V | undefined, key: K, map: T) => V): T;
264
- declare function mapUpdate<K, V>(ins: Map<K, V>, key: K, update: (value: V | undefined, key: K, map: Map<K, V>) => V): typeof ins;
265
-
266
- /**
267
- * Sorts the map entries using a custom comparison function and updates the map in place.
268
- * This is Map-specific because it maintains insertion order.
269
- *
270
- * @param ins - The map instance to sort
271
- * @param compare - Function that compares two entries
272
- * @returns The map instance for method chaining
273
- *
274
- * @example
275
- * ```typescript
276
- * const map = new ExtMap<string, number>([
277
- * ['c', 3],
278
- * ['a', 1],
279
- * ['b', 2],
280
- * ])
281
- *
282
- * sort(map, ([k1], [k2]) => k1.localeCompare(k2))
283
- * console.log(entriesArray(map)) // [['a', 1], ['b', 2], ['c', 3]]
284
- * ```
285
- */
286
- declare function sort<K, V>(ins: GenericMap<K, V, 'set' | 'clear' | typeof Symbol.iterator>, compare: (a: [K, V], b: [K, V]) => number): GenericMap<K, V, 'set' | 'clear' | typeof Symbol.iterator>;
287
-
288
- /**
289
- * Sorts the map entries by their keys and updates the map in place.
290
- *
291
- * @param ins - The map instance to sort
292
- * @param compare - Function that compares two keys
293
- * @returns The map instance for method chaining
294
- *
295
- * @example
296
- * ```typescript
297
- * const map = new ExtMap<string, number>([
298
- * ['c', 3],
299
- * ['a', 1],
300
- * ['b', 2],
301
- * ])
302
- *
303
- * sortByKeys(map, (a, b) => a.localeCompare(b))
304
- * console.log(entriesArray(map)) // [['a', 1], ['b', 2], ['c', 3]]
305
- * ```
306
- */
307
- declare function sortByKeys<K, V>(ins: GenericMap<K, V, 'set' | 'clear' | typeof Symbol.iterator>, compare: (a: K, b: K) => number): GenericMap<K, V, 'set' | 'clear' | typeof Symbol.iterator>;
308
-
309
- /**
310
- * Sorts the map entries by their values and updates the map in place.
311
- *
312
- * @param ins - The map instance to sort
313
- * @param compare - Function that compares two values
314
- * @returns The map instance for method chaining
315
- *
316
- * @example
317
- * ```typescript
318
- * const map = new ExtMap<string, number>([
319
- * ['c', 3],
320
- * ['a', 1],
321
- * ['b', 2],
322
- * ])
323
- *
324
- * sortByValues(map, (a, b) => a - b)
325
- * console.log(entriesArray(map)) // [['a', 1], ['b', 2], ['c', 3]]
326
- * ```
327
- */
328
- declare function sortByValues<T extends GenericMap<K, V, 'set' | 'clear' | typeof Symbol.iterator>, K, V>(ins: T, compare: (a: V, b: V) => number): T;
329
- declare function sortByValues<K, V>(ins: GenericMap<K, V, 'set' | 'clear' | typeof Symbol.iterator>, compare: (a: V, b: V) => number): GenericMap<K, V, 'set' | 'clear' | typeof Symbol.iterator>;
330
-
331
- /**
332
- * Converts a GenericMap to a native Map.
333
- *
334
- * @param ins - The GenericMap instance to convert
335
- * @returns A new native Map with the same entries
336
- *
337
- * @example
338
- * ```typescript
339
- * const extMap = new ExtMap<string, number>()
340
- * .load([['a', 1], ['b', 2], ['c', 3]])
341
- *
342
- * const nativeMap = toMap(extMap)
343
- * console.log(nativeMap instanceof Map) // true
344
- * ```
345
- */
346
- declare function toMap<K, V>(ins: GenericMap<K, V, typeof Symbol.iterator>): Map<K, V>;
347
-
348
- /**
349
- * Returns an array of all values in the map.
350
- * Convenience method that converts the values iterator to an array.
351
- *
352
- * @returns Array of all values
353
- *
354
- * @example
355
- * ```typescript
356
- * const map = new ExtMap<string, number>()
357
- * .load([['a', 1], ['b', 2], ['c', 3]])
358
- *
359
- * console.log(valuesArray(map)) // [1, 2, 3]
360
- * ```
361
- */
362
- declare function valuesArray<K, V>(ins: GenericMap<K, V, 'values'>): V[];
363
-
364
- export { ExtMap, TimeoutWeakMap, countUniques, entriesArray, isGenericMap, keysArray, mapGetOrDefault, mapLoad, mapReverse, mapUpdate, sort, sortByKeys, sortByValues, toMap, valuesArray };