@bemoje/map 1.0.0 → 1.2.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/README.md +100 -0
- package/lib/ExtMap.cjs +104 -0
- package/lib/ExtMap.d.cts +110 -0
- package/lib/ExtMap.d.ts +110 -0
- package/lib/ExtMap.js +101 -0
- package/lib/TimeoutWeakMap.cjs +120 -0
- package/lib/TimeoutWeakMap.d.cts +75 -0
- package/lib/TimeoutWeakMap.d.ts +75 -0
- package/lib/TimeoutWeakMap.js +118 -0
- package/lib/countUniques.cjs +18 -0
- package/lib/countUniques.d.cts +9 -0
- package/lib/countUniques.d.ts +9 -0
- package/lib/countUniques.js +15 -0
- package/lib/entriesArray.cjs +10 -0
- package/lib/entriesArray.d.cts +19 -0
- package/lib/entriesArray.d.ts +19 -0
- package/lib/entriesArray.js +8 -0
- package/lib/index.cjs +110 -0
- package/lib/index.d.cts +17 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.js +15 -0
- package/lib/isGenericMap.cjs +16 -0
- package/lib/isGenericMap.d.cts +6 -0
- package/lib/isGenericMap.d.ts +6 -0
- package/lib/isGenericMap.js +14 -0
- package/lib/keysArray.cjs +10 -0
- package/lib/keysArray.d.cts +19 -0
- package/lib/keysArray.d.ts +19 -0
- package/lib/keysArray.js +8 -0
- package/lib/mapGetOrDefault.cjs +16 -0
- package/lib/mapGetOrDefault.d.cts +8 -0
- package/lib/mapGetOrDefault.d.ts +8 -0
- package/lib/mapGetOrDefault.js +14 -0
- package/lib/mapLoad.cjs +13 -0
- package/lib/mapLoad.d.cts +11 -0
- package/lib/mapLoad.d.ts +11 -0
- package/lib/mapLoad.js +11 -0
- package/lib/mapReverse.cjs +15 -0
- package/lib/mapReverse.d.cts +15 -0
- package/lib/mapReverse.d.ts +15 -0
- package/lib/mapReverse.js +12 -0
- package/lib/mapUpdate.cjs +12 -0
- package/lib/mapUpdate.d.cts +13 -0
- package/lib/mapUpdate.d.ts +13 -0
- package/lib/mapUpdate.js +10 -0
- package/lib/sort.cjs +15 -0
- package/lib/sort.d.cts +25 -0
- package/lib/sort.d.ts +25 -0
- package/lib/sort.js +13 -0
- package/lib/sortByKeys.cjs +17 -0
- package/lib/sortByKeys.d.cts +24 -0
- package/lib/sortByKeys.d.ts +24 -0
- package/lib/sortByKeys.js +15 -0
- package/lib/sortByValues.cjs +17 -0
- package/lib/sortByValues.d.cts +25 -0
- package/lib/sortByValues.d.ts +25 -0
- package/lib/sortByValues.js +15 -0
- package/lib/toMap.cjs +10 -0
- package/lib/toMap.d.cts +20 -0
- package/lib/toMap.d.ts +20 -0
- package/lib/toMap.js +8 -0
- package/lib/valuesArray.cjs +10 -0
- package/lib/valuesArray.d.cts +19 -0
- package/lib/valuesArray.d.ts +19 -0
- package/lib/valuesArray.js +8 -0
- package/package.json +250 -19
- package/index.d.ts +0 -348
- package/index.mjs +0 -446
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# @bemoje/map
|
|
2
|
+
|
|
3
|
+
Extended Map class and utilities for sorting, filtering, mapping, and managing key-value data.
|
|
4
|
+
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](https://nodejs.org/api/esm.html)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @bemoje/map
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
### ExtMap
|
|
17
|
+
|
|
18
|
+
An extended Map with built-in sorting, filtering, mapping, and chaining:
|
|
19
|
+
|
|
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
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Standalone Utilities
|
|
68
|
+
|
|
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
|
+
```
|
|
82
|
+
|
|
83
|
+
## API Reference
|
|
84
|
+
|
|
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/lib/ExtMap.cjs
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var composition = require('@mono/composition');
|
|
4
|
+
var defineMethod = require('@mono/object/defineMethod');
|
|
5
|
+
var entriesArray = require('./entriesArray');
|
|
6
|
+
var entriesOf = require('@mono/object/entriesOf');
|
|
7
|
+
var filterIterableValues = require('@mono/iter/filterIterableValues');
|
|
8
|
+
var util = require('util');
|
|
9
|
+
var iterTools = require('iter-tools');
|
|
10
|
+
var keysArray = require('./keysArray');
|
|
11
|
+
var mapGetOrDefault = require('./mapGetOrDefault');
|
|
12
|
+
var mapIterableKeys = require('@mono/iter/mapIterableKeys');
|
|
13
|
+
var mapIterableValues = require('@mono/iter/mapIterableValues');
|
|
14
|
+
var mapLoad = require('./mapLoad');
|
|
15
|
+
var mapReverse = require('./mapReverse');
|
|
16
|
+
var mapUpdate = require('./mapUpdate');
|
|
17
|
+
var reduceIterableValues = require('@mono/iter/reduceIterableValues');
|
|
18
|
+
var sort = require('./sort');
|
|
19
|
+
var sortByKeys = require('./sortByKeys');
|
|
20
|
+
var sortByValues = require('./sortByValues');
|
|
21
|
+
var thisify = require('@mono/fn/thisify');
|
|
22
|
+
var toMap = require('./toMap');
|
|
23
|
+
var toObjectIterable = require('@mono/iter/toObjectIterable');
|
|
24
|
+
var transformReturnValue = require('@mono/fn/transformReturnValue');
|
|
25
|
+
var valuesArray = require('./valuesArray');
|
|
26
|
+
|
|
27
|
+
var __defProp = Object.defineProperty;
|
|
28
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
29
|
+
class ExtMap extends composition.View {
|
|
30
|
+
static {
|
|
31
|
+
__name(this, "ExtMap");
|
|
32
|
+
}
|
|
33
|
+
constructor(arg) {
|
|
34
|
+
super(new Map(iterTools.isIterable(arg) ? arg : arg ? entriesOf.entriesOf(arg) : void 0));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new ExtMap instance from a regular object.
|
|
38
|
+
*
|
|
39
|
+
* @param obj - An object whose key-value pairs will be used to initialize the map
|
|
40
|
+
* @returns A new ExtMap instance containing the entries from the object
|
|
41
|
+
*/
|
|
42
|
+
static fromObject(obj) {
|
|
43
|
+
return new ExtMap(Object.entries(obj));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new ExtMap instance from an iterable of key-value pairs.
|
|
47
|
+
*
|
|
48
|
+
* @param entries - An iterable of key-value pairs to initialize the map
|
|
49
|
+
* @returns A new ExtMap instance containing the provided entries
|
|
50
|
+
*/
|
|
51
|
+
static fromIterable(entries) {
|
|
52
|
+
return new ExtMap(entries);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Creates a shallow copy of the map.
|
|
56
|
+
*
|
|
57
|
+
* @returns A new ExtMap instance with the same entries
|
|
58
|
+
*/
|
|
59
|
+
clone() {
|
|
60
|
+
return new ExtMap(this);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Converts the map to a JSON-serializable format.
|
|
64
|
+
*
|
|
65
|
+
* @returns Array of key-value pairs suitable for JSON serialization
|
|
66
|
+
*/
|
|
67
|
+
toJSON() {
|
|
68
|
+
return [
|
|
69
|
+
...this.entries()
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Custom inspection method for better debugging output.
|
|
74
|
+
*
|
|
75
|
+
* @param _depth - The depth of inspection
|
|
76
|
+
* @param _options - Options for inspection
|
|
77
|
+
* @returns A string representation of the map
|
|
78
|
+
*/
|
|
79
|
+
[util.inspect.custom](_depth, _options) {
|
|
80
|
+
return util.inspect(this.target, {
|
|
81
|
+
breakLength: 50
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
composition.inheritProxifiedPrototype(ExtMap, Map, []);
|
|
86
|
+
defineMethod.defineMethod(ExtMap.prototype, "load", thisify.thisify(mapLoad.mapLoad));
|
|
87
|
+
defineMethod.defineMethod(ExtMap.prototype, "sort", thisify.thisify(sort.sort));
|
|
88
|
+
defineMethod.defineMethod(ExtMap.prototype, "sortByKeys", thisify.thisify(sortByKeys.sortByKeys));
|
|
89
|
+
defineMethod.defineMethod(ExtMap.prototype, "sortByValues", thisify.thisify(sortByValues.sortByValues));
|
|
90
|
+
defineMethod.defineMethod(ExtMap.prototype, "reverse", thisify.thisify(mapReverse.mapReverse));
|
|
91
|
+
defineMethod.defineMethod(ExtMap.prototype, "update", thisify.thisify(mapUpdate.mapUpdate));
|
|
92
|
+
defineMethod.defineMethod(ExtMap.prototype, "getOrDefault", thisify.thisify(mapGetOrDefault.mapGetOrDefault));
|
|
93
|
+
defineMethod.defineMethod(ExtMap.prototype, "keysArray", thisify.thisify(keysArray.keysArray));
|
|
94
|
+
defineMethod.defineMethod(ExtMap.prototype, "valuesArray", thisify.thisify(valuesArray.valuesArray));
|
|
95
|
+
defineMethod.defineMethod(ExtMap.prototype, "entriesArray", thisify.thisify(entriesArray.entriesArray));
|
|
96
|
+
defineMethod.defineMethod(ExtMap.prototype, "toObject", thisify.thisify(toObjectIterable.toObjectIterable));
|
|
97
|
+
defineMethod.defineMethod(ExtMap.prototype, "toMap", thisify.thisify(toMap.toMap));
|
|
98
|
+
defineMethod.defineMethod(ExtMap.prototype, "reduce", thisify.thisify(reduceIterableValues.reduceIterableValues));
|
|
99
|
+
defineMethod.defineMethod(ExtMap.prototype, "map", thisify.thisify(transformReturnValue.transformReturnValue(mapIterableValues.mapIterableValues, ExtMap.fromIterable)));
|
|
100
|
+
defineMethod.defineMethod(ExtMap.prototype, "mapKeys", thisify.thisify(transformReturnValue.transformReturnValue(mapIterableKeys.mapIterableKeys, ExtMap.fromIterable)));
|
|
101
|
+
defineMethod.defineMethod(ExtMap.prototype, "mapValues", thisify.thisify(transformReturnValue.transformReturnValue(mapIterableValues.mapIterableValues, ExtMap.fromIterable)));
|
|
102
|
+
defineMethod.defineMethod(ExtMap.prototype, "filter", thisify.thisify(transformReturnValue.transformReturnValue(filterIterableValues.filterIterableValues, ExtMap.fromIterable)));
|
|
103
|
+
|
|
104
|
+
exports.ExtMap = ExtMap;
|
package/lib/ExtMap.d.cts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { inspect, InspectOptions } from '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
|
+
declare module './ExtMap' {
|
|
26
|
+
interface ExtMap<K = any, V = any> extends Map<K, V> {
|
|
27
|
+
load(entries: Iterable<[K, V]>): this;
|
|
28
|
+
sort(compare: (a: [K, V], b: [K, V]) => number): this;
|
|
29
|
+
sortByKeys(compare: (a: K, b: K) => number): this;
|
|
30
|
+
sortByValues(compare: (a: V, b: V) => number): this;
|
|
31
|
+
reverse(): this;
|
|
32
|
+
update(key: K, update: (value: V | undefined, key: K, map: this) => V): this;
|
|
33
|
+
getOrDefault(key: K, factory: (key: K, map: this) => V): V;
|
|
34
|
+
keysArray(): K[];
|
|
35
|
+
valuesArray(): V[];
|
|
36
|
+
entriesArray(): [K, V][];
|
|
37
|
+
toMap(): Map<K, V>;
|
|
38
|
+
toObject<K extends string, V>(): Record<K, V>;
|
|
39
|
+
toObject<K, V>(): Record<Extract<K, PropertyKey> | string, V>;
|
|
40
|
+
reduce<R>(reducer: (accumulator: R, value: V, key: K) => R, initialValue: R): R;
|
|
41
|
+
map<NewK, NewV>(mapper: (value: V, key: K) => [NewK, NewV]): ExtMap<NewK, NewV>;
|
|
42
|
+
mapKeys<NewK>(mapper: (key: K, value: V) => NewK): ExtMap<NewK, V>;
|
|
43
|
+
mapValues<NewV>(mapper: (value: V, key: K) => NewV): ExtMap<K, NewV>;
|
|
44
|
+
filter(predicate: (value: V, key: K) => boolean): ExtMap<K, V>;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Minimal Extended Map class focused only on Map-specific utilities.
|
|
49
|
+
*
|
|
50
|
+
* @template K - The type of keys in the map
|
|
51
|
+
* @template V - The type of values in the map
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* import { map as iterMap, filter } from 'iter-tools'
|
|
56
|
+
*
|
|
57
|
+
* const extMap = new ExtMap<string, number>()
|
|
58
|
+
* .load([['a', 1], ['b', 2], ['c', 3]])
|
|
59
|
+
* .update('a', v => (v ?? 0) + 10)
|
|
60
|
+
* .sortByValues((a, b) => a - b)
|
|
61
|
+
*
|
|
62
|
+
* // Use iter-tools for transformations
|
|
63
|
+
* const doubled = new ExtMap<string, number>()
|
|
64
|
+
* .load(iterMap(([k, v]) => [k, v * 2], extMap))
|
|
65
|
+
*
|
|
66
|
+
* // Use iter-tools for filtering
|
|
67
|
+
* const filtered = new ExtMap<string, number>()
|
|
68
|
+
* .load(filter(([k, v]) => v > 5, extMap))
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare class ExtMap<K = any, V = any> extends View<Map<K, V>> implements Map<K, V> {
|
|
72
|
+
constructor(iterable?: Iterable<readonly [K, V]> | null | undefined);
|
|
73
|
+
constructor(obj?: Record<Extract<K, PropertyKey>, V>);
|
|
74
|
+
/**
|
|
75
|
+
* Creates a new ExtMap instance from a regular object.
|
|
76
|
+
*
|
|
77
|
+
* @param obj - An object whose key-value pairs will be used to initialize the map
|
|
78
|
+
* @returns A new ExtMap instance containing the entries from the object
|
|
79
|
+
*/
|
|
80
|
+
static fromObject<K extends PropertyKey, V>(obj: Record<K, V>): ExtMap<K, V>;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a new ExtMap instance from an iterable of key-value pairs.
|
|
83
|
+
*
|
|
84
|
+
* @param entries - An iterable of key-value pairs to initialize the map
|
|
85
|
+
* @returns A new ExtMap instance containing the provided entries
|
|
86
|
+
*/
|
|
87
|
+
static fromIterable<K, V>(entries: Iterable<readonly [K, V]>): ExtMap<K, V>;
|
|
88
|
+
/**
|
|
89
|
+
* Creates a shallow copy of the map.
|
|
90
|
+
*
|
|
91
|
+
* @returns A new ExtMap instance with the same entries
|
|
92
|
+
*/
|
|
93
|
+
clone(): ExtMap<K, V>;
|
|
94
|
+
/**
|
|
95
|
+
* Converts the map to a JSON-serializable format.
|
|
96
|
+
*
|
|
97
|
+
* @returns Array of key-value pairs suitable for JSON serialization
|
|
98
|
+
*/
|
|
99
|
+
toJSON(): [K, V][];
|
|
100
|
+
/**
|
|
101
|
+
* Custom inspection method for better debugging output.
|
|
102
|
+
*
|
|
103
|
+
* @param _depth - The depth of inspection
|
|
104
|
+
* @param _options - Options for inspection
|
|
105
|
+
* @returns A string representation of the map
|
|
106
|
+
*/
|
|
107
|
+
[inspect.custom](_depth: number, _options: InspectOptions): string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export { ExtMap };
|
package/lib/ExtMap.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { inspect, InspectOptions } from '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
|
+
declare module './ExtMap' {
|
|
26
|
+
interface ExtMap<K = any, V = any> extends Map<K, V> {
|
|
27
|
+
load(entries: Iterable<[K, V]>): this;
|
|
28
|
+
sort(compare: (a: [K, V], b: [K, V]) => number): this;
|
|
29
|
+
sortByKeys(compare: (a: K, b: K) => number): this;
|
|
30
|
+
sortByValues(compare: (a: V, b: V) => number): this;
|
|
31
|
+
reverse(): this;
|
|
32
|
+
update(key: K, update: (value: V | undefined, key: K, map: this) => V): this;
|
|
33
|
+
getOrDefault(key: K, factory: (key: K, map: this) => V): V;
|
|
34
|
+
keysArray(): K[];
|
|
35
|
+
valuesArray(): V[];
|
|
36
|
+
entriesArray(): [K, V][];
|
|
37
|
+
toMap(): Map<K, V>;
|
|
38
|
+
toObject<K extends string, V>(): Record<K, V>;
|
|
39
|
+
toObject<K, V>(): Record<Extract<K, PropertyKey> | string, V>;
|
|
40
|
+
reduce<R>(reducer: (accumulator: R, value: V, key: K) => R, initialValue: R): R;
|
|
41
|
+
map<NewK, NewV>(mapper: (value: V, key: K) => [NewK, NewV]): ExtMap<NewK, NewV>;
|
|
42
|
+
mapKeys<NewK>(mapper: (key: K, value: V) => NewK): ExtMap<NewK, V>;
|
|
43
|
+
mapValues<NewV>(mapper: (value: V, key: K) => NewV): ExtMap<K, NewV>;
|
|
44
|
+
filter(predicate: (value: V, key: K) => boolean): ExtMap<K, V>;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Minimal Extended Map class focused only on Map-specific utilities.
|
|
49
|
+
*
|
|
50
|
+
* @template K - The type of keys in the map
|
|
51
|
+
* @template V - The type of values in the map
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* import { map as iterMap, filter } from 'iter-tools'
|
|
56
|
+
*
|
|
57
|
+
* const extMap = new ExtMap<string, number>()
|
|
58
|
+
* .load([['a', 1], ['b', 2], ['c', 3]])
|
|
59
|
+
* .update('a', v => (v ?? 0) + 10)
|
|
60
|
+
* .sortByValues((a, b) => a - b)
|
|
61
|
+
*
|
|
62
|
+
* // Use iter-tools for transformations
|
|
63
|
+
* const doubled = new ExtMap<string, number>()
|
|
64
|
+
* .load(iterMap(([k, v]) => [k, v * 2], extMap))
|
|
65
|
+
*
|
|
66
|
+
* // Use iter-tools for filtering
|
|
67
|
+
* const filtered = new ExtMap<string, number>()
|
|
68
|
+
* .load(filter(([k, v]) => v > 5, extMap))
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare class ExtMap<K = any, V = any> extends View<Map<K, V>> implements Map<K, V> {
|
|
72
|
+
constructor(iterable?: Iterable<readonly [K, V]> | null | undefined);
|
|
73
|
+
constructor(obj?: Record<Extract<K, PropertyKey>, V>);
|
|
74
|
+
/**
|
|
75
|
+
* Creates a new ExtMap instance from a regular object.
|
|
76
|
+
*
|
|
77
|
+
* @param obj - An object whose key-value pairs will be used to initialize the map
|
|
78
|
+
* @returns A new ExtMap instance containing the entries from the object
|
|
79
|
+
*/
|
|
80
|
+
static fromObject<K extends PropertyKey, V>(obj: Record<K, V>): ExtMap<K, V>;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a new ExtMap instance from an iterable of key-value pairs.
|
|
83
|
+
*
|
|
84
|
+
* @param entries - An iterable of key-value pairs to initialize the map
|
|
85
|
+
* @returns A new ExtMap instance containing the provided entries
|
|
86
|
+
*/
|
|
87
|
+
static fromIterable<K, V>(entries: Iterable<readonly [K, V]>): ExtMap<K, V>;
|
|
88
|
+
/**
|
|
89
|
+
* Creates a shallow copy of the map.
|
|
90
|
+
*
|
|
91
|
+
* @returns A new ExtMap instance with the same entries
|
|
92
|
+
*/
|
|
93
|
+
clone(): ExtMap<K, V>;
|
|
94
|
+
/**
|
|
95
|
+
* Converts the map to a JSON-serializable format.
|
|
96
|
+
*
|
|
97
|
+
* @returns Array of key-value pairs suitable for JSON serialization
|
|
98
|
+
*/
|
|
99
|
+
toJSON(): [K, V][];
|
|
100
|
+
/**
|
|
101
|
+
* Custom inspection method for better debugging output.
|
|
102
|
+
*
|
|
103
|
+
* @param _depth - The depth of inspection
|
|
104
|
+
* @param _options - Options for inspection
|
|
105
|
+
* @returns A string representation of the map
|
|
106
|
+
*/
|
|
107
|
+
[inspect.custom](_depth: number, _options: InspectOptions): string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export { ExtMap };
|
package/lib/ExtMap.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { __name } from './chunk-SHUYVCID.js';
|
|
2
|
+
import { View, inheritProxifiedPrototype } from '@mono/composition';
|
|
3
|
+
import { defineMethod } from '@mono/object/defineMethod';
|
|
4
|
+
import { entriesArray } from './entriesArray';
|
|
5
|
+
import { entriesOf } from '@mono/object/entriesOf';
|
|
6
|
+
import { filterIterableValues } from '@mono/iter/filterIterableValues';
|
|
7
|
+
import { inspect } from 'util';
|
|
8
|
+
import { isIterable } from 'iter-tools';
|
|
9
|
+
import { keysArray } from './keysArray';
|
|
10
|
+
import { mapGetOrDefault } from './mapGetOrDefault';
|
|
11
|
+
import { mapIterableKeys } from '@mono/iter/mapIterableKeys';
|
|
12
|
+
import { mapIterableValues } from '@mono/iter/mapIterableValues';
|
|
13
|
+
import { mapLoad } from './mapLoad';
|
|
14
|
+
import { mapReverse } from './mapReverse';
|
|
15
|
+
import { mapUpdate } from './mapUpdate';
|
|
16
|
+
import { reduceIterableValues } from '@mono/iter/reduceIterableValues';
|
|
17
|
+
import { sort } from './sort';
|
|
18
|
+
import { sortByKeys } from './sortByKeys';
|
|
19
|
+
import { sortByValues } from './sortByValues';
|
|
20
|
+
import { thisify } from '@mono/fn/thisify';
|
|
21
|
+
import { toMap } from './toMap';
|
|
22
|
+
import { toObjectIterable } from '@mono/iter/toObjectIterable';
|
|
23
|
+
import { transformReturnValue } from '@mono/fn/transformReturnValue';
|
|
24
|
+
import { valuesArray } from './valuesArray';
|
|
25
|
+
|
|
26
|
+
class ExtMap extends View {
|
|
27
|
+
static {
|
|
28
|
+
__name(this, "ExtMap");
|
|
29
|
+
}
|
|
30
|
+
constructor(arg) {
|
|
31
|
+
super(new Map(isIterable(arg) ? arg : arg ? entriesOf(arg) : void 0));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Creates a new ExtMap instance from a regular object.
|
|
35
|
+
*
|
|
36
|
+
* @param obj - An object whose key-value pairs will be used to initialize the map
|
|
37
|
+
* @returns A new ExtMap instance containing the entries from the object
|
|
38
|
+
*/
|
|
39
|
+
static fromObject(obj) {
|
|
40
|
+
return new ExtMap(Object.entries(obj));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new ExtMap instance from an iterable of key-value pairs.
|
|
44
|
+
*
|
|
45
|
+
* @param entries - An iterable of key-value pairs to initialize the map
|
|
46
|
+
* @returns A new ExtMap instance containing the provided entries
|
|
47
|
+
*/
|
|
48
|
+
static fromIterable(entries) {
|
|
49
|
+
return new ExtMap(entries);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Creates a shallow copy of the map.
|
|
53
|
+
*
|
|
54
|
+
* @returns A new ExtMap instance with the same entries
|
|
55
|
+
*/
|
|
56
|
+
clone() {
|
|
57
|
+
return new ExtMap(this);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Converts the map to a JSON-serializable format.
|
|
61
|
+
*
|
|
62
|
+
* @returns Array of key-value pairs suitable for JSON serialization
|
|
63
|
+
*/
|
|
64
|
+
toJSON() {
|
|
65
|
+
return [
|
|
66
|
+
...this.entries()
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Custom inspection method for better debugging output.
|
|
71
|
+
*
|
|
72
|
+
* @param _depth - The depth of inspection
|
|
73
|
+
* @param _options - Options for inspection
|
|
74
|
+
* @returns A string representation of the map
|
|
75
|
+
*/
|
|
76
|
+
[inspect.custom](_depth, _options) {
|
|
77
|
+
return inspect(this.target, {
|
|
78
|
+
breakLength: 50
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
inheritProxifiedPrototype(ExtMap, Map, []);
|
|
83
|
+
defineMethod(ExtMap.prototype, "load", thisify(mapLoad));
|
|
84
|
+
defineMethod(ExtMap.prototype, "sort", thisify(sort));
|
|
85
|
+
defineMethod(ExtMap.prototype, "sortByKeys", thisify(sortByKeys));
|
|
86
|
+
defineMethod(ExtMap.prototype, "sortByValues", thisify(sortByValues));
|
|
87
|
+
defineMethod(ExtMap.prototype, "reverse", thisify(mapReverse));
|
|
88
|
+
defineMethod(ExtMap.prototype, "update", thisify(mapUpdate));
|
|
89
|
+
defineMethod(ExtMap.prototype, "getOrDefault", thisify(mapGetOrDefault));
|
|
90
|
+
defineMethod(ExtMap.prototype, "keysArray", thisify(keysArray));
|
|
91
|
+
defineMethod(ExtMap.prototype, "valuesArray", thisify(valuesArray));
|
|
92
|
+
defineMethod(ExtMap.prototype, "entriesArray", thisify(entriesArray));
|
|
93
|
+
defineMethod(ExtMap.prototype, "toObject", thisify(toObjectIterable));
|
|
94
|
+
defineMethod(ExtMap.prototype, "toMap", thisify(toMap));
|
|
95
|
+
defineMethod(ExtMap.prototype, "reduce", thisify(reduceIterableValues));
|
|
96
|
+
defineMethod(ExtMap.prototype, "map", thisify(transformReturnValue(mapIterableValues, ExtMap.fromIterable)));
|
|
97
|
+
defineMethod(ExtMap.prototype, "mapKeys", thisify(transformReturnValue(mapIterableKeys, ExtMap.fromIterable)));
|
|
98
|
+
defineMethod(ExtMap.prototype, "mapValues", thisify(transformReturnValue(mapIterableValues, ExtMap.fromIterable)));
|
|
99
|
+
defineMethod(ExtMap.prototype, "filter", thisify(transformReturnValue(filterIterableValues, ExtMap.fromIterable)));
|
|
100
|
+
|
|
101
|
+
export { ExtMap };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
+
class TimeoutWeakMap {
|
|
6
|
+
static {
|
|
7
|
+
__name(this, "TimeoutWeakMap");
|
|
8
|
+
}
|
|
9
|
+
timeoutMs;
|
|
10
|
+
wmap = /* @__PURE__ */ new WeakMap();
|
|
11
|
+
/**
|
|
12
|
+
* @param timeoutMs Default timeout in milliseconds for entries
|
|
13
|
+
*/
|
|
14
|
+
constructor(timeoutMs) {
|
|
15
|
+
this.timeoutMs = timeoutMs;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves the value for the given key and refreshes its timeout.
|
|
19
|
+
*
|
|
20
|
+
* @param key The key to retrieve
|
|
21
|
+
* @returns The value if found, undefined otherwise
|
|
22
|
+
*/
|
|
23
|
+
get(key) {
|
|
24
|
+
const vt = this.wmap.get(key);
|
|
25
|
+
if (!vt) {
|
|
26
|
+
return void 0;
|
|
27
|
+
}
|
|
28
|
+
const [value, timeout] = vt;
|
|
29
|
+
timeout.refresh();
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Sets a key-value pair with an optional custom timeout.
|
|
34
|
+
*
|
|
35
|
+
* @param key The key to set
|
|
36
|
+
* @param value The value to associate with the key
|
|
37
|
+
* @param timeoutMs Optional timeout override for this entry
|
|
38
|
+
* @returns This instance for chaining
|
|
39
|
+
*/
|
|
40
|
+
set(key, value, timeoutMs) {
|
|
41
|
+
this.wmap.set(key, [
|
|
42
|
+
value,
|
|
43
|
+
setTimeout(() => {
|
|
44
|
+
this.delete(key);
|
|
45
|
+
}, timeoutMs ?? this.timeoutMs).unref()
|
|
46
|
+
]);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Removes a key-value pair and clears its timeout.
|
|
51
|
+
*
|
|
52
|
+
* @param key The key to delete
|
|
53
|
+
* @returns True if the key existed and was deleted, false otherwise
|
|
54
|
+
*/
|
|
55
|
+
delete(key) {
|
|
56
|
+
const vt = this.wmap.get(key);
|
|
57
|
+
if (!vt) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const timeout = vt[1];
|
|
61
|
+
clearTimeout(timeout);
|
|
62
|
+
return this.wmap.delete(key);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Checks if a key exists in the map.
|
|
66
|
+
* Does not refresh the timeout.
|
|
67
|
+
*
|
|
68
|
+
* @param key The key to check
|
|
69
|
+
* @returns True if the key exists, false otherwise
|
|
70
|
+
*/
|
|
71
|
+
has(key) {
|
|
72
|
+
return this.wmap.has(key);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Loads multiple key-value pairs into the map.
|
|
76
|
+
*
|
|
77
|
+
* @param entries Iterable of key-value pairs to load
|
|
78
|
+
* @param timeoutMs Optional timeout override for all entries
|
|
79
|
+
* @returns This instance for chaining
|
|
80
|
+
*/
|
|
81
|
+
load(entries, timeoutMs) {
|
|
82
|
+
for (const [key, value] of entries) {
|
|
83
|
+
this.set(key, value, timeoutMs);
|
|
84
|
+
}
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Updates an existing value or creates a new one using an update function.
|
|
89
|
+
*
|
|
90
|
+
* @param key The key to update
|
|
91
|
+
* @param update Function that receives the current value and returns the new value
|
|
92
|
+
* @returns This instance for chaining
|
|
93
|
+
*/
|
|
94
|
+
update(key, update) {
|
|
95
|
+
return this.set(key, update(this.get(key), key, this));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Gets a value or creates it using a factory function if it doesn't exist.
|
|
99
|
+
* Refreshes timeout if the value exists.
|
|
100
|
+
*
|
|
101
|
+
* @param key The key to get or create
|
|
102
|
+
* @param factory Function to create the value if it doesn't exist
|
|
103
|
+
* @param timeoutMs Optional timeout override for new entries
|
|
104
|
+
* @returns The existing or newly created value
|
|
105
|
+
*/
|
|
106
|
+
getOrDefault(key, factory, timeoutMs) {
|
|
107
|
+
const vt = this.wmap.get(key);
|
|
108
|
+
if (vt) {
|
|
109
|
+
const [value, timeout] = vt;
|
|
110
|
+
timeout.refresh();
|
|
111
|
+
return value;
|
|
112
|
+
} else {
|
|
113
|
+
const value = factory(key, this);
|
|
114
|
+
this.set(key, value, timeoutMs);
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
exports.TimeoutWeakMap = TimeoutWeakMap;
|