@bemoje/map 0.2.2 → 1.0.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.
package/README.md CHANGED
@@ -1,34 +1,100 @@
1
1
  # @bemoje/map
2
2
 
3
+ Extended Map class and utilities for sorting, filtering, mapping, and managing key-value data.
3
4
 
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5-blue)](https://www.typescriptlang.org/)
6
+ [![Module](https://img.shields.io/badge/Module-ESM-yellow)](https://nodejs.org/api/esm.html)
4
7
 
5
- ![npm (scoped)](https://img.shields.io/npm/v/%40bemoje/trie-map)
6
- ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/%40bemoje/map)
7
- ![npm](https://img.shields.io/npm/dt/%40bemoje/map)
8
- ![NPM](https://img.shields.io/npm/l/%40bemoje%2Fmap)
8
+ ## Installation
9
9
 
10
- ![GitHub last commit (by committer)](https://img.shields.io/github/last-commit/bemoje/tsmono)
11
- ![GitHub closed pull requests](https://img.shields.io/github/issues-pr-closed/bemoje/tsmono)
12
- ![GitHub top language](https://img.shields.io/github/languages/top/bemoje/tsmono)
10
+ ```bash
11
+ npm install @bemoje/map
12
+ ```
13
13
 
14
+ ## Usage
14
15
 
15
- ## Documentation
16
- [Documentation Website](https://bemoje.github.io/tsmono/modules/map.html)
16
+ ### ExtMap
17
17
 
18
- ## Installation
19
- This library is published in the NPM registry and can be installed using any compatible package manager.
18
+ An extended Map with built-in sorting, filtering, mapping, and chaining:
20
19
 
21
- #### NPM
22
- ```sh
23
- npm install @bemoje/map
20
+ ```ts
21
+ import { ExtMap } from '@bemoje/map'
22
+
23
+ const map = new ExtMap<string, number>()
24
+ .load([
25
+ ['c', 3],
26
+ ['a', 1],
27
+ ['b', 2],
28
+ ])
29
+ .sortByKeys((a, b) => a.localeCompare(b))
30
+
31
+ map.entriesArray() // [['a', 1], ['b', 2], ['c', 3]]
32
+ map.keysArray() // ['a', 'b', 'c']
33
+ map.valuesArray() // [1, 2, 3]
34
+
35
+ // Filter, map, reduce
36
+ const big = map.filter((v) => v > 1) // ExtMap { 'b' => 2, 'c' => 3 }
37
+ const doubled = map.mapValues((v) => v * 2) // ExtMap { 'a' => 2, 'b' => 4, 'c' => 6 }
38
+ const sum = map.reduce((acc, v) => acc + v, 0) // 6
39
+
40
+ // From objects
41
+ const fromObj = ExtMap.fromObject({ x: 10, y: 20 })
42
+ fromObj.toObject() // { x: 10, y: 20 }
43
+ ```
44
+
45
+ ### TimeoutWeakMap
46
+
47
+ A WeakMap with automatic timeout-based expiry:
48
+
49
+ ```ts
50
+ import { TimeoutWeakMap } from '@bemoje/map'
51
+
52
+ const cache = new TimeoutWeakMap<object, string>(5000) // 5s default TTL
53
+
54
+ const key = {}
55
+ cache.set(key, 'cached-value')
56
+ cache.get(key) // 'cached-value' (refreshes timeout)
57
+
58
+ // Auto-expires after 5 seconds of no access
59
+
60
+ // Get-or-create pattern
61
+ cache.getOrDefault(key, () => 'computed-value')
62
+
63
+ // Custom timeout per entry
64
+ cache.set(key, 'short-lived', 1000) // 1s TTL
24
65
  ```
25
66
 
67
+ ### Standalone Utilities
26
68
 
27
- ## Issues
28
- Please let me know of any bugs or [issues](https://github.com/bemoje/tsmono/issues).
69
+ ```ts
70
+ import { mapUpdate, mapGetOrDefault, mapReverse, mapLoad, sortByValues } from '@bemoje/map'
71
+
72
+ const m = new Map<string, number>()
73
+ mapLoad(m, [
74
+ ['a', 1],
75
+ ['b', 2],
76
+ ])
77
+ mapUpdate(m, 'a', (v) => (v ?? 0) + 10) // Map { 'a' => 11, 'b' => 2 }
78
+ mapGetOrDefault(m, 'c', () => 99) // 99 (inserted into map)
79
+ mapReverse(m) // reverses entry order in-place
80
+ sortByValues(m, (a, b) => a - b) // sorts entries by value in-place
81
+ ```
29
82
 
30
- ## Contribute
31
- Contributors are welcome to open a [pull request](https://github.com/bemoje/tsmono/pulls).
83
+ ## API Reference
32
84
 
33
- ## License
34
- Released under the [undefined License](./LICENSE).
85
+ | Export | Description |
86
+ | ----------------- | --------------------------------------------------------- |
87
+ | `ExtMap` | Extended Map with sort, filter, map, reduce, and chaining |
88
+ | `TimeoutWeakMap` | WeakMap with automatic timeout-based entry expiry |
89
+ | `mapGetOrDefault` | Get a value or create it with a factory function |
90
+ | `mapLoad` | Load multiple entries into a map |
91
+ | `mapReverse` | Reverse the order of map entries |
92
+ | `mapUpdate` | Update a map value using an update function |
93
+ | `sort` | Sort map entries with a custom comparator |
94
+ | `sortByKeys` | Sort map entries by keys |
95
+ | `sortByValues` | Sort map entries by values |
96
+ | `toMap` | Convert a GenericMap to a native Map |
97
+ | `keysArray` | Get all keys as an array |
98
+ | `valuesArray` | Get all values as an array |
99
+ | `entriesArray` | Get all entries as an array |
100
+ | `isGenericMap` | Check if a value implements the Map interface |
package/index.d.ts ADDED
@@ -0,0 +1,364 @@
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 };
package/index.mjs ADDED
@@ -0,0 +1,475 @@
1
+ import { isIterable, map, filter, reduce } from 'iter-tools';
2
+ import { inspect } from 'util';
3
+ import { MultiSet } from 'mnemonist';
4
+
5
+ var __defProp = Object.defineProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+
8
+ // ../composition/src/View.ts
9
+ var TARGET_SYMBOL = /* @__PURE__ */ Symbol("target");
10
+ var View = class {
11
+ static {
12
+ __name(this, "View");
13
+ }
14
+ [TARGET_SYMBOL];
15
+ constructor(target) {
16
+ this[TARGET_SYMBOL] = target;
17
+ }
18
+ get [Symbol.toStringTag]() {
19
+ return this.constructor.name;
20
+ }
21
+ get target() {
22
+ return this[TARGET_SYMBOL];
23
+ }
24
+ };
25
+
26
+ // ../composition/src/inheritProxifiedPrototypeProperty.ts
27
+ function inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, key) {
28
+ const des = Reflect.getOwnPropertyDescriptor(TargetClass.prototype, key);
29
+ if (!des) {
30
+ return;
31
+ }
32
+ if (des.value && typeof des.value === "function") {
33
+ return Object.defineProperty(ViewerClass.prototype, key, {
34
+ value(...args) {
35
+ return this.target[key](...args);
36
+ },
37
+ writable: true,
38
+ configurable: true,
39
+ enumerable: false
40
+ });
41
+ } else if (des && !des.get && !des.set) {
42
+ return Object.defineProperty(ViewerClass.prototype, key, {
43
+ get() {
44
+ return this.target[key];
45
+ },
46
+ set: des.writable ? /* @__PURE__ */ __name(function set(value) {
47
+ this.target[key] = value;
48
+ }, "set") : void 0,
49
+ configurable: true,
50
+ enumerable: false
51
+ });
52
+ } else {
53
+ Object.defineProperty(ViewerClass.prototype, key, {
54
+ get: des.get ? function() {
55
+ return this.target[key];
56
+ } : void 0,
57
+ set: des.set ? function(value) {
58
+ this.target[key] = value;
59
+ } : void 0,
60
+ configurable: true,
61
+ enumerable: false
62
+ });
63
+ }
64
+ }
65
+ __name(inheritProxifiedPrototypeProperty, "inheritProxifiedPrototypeProperty");
66
+
67
+ // ../composition/src/inheritProxifiedPrototype.ts
68
+ function inheritProxifiedPrototype(ViewerClass, TargetClass, omitKeys) {
69
+ Reflect.ownKeys(TargetClass.prototype).filter((k) => {
70
+ return k !== "constructor" && k !== Symbol.toStringTag;
71
+ }).filter((k) => {
72
+ return omitKeys ? !omitKeys.includes(k) : true;
73
+ }).forEach((k) => {
74
+ return inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, k);
75
+ });
76
+ return ViewerClass;
77
+ }
78
+ __name(inheritProxifiedPrototype, "inheritProxifiedPrototype");
79
+
80
+ // ../object/src/entriesOf.ts
81
+ function entriesOf(obj) {
82
+ return Object.entries(obj);
83
+ }
84
+ __name(entriesOf, "entriesOf");
85
+
86
+ // ../object/src/defineMethod.ts
87
+ function defineMethod(obj, key, value, des = {}) {
88
+ return Object.defineProperty(obj, key, {
89
+ value,
90
+ configurable: true,
91
+ writable: true,
92
+ enumerable: false,
93
+ ...des
94
+ });
95
+ }
96
+ __name(defineMethod, "defineMethod");
97
+ function filterIterableEntries(mapLike, predicate) {
98
+ return filter(([k, v]) => {
99
+ return predicate(v, k);
100
+ }, mapLike);
101
+ }
102
+ __name(filterIterableEntries, "filterIterableEntries");
103
+ function mapIterableEntries(mapLike, transform) {
104
+ return map(([k, v]) => {
105
+ return transform(v, k);
106
+ }, mapLike);
107
+ }
108
+ __name(mapIterableEntries, "mapIterableEntries");
109
+ function mapIterableKeys(mapLike, transform) {
110
+ return map(([k, v]) => {
111
+ return [
112
+ transform(k, v),
113
+ v
114
+ ];
115
+ }, mapLike);
116
+ }
117
+ __name(mapIterableKeys, "mapIterableKeys");
118
+ function mapIterableValues(mapLike, transform) {
119
+ return map(([k, v]) => {
120
+ return [
121
+ k,
122
+ transform(v, k)
123
+ ];
124
+ }, mapLike);
125
+ }
126
+ __name(mapIterableValues, "mapIterableValues");
127
+
128
+ // ../iter/src/reduceIterableEntries.ts
129
+ function reduceIterableEntries(mapLike, reducer, initialValue) {
130
+ for (const [key, value] of mapLike) {
131
+ initialValue = reducer(initialValue, value, key);
132
+ }
133
+ return initialValue;
134
+ }
135
+ __name(reduceIterableEntries, "reduceIterableEntries");
136
+
137
+ // ../iter/src/toObjectIterable.ts
138
+ function toObjectIterable(mapLike) {
139
+ const object = {};
140
+ for (const [key, value] of mapLike) {
141
+ if (typeof key !== "string" && typeof key !== "number" && typeof key !== "symbol") {
142
+ object[String(key)] = value;
143
+ } else {
144
+ object[key] = value;
145
+ }
146
+ }
147
+ return object;
148
+ }
149
+ __name(toObjectIterable, "toObjectIterable");
150
+
151
+ // src/entriesArray.ts
152
+ function entriesArray(ins) {
153
+ return Array.from(ins);
154
+ }
155
+ __name(entriesArray, "entriesArray");
156
+
157
+ // src/keysArray.ts
158
+ function keysArray(ins) {
159
+ return Array.from(ins.keys());
160
+ }
161
+ __name(keysArray, "keysArray");
162
+
163
+ // src/mapGetOrDefault.ts
164
+ function mapGetOrDefault(map4, key, factory) {
165
+ let value = map4.get(key);
166
+ if (value !== void 0 || map4.has(key)) {
167
+ return value;
168
+ }
169
+ value = factory(key, map4);
170
+ map4.set(key, value);
171
+ return value;
172
+ }
173
+ __name(mapGetOrDefault, "mapGetOrDefault");
174
+
175
+ // src/mapLoad.ts
176
+ function mapLoad(ins, entries) {
177
+ for (const [key, value] of entries) {
178
+ ins.set(key, value);
179
+ }
180
+ return ins;
181
+ }
182
+ __name(mapLoad, "mapLoad");
183
+
184
+ // src/mapReverse.ts
185
+ function mapReverse(ins) {
186
+ const entries = Array.from(ins).reverse();
187
+ ins.clear();
188
+ mapLoad(ins, entries);
189
+ return ins;
190
+ }
191
+ __name(mapReverse, "mapReverse");
192
+
193
+ // src/mapUpdate.ts
194
+ function mapUpdate(ins, key, update) {
195
+ const newValue = update(ins.get(key), key, ins);
196
+ ins.set(key, newValue);
197
+ return ins;
198
+ }
199
+ __name(mapUpdate, "mapUpdate");
200
+
201
+ // src/sort.ts
202
+ function sort(ins, compare) {
203
+ const entries = Array.from(ins).sort(compare);
204
+ ins.clear();
205
+ for (const [k, v] of entries) {
206
+ ins.set(k, v);
207
+ }
208
+ return ins;
209
+ }
210
+ __name(sort, "sort");
211
+
212
+ // src/sortByKeys.ts
213
+ function sortByKeys(ins, compare) {
214
+ const entries = Array.from(ins).sort((a, b) => {
215
+ return compare(a[0], b[0]);
216
+ });
217
+ ins.clear();
218
+ for (const [k, v] of entries) {
219
+ ins.set(k, v);
220
+ }
221
+ return ins;
222
+ }
223
+ __name(sortByKeys, "sortByKeys");
224
+
225
+ // src/sortByValues.ts
226
+ function sortByValues(ins, compare) {
227
+ const entries = Array.from(ins).sort((a, b) => {
228
+ return compare(a[1], b[1]);
229
+ });
230
+ ins.clear();
231
+ for (const [k, v] of entries) {
232
+ ins.set(k, v);
233
+ }
234
+ return ins;
235
+ }
236
+ __name(sortByValues, "sortByValues");
237
+
238
+ // src/toMap.ts
239
+ function toMap(ins) {
240
+ return new Map(ins);
241
+ }
242
+ __name(toMap, "toMap");
243
+
244
+ // src/valuesArray.ts
245
+ function valuesArray(ins) {
246
+ return Array.from(ins.values());
247
+ }
248
+ __name(valuesArray, "valuesArray");
249
+
250
+ // src/ExtMap.ts
251
+ var ExtMap = class _ExtMap extends View {
252
+ static {
253
+ __name(this, "ExtMap");
254
+ }
255
+ constructor(arg) {
256
+ super(new Map(isIterable(arg) ? arg : arg ? entriesOf(arg) : void 0));
257
+ }
258
+ /**
259
+ * Creates a new ExtMap instance from a regular object.
260
+ *
261
+ * @param obj - An object whose key-value pairs will be used to initialize the map
262
+ * @returns A new ExtMap instance containing the entries from the object
263
+ */
264
+ static fromObject(obj) {
265
+ return new _ExtMap(Object.entries(obj));
266
+ }
267
+ /**
268
+ * Creates a new ExtMap instance from an iterable of key-value pairs.
269
+ *
270
+ * @param entries - An iterable of key-value pairs to initialize the map
271
+ * @returns A new ExtMap instance containing the provided entries
272
+ */
273
+ static fromIterable(entries) {
274
+ return new _ExtMap(entries);
275
+ }
276
+ /**
277
+ * Creates a shallow copy of the map.
278
+ *
279
+ * @returns A new ExtMap instance with the same entries
280
+ */
281
+ clone() {
282
+ return new _ExtMap(this);
283
+ }
284
+ /**
285
+ * Converts the map to a JSON-serializable format.
286
+ *
287
+ * @returns Array of key-value pairs suitable for JSON serialization
288
+ */
289
+ toJSON() {
290
+ return [
291
+ ...this.entries()
292
+ ];
293
+ }
294
+ /**
295
+ * Custom inspection method for better debugging output.
296
+ *
297
+ * @param _depth - The depth of inspection
298
+ * @param _options - Options for inspection
299
+ * @returns A string representation of the map
300
+ */
301
+ [inspect.custom](_depth, _options) {
302
+ return inspect(this.target, {
303
+ breakLength: 50
304
+ });
305
+ }
306
+ };
307
+ function thisify(fn) {
308
+ return /* @__PURE__ */ __name(function wrapped(...args) {
309
+ return fn(this, ...args);
310
+ }, "wrapped");
311
+ }
312
+ __name(thisify, "thisify");
313
+ function transformReturnValue(fn, transform) {
314
+ return /* @__PURE__ */ __name(function wrapped(...args) {
315
+ const result = fn.apply(this, args);
316
+ return transform(result);
317
+ }, "wrapped");
318
+ }
319
+ __name(transformReturnValue, "transformReturnValue");
320
+ inheritProxifiedPrototype(ExtMap, Map, []);
321
+ defineMethod(ExtMap.prototype, "load", thisify(mapLoad));
322
+ defineMethod(ExtMap.prototype, "sort", thisify(sort));
323
+ defineMethod(ExtMap.prototype, "sortByKeys", thisify(sortByKeys));
324
+ defineMethod(ExtMap.prototype, "sortByValues", thisify(sortByValues));
325
+ defineMethod(ExtMap.prototype, "reverse", thisify(mapReverse));
326
+ defineMethod(ExtMap.prototype, "update", thisify(mapUpdate));
327
+ defineMethod(ExtMap.prototype, "getOrDefault", thisify(mapGetOrDefault));
328
+ defineMethod(ExtMap.prototype, "keysArray", thisify(keysArray));
329
+ defineMethod(ExtMap.prototype, "valuesArray", thisify(valuesArray));
330
+ defineMethod(ExtMap.prototype, "entriesArray", thisify(entriesArray));
331
+ defineMethod(ExtMap.prototype, "toObject", thisify(toObjectIterable));
332
+ defineMethod(ExtMap.prototype, "toMap", thisify(toMap));
333
+ defineMethod(ExtMap.prototype, "reduce", thisify(reduceIterableEntries));
334
+ defineMethod(ExtMap.prototype, "map", thisify(transformReturnValue(mapIterableEntries, ExtMap.fromIterable)));
335
+ defineMethod(ExtMap.prototype, "mapKeys", thisify(transformReturnValue(mapIterableKeys, ExtMap.fromIterable)));
336
+ defineMethod(ExtMap.prototype, "mapValues", thisify(transformReturnValue(mapIterableValues, ExtMap.fromIterable)));
337
+ defineMethod(ExtMap.prototype, "filter", thisify(transformReturnValue(filterIterableEntries, ExtMap.fromIterable)));
338
+
339
+ // src/TimeoutWeakMap.ts
340
+ var TimeoutWeakMap = class {
341
+ static {
342
+ __name(this, "TimeoutWeakMap");
343
+ }
344
+ timeoutMs;
345
+ wmap = /* @__PURE__ */ new WeakMap();
346
+ /**
347
+ * @param timeoutMs Default timeout in milliseconds for entries
348
+ */
349
+ constructor(timeoutMs) {
350
+ this.timeoutMs = timeoutMs;
351
+ }
352
+ /**
353
+ * Retrieves the value for the given key and refreshes its timeout.
354
+ *
355
+ * @param key The key to retrieve
356
+ * @returns The value if found, undefined otherwise
357
+ */
358
+ get(key) {
359
+ const vt = this.wmap.get(key);
360
+ if (!vt) {
361
+ return void 0;
362
+ }
363
+ const [value, timeout] = vt;
364
+ timeout.refresh();
365
+ return value;
366
+ }
367
+ /**
368
+ * Sets a key-value pair with an optional custom timeout.
369
+ *
370
+ * @param key The key to set
371
+ * @param value The value to associate with the key
372
+ * @param timeoutMs Optional timeout override for this entry
373
+ * @returns This instance for chaining
374
+ */
375
+ set(key, value, timeoutMs) {
376
+ this.wmap.set(key, [
377
+ value,
378
+ setTimeout(() => {
379
+ this.delete(key);
380
+ }, timeoutMs ?? this.timeoutMs).unref()
381
+ ]);
382
+ return this;
383
+ }
384
+ /**
385
+ * Removes a key-value pair and clears its timeout.
386
+ *
387
+ * @param key The key to delete
388
+ * @returns True if the key existed and was deleted, false otherwise
389
+ */
390
+ delete(key) {
391
+ const vt = this.wmap.get(key);
392
+ if (!vt) {
393
+ return false;
394
+ }
395
+ const timeout = vt[1];
396
+ clearTimeout(timeout);
397
+ return this.wmap.delete(key);
398
+ }
399
+ /**
400
+ * Checks if a key exists in the map.
401
+ * Does not refresh the timeout.
402
+ *
403
+ * @param key The key to check
404
+ * @returns True if the key exists, false otherwise
405
+ */
406
+ has(key) {
407
+ return this.wmap.has(key);
408
+ }
409
+ /**
410
+ * Loads multiple key-value pairs into the map.
411
+ *
412
+ * @param entries Iterable of key-value pairs to load
413
+ * @param timeoutMs Optional timeout override for all entries
414
+ * @returns This instance for chaining
415
+ */
416
+ load(entries, timeoutMs) {
417
+ for (const [key, value] of entries) {
418
+ this.set(key, value, timeoutMs);
419
+ }
420
+ return this;
421
+ }
422
+ /**
423
+ * Updates an existing value or creates a new one using an update function.
424
+ *
425
+ * @param key The key to update
426
+ * @param update Function that receives the current value and returns the new value
427
+ * @returns This instance for chaining
428
+ */
429
+ update(key, update) {
430
+ return this.set(key, update(this.get(key), key, this));
431
+ }
432
+ /**
433
+ * Gets a value or creates it using a factory function if it doesn't exist.
434
+ * Refreshes timeout if the value exists.
435
+ *
436
+ * @param key The key to get or create
437
+ * @param factory Function to create the value if it doesn't exist
438
+ * @param timeoutMs Optional timeout override for new entries
439
+ * @returns The existing or newly created value
440
+ */
441
+ getOrDefault(key, factory, timeoutMs) {
442
+ const vt = this.wmap.get(key);
443
+ if (vt) {
444
+ const [value, timeout] = vt;
445
+ timeout.refresh();
446
+ return value;
447
+ } else {
448
+ const value = factory(key, this);
449
+ this.set(key, value, timeoutMs);
450
+ return value;
451
+ }
452
+ }
453
+ };
454
+ function countUniques(input) {
455
+ return new ExtMap(reduce(new MultiSet(), (acc, imp) => {
456
+ return acc.add(imp);
457
+ }, input).multiplicities()).sortByValues((a, b) => {
458
+ return b - a;
459
+ });
460
+ }
461
+ __name(countUniques, "countUniques");
462
+
463
+ // src/isGenericMap.ts
464
+ function isGenericMap(target, requiredProps = [
465
+ "get",
466
+ "set",
467
+ "has"
468
+ ]) {
469
+ return !!target && requiredProps.every((prop) => {
470
+ return typeof target[prop] === typeof Map.prototype[prop];
471
+ });
472
+ }
473
+ __name(isGenericMap, "isGenericMap");
474
+
475
+ export { ExtMap, TimeoutWeakMap, countUniques, entriesArray, isGenericMap, keysArray, mapGetOrDefault, mapLoad, mapReverse, mapUpdate, sort, sortByKeys, sortByValues, toMap, valuesArray };
package/package.json CHANGED
@@ -1,14 +1,27 @@
1
1
  {
2
2
  "name": "@bemoje/map",
3
- "version": "0.2.2",
4
- "type": "commonjs",
5
- "main": "cjs/index.js",
6
- "types": "cjs/index.d.ts",
3
+ "version": "1.0.1",
4
+ "description": "map",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "keywords": [
8
+ "map"
9
+ ],
10
+ "exports": {
11
+ ".": {
12
+ "types": "./index.d.ts",
13
+ "import": "./index.mjs",
14
+ "default": "./index.mjs"
15
+ }
16
+ },
7
17
  "dependencies": {
8
- "tslib": "latest"
18
+ "iter-tools": "^7.5.4",
19
+ "mnemonist": "^0.40.3"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
9
23
  },
10
24
  "license": "MIT",
11
- "keywords": [],
12
25
  "author": {
13
26
  "name": "Benjamin Møller Jensen",
14
27
  "email": "bemoje@bemoje.net",
@@ -16,14 +29,7 @@
16
29
  },
17
30
  "repository": {
18
31
  "type": "git",
19
- "url": "git+https://github.com/bemoje/tsmono.git"
20
- },
21
- "funding": {
22
- "type": "patreon",
23
- "url": "https://www.patreon.com/user?u=40752770"
24
- },
25
- "bugs": {
26
- "url": "https://github.com/bemoje/tsmono/issues"
27
- },
28
- "homepage": "https://github.com/bemoje/tsmono"
32
+ "url": "git+https://github.com/bemoje/mono.git",
33
+ "directory": "libs/map"
34
+ }
29
35
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Benjamin Møller Jensen
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/cjs/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './lib/mapGetOrElse';
2
- export * from './lib/mapReverse';
3
- export * from './lib/mapUpdate';
4
- export * from './lib/WeakMapWithTimeout';
5
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../packages/map/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,0BAA0B,CAAA"}
package/cjs/index.js DELETED
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./lib/mapGetOrElse"), exports);
5
- tslib_1.__exportStar(require("./lib/mapReverse"), exports);
6
- tslib_1.__exportStar(require("./lib/mapUpdate"), exports);
7
- tslib_1.__exportStar(require("./lib/WeakMapWithTimeout"), exports);
8
- //# sourceMappingURL=index.js.map
package/cjs/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/map/src/index.ts"],"names":[],"mappings":";;;AAAA,6DAAkC;AAClC,2DAAgC;AAChC,0DAA+B;AAC/B,mEAAwC"}
@@ -1,21 +0,0 @@
1
- /// <reference types="node" />
2
- export interface ITimeoutMapOptions<K extends object, V> {
3
- /**
4
- * auto delete after timeout (ms)
5
- */
6
- timeout: number;
7
- /**
8
- * callback when a key is deleted
9
- */
10
- callback?: <R = void>(key: K, value: V, map: WeakMapWithTimeout<K, V>) => R;
11
- }
12
- export declare class WeakMapWithTimeout<K extends object, V> extends WeakMap<K, V> {
13
- protected readonly timeouts: WeakMap<K, NodeJS.Timeout>;
14
- readonly options: ITimeoutMapOptions<K, V>;
15
- constructor(options: ITimeoutMapOptions<K, V>);
16
- set(key: K, value: V, callback?: () => any): this;
17
- delete(key: K): boolean;
18
- protected clearTimeout(key: K): void;
19
- protected setTimeout(key: K, callback?: <R>() => R): void;
20
- }
21
- //# sourceMappingURL=WeakMapWithTimeout.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"WeakMapWithTimeout.d.ts","sourceRoot":"","sources":["../../../../../packages/map/src/lib/WeakMapWithTimeout.ts"],"names":[],"mappings":";AAAA,MAAM,WAAW,kBAAkB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC;IACrD;;OAEG;IACH,OAAO,EAAE,MAAM,CAAA;IAEf;;OAEG;IACH,QAAQ,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAA;CAC5E;AAED,qBAAa,kBAAkB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC;IACxE,SAAS,CAAC,QAAQ,CAAC,QAAQ,6BAAmC;IAC9D,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;gBAE9B,OAAO,EAAE,kBAAkB,CAAC,CAAC,EAAE,CAAC,CAAC;IAKpC,GAAG,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG;IAK1C,MAAM,CAAC,GAAG,EAAE,CAAC;IAKtB,SAAS,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC;IAM7B,SAAS,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC;CAcnD"}
@@ -1,39 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WeakMapWithTimeout = void 0;
4
- class WeakMapWithTimeout extends WeakMap {
5
- timeouts = new WeakMap();
6
- options;
7
- constructor(options) {
8
- super();
9
- this.options = { ...options };
10
- }
11
- set(key, value, callback) {
12
- this.setTimeout(key, callback);
13
- return super.set(key, value);
14
- }
15
- delete(key) {
16
- this.clearTimeout(key);
17
- return super.delete(key);
18
- }
19
- clearTimeout(key) {
20
- const timeout = this.timeouts.get(key);
21
- if (timeout)
22
- clearTimeout(timeout);
23
- this.timeouts.delete(key);
24
- }
25
- setTimeout(key, callback) {
26
- this.clearTimeout(key);
27
- this.timeouts.set(key, setTimeout(() => {
28
- const v = this.get(key);
29
- this.delete(key);
30
- if (callback)
31
- callback();
32
- if (v && this.options.callback) {
33
- this.options.callback(key, v, this);
34
- }
35
- }, this.options.timeout));
36
- }
37
- }
38
- exports.WeakMapWithTimeout = WeakMapWithTimeout;
39
- //# sourceMappingURL=WeakMapWithTimeout.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"WeakMapWithTimeout.js","sourceRoot":"","sources":["../../../../../packages/map/src/lib/WeakMapWithTimeout.ts"],"names":[],"mappings":";;;AAYA,MAAa,kBAAwC,SAAQ,OAAa;IACrD,QAAQ,GAAG,IAAI,OAAO,EAAqB,CAAA;IACrD,OAAO,CAA0B;IAE1C,YAAY,OAAiC;QAC3C,KAAK,EAAE,CAAA;QACP,IAAI,CAAC,OAAO,GAAG,EAAE,GAAG,OAAO,EAAE,CAAA;IAC/B,CAAC;IAEQ,GAAG,CAAC,GAAM,EAAE,KAAQ,EAAE,QAAoB;QACjD,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;QAC9B,OAAO,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IAC9B,CAAC;IAEQ,MAAM,CAAC,GAAM;QACpB,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;QACtB,OAAO,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IAC1B,CAAC;IAES,YAAY,CAAC,GAAM;QAC3B,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QACtC,IAAI,OAAO;YAAE,YAAY,CAAC,OAAO,CAAC,CAAA;QAClC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IAC3B,CAAC;IAES,UAAU,CAAC,GAAM,EAAE,QAAqB;QAChD,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAA;QACtB,IAAI,CAAC,QAAQ,CAAC,GAAG,CACf,GAAG,EACH,UAAU,CAAC,GAAG,EAAE;YACd,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;YACvB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;YAChB,IAAI,QAAQ;gBAAE,QAAQ,EAAE,CAAA;YACxB,IAAI,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;gBAC9B,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;aACpC;QACH,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CACzB,CAAA;IACH,CAAC;CACF;AAvCD,gDAuCC"}
@@ -1,15 +0,0 @@
1
- /**
2
- * Returns a value from a map, while setting a given default value before returning it, if the key is not present.
3
- * @param map map to get value from
4
- * @param key key to get value for
5
- * @param callback callback to set the value if key is not present
6
- * @template K - The type of the keys in the Map.
7
- * @returns The value retrieved from the Map or generated by the callback function.
8
- * @example ```ts
9
- * const map = new Map<string, number>()
10
- * map.set('key', 1)
11
- * mapGetOrElse(map, 'nonexistentKey', () => 2) // Output: 2
12
- * ```
13
- */
14
- export declare function mapGetOrElse<K, V>(map: Map<K, V>, key: K, callback: (key: K) => V): V;
15
- //# sourceMappingURL=mapGetOrElse.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mapGetOrElse.d.ts","sourceRoot":"","sources":["../../../../../packages/map/src/lib/mapGetOrElse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAMrF"}
@@ -1,26 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapGetOrElse = void 0;
4
- /**
5
- * Returns a value from a map, while setting a given default value before returning it, if the key is not present.
6
- * @param map map to get value from
7
- * @param key key to get value for
8
- * @param callback callback to set the value if key is not present
9
- * @template K - The type of the keys in the Map.
10
- * @returns The value retrieved from the Map or generated by the callback function.
11
- * @example ```ts
12
- * const map = new Map<string, number>()
13
- * map.set('key', 1)
14
- * mapGetOrElse(map, 'nonexistentKey', () => 2) // Output: 2
15
- * ```
16
- */
17
- function mapGetOrElse(map, key, callback) {
18
- let value = map.get(key);
19
- if (value !== undefined)
20
- return value;
21
- value = callback(key);
22
- map.set(key, value);
23
- return value;
24
- }
25
- exports.mapGetOrElse = mapGetOrElse;
26
- //# sourceMappingURL=mapGetOrElse.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mapGetOrElse.js","sourceRoot":"","sources":["../../../../../packages/map/src/lib/mapGetOrElse.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;GAYG;AACH,SAAgB,YAAY,CAAO,GAAc,EAAE,GAAM,EAAE,QAAuB;IAChF,IAAI,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACxB,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,KAAK,CAAA;IACrC,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAA;IACrB,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IACnB,OAAO,KAAK,CAAA;AACd,CAAC;AAND,oCAMC"}
@@ -1,15 +0,0 @@
1
- /**
2
- * Reverses the order of entries in a Map.
3
- * @remarks This function creates a new Map with the entries of the original Map in reverse order.
4
- * The original Map is not modified.
5
- * @typeparam K - The type of the keys in the Map.
6
- * @typeparam V - The type of the values in the Map.
7
- * @param map The Map to reverse.
8
- * @returns A new Map with the entries of the original Map in reverse order.
9
- * @example ```ts
10
- * mapReverse(new Map([['a', 1], ['b', 2], ['c', 3]]));
11
- * //=> [['c', 3], ['b', 2], ['a', 1]]
12
- * ```
13
- */
14
- export declare function mapReverse<K, V>(map: Map<K, V>): Map<K, V>;
15
- //# sourceMappingURL=mapReverse.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mapReverse.d.ts","sourceRoot":"","sources":["../../../../../packages/map/src/lib/mapReverse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAE1D"}
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapReverse = void 0;
4
- /**
5
- * Reverses the order of entries in a Map.
6
- * @remarks This function creates a new Map with the entries of the original Map in reverse order.
7
- * The original Map is not modified.
8
- * @typeparam K - The type of the keys in the Map.
9
- * @typeparam V - The type of the values in the Map.
10
- * @param map The Map to reverse.
11
- * @returns A new Map with the entries of the original Map in reverse order.
12
- * @example ```ts
13
- * mapReverse(new Map([['a', 1], ['b', 2], ['c', 3]]));
14
- * //=> [['c', 3], ['b', 2], ['a', 1]]
15
- * ```
16
- */
17
- function mapReverse(map) {
18
- return new Map([...map.entries()].reverse());
19
- }
20
- exports.mapReverse = mapReverse;
21
- //# sourceMappingURL=mapReverse.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mapReverse.js","sourceRoot":"","sources":["../../../../../packages/map/src/lib/mapReverse.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;GAYG;AACH,SAAgB,UAAU,CAAO,GAAc;IAC7C,OAAO,IAAI,GAAG,CAAO,CAAC,GAAG,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAA;AACpD,CAAC;AAFD,gCAEC"}
@@ -1,20 +0,0 @@
1
- /**
2
- * Updates the value for the given key in the map using the provided function.
3
- * If the key does not exist in the map, the function will be called with `undefined` as the value.
4
- * @template K - The type of the keys in the map.
5
- * @param map - The map to update.
6
- * @param key - The key of the value to update.
7
- * @param fun - The function to use to update the value.
8
- * This function takes the current value (or `undefined` if the key does not exist) and the key as arguments,
9
- * and should return the new value.
10
- * @returns - The new value for the key.
11
- * @example ```ts
12
- * const map = new Map<string, number>()
13
- * map.set('a', 1)
14
- * mapUpdate(map, 'a', (value, key) => value !== undefined ? value + 1 : 0)
15
- * map.get('a')
16
- * //=> 2
17
- * ```
18
- */
19
- export declare function mapUpdate<K, V>(map: Map<K, V>, key: K, fun: (value: V | undefined, key: K) => V): V;
20
- //# sourceMappingURL=mapUpdate.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mapUpdate.d.ts","sourceRoot":"","sources":["../../../../../packages/map/src/lib/mapUpdate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,SAAS,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAInG"}
@@ -1,28 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapUpdate = void 0;
4
- /**
5
- * Updates the value for the given key in the map using the provided function.
6
- * If the key does not exist in the map, the function will be called with `undefined` as the value.
7
- * @template K - The type of the keys in the map.
8
- * @param map - The map to update.
9
- * @param key - The key of the value to update.
10
- * @param fun - The function to use to update the value.
11
- * This function takes the current value (or `undefined` if the key does not exist) and the key as arguments,
12
- * and should return the new value.
13
- * @returns - The new value for the key.
14
- * @example ```ts
15
- * const map = new Map<string, number>()
16
- * map.set('a', 1)
17
- * mapUpdate(map, 'a', (value, key) => value !== undefined ? value + 1 : 0)
18
- * map.get('a')
19
- * //=> 2
20
- * ```
21
- */
22
- function mapUpdate(map, key, fun) {
23
- const value = fun(map.get(key), key);
24
- map.set(key, value);
25
- return value;
26
- }
27
- exports.mapUpdate = mapUpdate;
28
- //# sourceMappingURL=mapUpdate.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mapUpdate.js","sourceRoot":"","sources":["../../../../../packages/map/src/lib/mapUpdate.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,SAAS,CAAO,GAAc,EAAE,GAAM,EAAE,GAAwC;IAC9F,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,CAAA;IACpC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IACnB,OAAO,KAAK,CAAA;AACd,CAAC;AAJD,8BAIC"}