@bemoje/map 1.0.1 → 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/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 +248 -18
- package/index.d.ts +0 -364
- package/index.mjs +0 -475
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;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A WeakMap with automatic timeout-based expiry for entries.
|
|
3
|
+
*
|
|
4
|
+
* Entries are automatically removed after a specified timeout period.
|
|
5
|
+
* Accessing an entry refreshes its timeout, extending its lifetime.
|
|
6
|
+
* This is useful for caching scenarios where you want automatic cleanup
|
|
7
|
+
* of unused entries while keeping frequently accessed ones alive.
|
|
8
|
+
*/
|
|
9
|
+
declare class TimeoutWeakMap<K extends object, V> {
|
|
10
|
+
readonly timeoutMs: number;
|
|
11
|
+
protected readonly wmap: WeakMap<K, [V, NodeJS.Timeout]>;
|
|
12
|
+
/**
|
|
13
|
+
* @param timeoutMs Default timeout in milliseconds for entries
|
|
14
|
+
*/
|
|
15
|
+
constructor(timeoutMs: number);
|
|
16
|
+
/**
|
|
17
|
+
* Retrieves the value for the given key and refreshes its timeout.
|
|
18
|
+
*
|
|
19
|
+
* @param key The key to retrieve
|
|
20
|
+
* @returns The value if found, undefined otherwise
|
|
21
|
+
*/
|
|
22
|
+
get(key: K): V | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Sets a key-value pair with an optional custom timeout.
|
|
25
|
+
*
|
|
26
|
+
* @param key The key to set
|
|
27
|
+
* @param value The value to associate with the key
|
|
28
|
+
* @param timeoutMs Optional timeout override for this entry
|
|
29
|
+
* @returns This instance for chaining
|
|
30
|
+
*/
|
|
31
|
+
set(key: K, value: V, timeoutMs?: number): this;
|
|
32
|
+
/**
|
|
33
|
+
* Removes a key-value pair and clears its timeout.
|
|
34
|
+
*
|
|
35
|
+
* @param key The key to delete
|
|
36
|
+
* @returns True if the key existed and was deleted, false otherwise
|
|
37
|
+
*/
|
|
38
|
+
delete(key: K): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Checks if a key exists in the map.
|
|
41
|
+
* Does not refresh the timeout.
|
|
42
|
+
*
|
|
43
|
+
* @param key The key to check
|
|
44
|
+
* @returns True if the key exists, false otherwise
|
|
45
|
+
*/
|
|
46
|
+
has(key: K): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Loads multiple key-value pairs into the map.
|
|
49
|
+
*
|
|
50
|
+
* @param entries Iterable of key-value pairs to load
|
|
51
|
+
* @param timeoutMs Optional timeout override for all entries
|
|
52
|
+
* @returns This instance for chaining
|
|
53
|
+
*/
|
|
54
|
+
load(entries: Iterable<[K, V]>, timeoutMs?: number): this;
|
|
55
|
+
/**
|
|
56
|
+
* Updates an existing value or creates a new one using an update function.
|
|
57
|
+
*
|
|
58
|
+
* @param key The key to update
|
|
59
|
+
* @param update Function that receives the current value and returns the new value
|
|
60
|
+
* @returns This instance for chaining
|
|
61
|
+
*/
|
|
62
|
+
update(key: K, update: (value: V | undefined, key: K, map: this) => V): this;
|
|
63
|
+
/**
|
|
64
|
+
* Gets a value or creates it using a factory function if it doesn't exist.
|
|
65
|
+
* Refreshes timeout if the value exists.
|
|
66
|
+
*
|
|
67
|
+
* @param key The key to get or create
|
|
68
|
+
* @param factory Function to create the value if it doesn't exist
|
|
69
|
+
* @param timeoutMs Optional timeout override for new entries
|
|
70
|
+
* @returns The existing or newly created value
|
|
71
|
+
*/
|
|
72
|
+
getOrDefault(key: K, factory: (key: K, map: this) => V, timeoutMs?: number): V;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { TimeoutWeakMap };
|