@bemoje/map 1.0.0 → 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 +100 -0
- package/index.d.ts +26 -10
- package/index.mjs +48 -19
- package/package.json +3 -2
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/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { inspect, InspectOptions } from 'util';
|
|
1
|
+
import { inspect, InspectOptions } from 'node:util';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Interface for a View that wraps a target object.
|
|
@@ -22,10 +22,13 @@ declare class View<Target extends object> implements IView<Target> {
|
|
|
22
22
|
get target(): Target;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
type
|
|
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
|
+
};
|
|
26
29
|
|
|
27
30
|
declare module './ExtMap' {
|
|
28
|
-
interface ExtMap<K, V> extends Map<K, V> {
|
|
31
|
+
interface ExtMap<K = any, V = any> extends Map<K, V> {
|
|
29
32
|
load(entries: Iterable<[K, V]>): this;
|
|
30
33
|
sort(compare: (a: [K, V], b: [K, V]) => number): this;
|
|
31
34
|
sortByKeys(compare: (a: K, b: K) => number): this;
|
|
@@ -38,6 +41,7 @@ declare module './ExtMap' {
|
|
|
38
41
|
entriesArray(): [K, V][];
|
|
39
42
|
toMap(): Map<K, V>;
|
|
40
43
|
toObject<K extends string, V>(): Record<K, V>;
|
|
44
|
+
toObject<K, V>(): Record<Extract<K, PropertyKey> | string, V>;
|
|
41
45
|
reduce<R>(reducer: (accumulator: R, value: V, key: K) => R, initialValue: R): R;
|
|
42
46
|
map<NewK, NewV>(mapper: (value: V, key: K) => [NewK, NewV]): ExtMap<NewK, NewV>;
|
|
43
47
|
mapKeys<NewK>(mapper: (key: K, value: V) => NewK): ExtMap<NewK, V>;
|
|
@@ -71,8 +75,20 @@ declare module './ExtMap' {
|
|
|
71
75
|
*/
|
|
72
76
|
declare class ExtMap<K = any, V = any> extends View<Map<K, V>> implements Map<K, V> {
|
|
73
77
|
constructor(iterable?: Iterable<readonly [K, V]> | null | undefined);
|
|
74
|
-
constructor(obj?: Record<Extract<K,
|
|
75
|
-
|
|
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
|
+
*/
|
|
76
92
|
static fromIterable<K, V>(entries: Iterable<readonly [K, V]>): ExtMap<K, V>;
|
|
77
93
|
/**
|
|
78
94
|
* Creates a shallow copy of the map.
|
|
@@ -170,10 +186,10 @@ declare class TimeoutWeakMap<K extends object, V> {
|
|
|
170
186
|
getOrDefault(key: K, factory: (key: K, map: this) => V, timeoutMs?: number): V;
|
|
171
187
|
}
|
|
172
188
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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>;
|
|
177
193
|
|
|
178
194
|
/**
|
|
179
195
|
* Returns an array of all key-value pairs in the map.
|
|
@@ -345,4 +361,4 @@ declare function toMap<K, V>(ins: GenericMap<K, V, typeof Symbol.iterator>): Map
|
|
|
345
361
|
*/
|
|
346
362
|
declare function valuesArray<K, V>(ins: GenericMap<K, V, 'values'>): V[];
|
|
347
363
|
|
|
348
|
-
export { ExtMap,
|
|
364
|
+
export { ExtMap, TimeoutWeakMap, countUniques, entriesArray, isGenericMap, keysArray, mapGetOrDefault, mapLoad, mapReverse, mapUpdate, sort, sortByKeys, sortByValues, toMap, valuesArray };
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { isIterable, map, filter } from 'iter-tools';
|
|
1
|
+
import { isIterable, map, filter, reduce } from 'iter-tools';
|
|
2
2
|
import { inspect } from 'util';
|
|
3
|
+
import { MultiSet } from 'mnemonist';
|
|
3
4
|
|
|
4
5
|
var __defProp = Object.defineProperty;
|
|
5
6
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
@@ -93,18 +94,18 @@ function defineMethod(obj, key, value, des = {}) {
|
|
|
93
94
|
});
|
|
94
95
|
}
|
|
95
96
|
__name(defineMethod, "defineMethod");
|
|
96
|
-
function
|
|
97
|
+
function filterIterableEntries(mapLike, predicate) {
|
|
97
98
|
return filter(([k, v]) => {
|
|
98
99
|
return predicate(v, k);
|
|
99
100
|
}, mapLike);
|
|
100
101
|
}
|
|
101
|
-
__name(
|
|
102
|
-
function
|
|
102
|
+
__name(filterIterableEntries, "filterIterableEntries");
|
|
103
|
+
function mapIterableEntries(mapLike, transform) {
|
|
103
104
|
return map(([k, v]) => {
|
|
104
105
|
return transform(v, k);
|
|
105
106
|
}, mapLike);
|
|
106
107
|
}
|
|
107
|
-
__name(
|
|
108
|
+
__name(mapIterableEntries, "mapIterableEntries");
|
|
108
109
|
function mapIterableKeys(mapLike, transform) {
|
|
109
110
|
return map(([k, v]) => {
|
|
110
111
|
return [
|
|
@@ -124,18 +125,26 @@ function mapIterableValues(mapLike, transform) {
|
|
|
124
125
|
}
|
|
125
126
|
__name(mapIterableValues, "mapIterableValues");
|
|
126
127
|
|
|
127
|
-
// ../iter/src/
|
|
128
|
-
function
|
|
128
|
+
// ../iter/src/reduceIterableEntries.ts
|
|
129
|
+
function reduceIterableEntries(mapLike, reducer, initialValue) {
|
|
129
130
|
for (const [key, value] of mapLike) {
|
|
130
131
|
initialValue = reducer(initialValue, value, key);
|
|
131
132
|
}
|
|
132
133
|
return initialValue;
|
|
133
134
|
}
|
|
134
|
-
__name(
|
|
135
|
+
__name(reduceIterableEntries, "reduceIterableEntries");
|
|
135
136
|
|
|
136
137
|
// ../iter/src/toObjectIterable.ts
|
|
137
138
|
function toObjectIterable(mapLike) {
|
|
138
|
-
|
|
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;
|
|
139
148
|
}
|
|
140
149
|
__name(toObjectIterable, "toObjectIterable");
|
|
141
150
|
|
|
@@ -246,9 +255,21 @@ var ExtMap = class _ExtMap extends View {
|
|
|
246
255
|
constructor(arg) {
|
|
247
256
|
super(new Map(isIterable(arg) ? arg : arg ? entriesOf(arg) : void 0));
|
|
248
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
|
+
*/
|
|
249
264
|
static fromObject(obj) {
|
|
250
|
-
return new _ExtMap(
|
|
265
|
+
return new _ExtMap(Object.entries(obj));
|
|
251
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
|
+
*/
|
|
252
273
|
static fromIterable(entries) {
|
|
253
274
|
return new _ExtMap(entries);
|
|
254
275
|
}
|
|
@@ -284,16 +305,16 @@ var ExtMap = class _ExtMap extends View {
|
|
|
284
305
|
}
|
|
285
306
|
};
|
|
286
307
|
function thisify(fn) {
|
|
287
|
-
return function(...args) {
|
|
308
|
+
return /* @__PURE__ */ __name(function wrapped(...args) {
|
|
288
309
|
return fn(this, ...args);
|
|
289
|
-
};
|
|
310
|
+
}, "wrapped");
|
|
290
311
|
}
|
|
291
312
|
__name(thisify, "thisify");
|
|
292
313
|
function transformReturnValue(fn, transform) {
|
|
293
|
-
return function(...args) {
|
|
314
|
+
return /* @__PURE__ */ __name(function wrapped(...args) {
|
|
294
315
|
const result = fn.apply(this, args);
|
|
295
316
|
return transform(result);
|
|
296
|
-
};
|
|
317
|
+
}, "wrapped");
|
|
297
318
|
}
|
|
298
319
|
__name(transformReturnValue, "transformReturnValue");
|
|
299
320
|
inheritProxifiedPrototype(ExtMap, Map, []);
|
|
@@ -307,13 +328,13 @@ defineMethod(ExtMap.prototype, "getOrDefault", thisify(mapGetOrDefault));
|
|
|
307
328
|
defineMethod(ExtMap.prototype, "keysArray", thisify(keysArray));
|
|
308
329
|
defineMethod(ExtMap.prototype, "valuesArray", thisify(valuesArray));
|
|
309
330
|
defineMethod(ExtMap.prototype, "entriesArray", thisify(entriesArray));
|
|
310
|
-
defineMethod(ExtMap.prototype, "toMap", thisify(toMap));
|
|
311
331
|
defineMethod(ExtMap.prototype, "toObject", thisify(toObjectIterable));
|
|
312
|
-
defineMethod(ExtMap.prototype, "
|
|
313
|
-
defineMethod(ExtMap.prototype, "
|
|
332
|
+
defineMethod(ExtMap.prototype, "toMap", thisify(toMap));
|
|
333
|
+
defineMethod(ExtMap.prototype, "reduce", thisify(reduceIterableEntries));
|
|
334
|
+
defineMethod(ExtMap.prototype, "map", thisify(transformReturnValue(mapIterableEntries, ExtMap.fromIterable)));
|
|
314
335
|
defineMethod(ExtMap.prototype, "mapKeys", thisify(transformReturnValue(mapIterableKeys, ExtMap.fromIterable)));
|
|
315
336
|
defineMethod(ExtMap.prototype, "mapValues", thisify(transformReturnValue(mapIterableValues, ExtMap.fromIterable)));
|
|
316
|
-
defineMethod(ExtMap.prototype, "filter", thisify(transformReturnValue(
|
|
337
|
+
defineMethod(ExtMap.prototype, "filter", thisify(transformReturnValue(filterIterableEntries, ExtMap.fromIterable)));
|
|
317
338
|
|
|
318
339
|
// src/TimeoutWeakMap.ts
|
|
319
340
|
var TimeoutWeakMap = class {
|
|
@@ -430,6 +451,14 @@ var TimeoutWeakMap = class {
|
|
|
430
451
|
}
|
|
431
452
|
}
|
|
432
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");
|
|
433
462
|
|
|
434
463
|
// src/isGenericMap.ts
|
|
435
464
|
function isGenericMap(target, requiredProps = [
|
|
@@ -443,4 +472,4 @@ function isGenericMap(target, requiredProps = [
|
|
|
443
472
|
}
|
|
444
473
|
__name(isGenericMap, "isGenericMap");
|
|
445
474
|
|
|
446
|
-
export { ExtMap, TimeoutWeakMap, entriesArray, isGenericMap, keysArray, mapGetOrDefault, mapLoad, mapReverse, mapUpdate, sort, sortByKeys, sortByValues, toMap, valuesArray };
|
|
475
|
+
export { ExtMap, TimeoutWeakMap, countUniques, entriesArray, isGenericMap, keysArray, mapGetOrDefault, mapLoad, mapReverse, mapUpdate, sort, sortByKeys, sortByValues, toMap, valuesArray };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/map",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "map",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"iter-tools": "^7.5.4"
|
|
18
|
+
"iter-tools": "^7.5.4",
|
|
19
|
+
"mnemonist": "^0.40.3"
|
|
19
20
|
},
|
|
20
21
|
"publishConfig": {
|
|
21
22
|
"access": "public"
|