duo-map 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +217 -2
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +20 -0
- package/dist/index.d.mts +20 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +46 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kasukabe tsumugi
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,218 @@
|
|
|
1
|
-
#
|
|
1
|
+
# duo-map
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/duo-map) [](https://npmcharts.com/compare/duo-map,token-types?start=1200&interval=30)
|
|
4
|
+
[](https://opensource.org/licenses/MIT) [](https://www.codacy.com/app/Borewit/duo-map?utm_source=github.com&utm_medium=referral&utm_content=Borewit/duo-map&utm_campaign=Badge_Grade)
|
|
5
|
+
|
|
6
|
+
A `Map` that maps **two keys to a single value**. Think of it as a nested `Map<Map>` with a flat API — no manual `get`/`set` bookkeeping of the inner map.
|
|
7
|
+
|
|
8
|
+
For more awesome packages, check out [my homepage💛](https://baendlorel.github.io/?repoType=npm)
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- 🗝️ **Two keys, one lookup**: `get(key1, key2)` replaces the `map.get(a)?.get(b)` dance.
|
|
13
|
+
- 🌲 **Nested semantics**: entries are grouped by `key1`, but the API is flat.
|
|
14
|
+
- 🧹 **Self-cleaning**: deleting the last pair of a `key1` group removes the group itself.
|
|
15
|
+
- 💤 **Lazy inserts**: `getOrInsertComputed` only computes when the pair is actually missing.
|
|
16
|
+
- 🔒 **Type safe**: three independent generic parameters `DuoMap<K1, K2, V>`.
|
|
17
|
+
- ⚡ **Lightweight**: no dependencies, zero runtime overhead over plain nested `Map`s.
|
|
18
|
+
|
|
19
|
+
## Installation / Import
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install duo-map
|
|
23
|
+
# or
|
|
24
|
+
pnpm add duo-map
|
|
25
|
+
# or
|
|
26
|
+
yarn add duo-map
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { DuoMap } from 'duo-map';
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { DuoMap } from 'duo-map';
|
|
37
|
+
|
|
38
|
+
const map = new DuoMap<string, number, string>();
|
|
39
|
+
|
|
40
|
+
// set(key1, key2, value) — returns `this`, so calls chain
|
|
41
|
+
map.set('users', 1, 'alice');
|
|
42
|
+
map.set('users', 2, 'bob').set('posts', 1, 'hello');
|
|
43
|
+
|
|
44
|
+
// get(key1, key2)
|
|
45
|
+
map.get('users', 1); // 'alice'
|
|
46
|
+
map.get('users', 2); // 'bob'
|
|
47
|
+
map.get('users', 3); // undefined — no such key2
|
|
48
|
+
map.get('missing', 1); // undefined — no such key1
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The same `key2` under a different `key1` is a **different entry**, so namespaces stay independent:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
map.set('users', 1, 'alice');
|
|
55
|
+
map.set('posts', 1, 'hello');
|
|
56
|
+
|
|
57
|
+
map.get('users', 1); // 'alice'
|
|
58
|
+
map.get('posts', 1); // 'hello'
|
|
59
|
+
map.size; // 2 — not 1
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Memoizing with a lazy default
|
|
63
|
+
|
|
64
|
+
`getOrInsertComputed` runs the factory only when the pair is missing, which makes it a natural fit for memoization:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
type User = { id: number; name: string };
|
|
68
|
+
const cache = new DuoMap<string, number, User>();
|
|
69
|
+
|
|
70
|
+
function getUser(tenant: string, id: number): User {
|
|
71
|
+
return cache.getOrInsertComputed(tenant, id, () => fetchUserFromDb(tenant, id));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Computed once, then served from the cache.
|
|
75
|
+
getUser('acme', 1);
|
|
76
|
+
getUser('acme', 1);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
If the factory throws, nothing is written, so the next call retries instead of caching a failure:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
let attempts = 0;
|
|
83
|
+
const map = new DuoMap<string, string, number>();
|
|
84
|
+
|
|
85
|
+
expect(() =>
|
|
86
|
+
map.getOrInsertComputed('a', 'x', () => {
|
|
87
|
+
attempts++;
|
|
88
|
+
throw new Error('db down');
|
|
89
|
+
}),
|
|
90
|
+
).toThrow('db down');
|
|
91
|
+
|
|
92
|
+
map.has('a', 'x'); // false — no half-written entry
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Use `getOrInsert` when the default value is already on hand and cheap to build:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const counters = new DuoMap<string, string, number>();
|
|
99
|
+
|
|
100
|
+
counters.getOrInsert('page', '/home', 0); // 0 — inserted
|
|
101
|
+
counters.getOrInsert('page', '/home', 99); // 0 — already there, 99 ignored
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
> Note: the default passed to `getOrInsert` is a plain argument, so it is evaluated on **every** call even when it is not used. If building it is expensive, reach for `getOrInsertComputed`, which defers the factory until the pair is actually missing.
|
|
105
|
+
|
|
106
|
+
### Inspecting and iterating
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
const map = new DuoMap<string, string, number>();
|
|
110
|
+
map.set('a', 'x', 1)
|
|
111
|
+
.set('a', 'y', 2)
|
|
112
|
+
.set('b', 'x', 3);
|
|
113
|
+
|
|
114
|
+
map.size; // 3 — counts pairs, not key1 groups
|
|
115
|
+
map.keys(); // [['a', 'x'], ['a', 'y'], ['b', 'x']]
|
|
116
|
+
map.values(); // [1, 2, 3]
|
|
117
|
+
map.entries(); // [['a', 'x', 1], ['a', 'y', 2], ['b', 'x', 3]]
|
|
118
|
+
|
|
119
|
+
map.forEach((value, key1, key2) => {
|
|
120
|
+
console.log(key1, key2, value);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
for (const [key1, key2, value] of map) {
|
|
124
|
+
console.log(key1, key2, value);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
map.delete('a', 'x'); // true — 'a' still holds 'y', so the group survives
|
|
128
|
+
map.delete('a', 'y'); // true — last pair of 'a' gone, the group is dropped too
|
|
129
|
+
map.clear();
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## API
|
|
133
|
+
|
|
134
|
+
### `new DuoMap<K1, K2, V>()`
|
|
135
|
+
|
|
136
|
+
Creates an empty map. All three type parameters default to `any`.
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
const loosely = new DuoMap(); // DuoMap<any, any, any>
|
|
140
|
+
const typed = new DuoMap<string, number, User>();
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### `.set(key1: K1, key2: K2, value: V): this`
|
|
144
|
+
|
|
145
|
+
Stores `value` under the pair, overwriting any previous value. The `key1` group is created on demand. Returns `this` for chaining.
|
|
146
|
+
|
|
147
|
+
### `.get(key1: K1, key2: K2): V | undefined`
|
|
148
|
+
|
|
149
|
+
Returns the value stored under the pair, or `undefined` if the pair is absent. A stored `undefined` is indistinguishable from an absent pair here — use `has` when that matters.
|
|
150
|
+
|
|
151
|
+
### `.getOrInsert(key1: K1, key2: K2, value: V): V`
|
|
152
|
+
|
|
153
|
+
Returns the existing value if the pair is present, otherwise stores `value` and returns it. Presence is tested with `has`, so an explicitly stored `undefined` is preserved rather than replaced.
|
|
154
|
+
|
|
155
|
+
### `.getOrInsertComputed(key1: K1, key2: K2, compute: () => V): V`
|
|
156
|
+
|
|
157
|
+
Like `getOrInsert`, but `compute` is a factory invoked only when the pair is absent. If `compute` throws, the map is left untouched. Mirrors the [upsert proposal](https://github.com/tc39/proposal-upsert) (`Map.prototype.getOrInsertComputed`, Stage 4).
|
|
158
|
+
|
|
159
|
+
### `.has(key1: K1, key2: K2): boolean`
|
|
160
|
+
|
|
161
|
+
Returns `true` if the pair exists, including when its value is `undefined`. Does not create the group as a side effect.
|
|
162
|
+
|
|
163
|
+
### `.delete(key1: K1, key2: K2): boolean`
|
|
164
|
+
|
|
165
|
+
Removes the pair and reports whether it existed. When that was the group's last pair, the `key1` group is removed as well.
|
|
166
|
+
|
|
167
|
+
### `.clear(): void`
|
|
168
|
+
|
|
169
|
+
Removes every entry.
|
|
170
|
+
|
|
171
|
+
### `.size: number`
|
|
172
|
+
|
|
173
|
+
The total number of **pairs** across all `key1` groups, computed on each read by summing the group sizes. O(number of `key1` groups), unlike native `Map.size` which is O(1).
|
|
174
|
+
|
|
175
|
+
### `.forEach(callback: (value: V, key1: K1, key2: K2) => void): void`
|
|
176
|
+
|
|
177
|
+
Invokes `callback` for each pair. Note the argument order places the value first, and there is no index or collection argument.
|
|
178
|
+
|
|
179
|
+
### `.keys(): [K1, K2][]`
|
|
180
|
+
|
|
181
|
+
Returns an array of `[key1, key2]` pairs.
|
|
182
|
+
|
|
183
|
+
### `.values(): V[]`
|
|
184
|
+
|
|
185
|
+
Returns an array of all values.
|
|
186
|
+
|
|
187
|
+
### `.entries(): [K1, K2, V][]`
|
|
188
|
+
|
|
189
|
+
Returns an array of `[key1, key2, value]` triples.
|
|
190
|
+
|
|
191
|
+
### `[Symbol.iterator](): Iterator<[K1, K2, V]>`
|
|
192
|
+
|
|
193
|
+
Yields `[key1, key2, value]` triples, enabling `for...of` and spread.
|
|
194
|
+
|
|
195
|
+
> `keys()`, `values()`, `entries()` and the iterator all return **snapshots** built from the current state, not live views. Adding entries while iterating will not visit them, and mutating a returned array does not affect the map.
|
|
196
|
+
|
|
197
|
+
> Ordering follows native `Map`: entries come back grouped by `key1` in insertion order, and within a group by `key2` in insertion order.
|
|
198
|
+
|
|
199
|
+
### Key equality
|
|
200
|
+
|
|
201
|
+
Keys are compared with **SameValueZero**, exactly like native `Map` — `NaN` matches `NaN`, `+0` and `-0` are the same key, and object keys match by reference.
|
|
202
|
+
|
|
203
|
+
## Differences from native `Map`
|
|
204
|
+
|
|
205
|
+
| Feature | `Map` | `DuoMap` |
|
|
206
|
+
| --------------------- | --------------------- | -------------------------------------------- |
|
|
207
|
+
| Keys per entry | 1 | 2 |
|
|
208
|
+
| Read / write | `(key)` | `(key1, key2)` |
|
|
209
|
+
| Iteration yields | `[key, value]` | `[key1, key2, value]` |
|
|
210
|
+
| `forEach` callback | `(value, key, map)` | `(value, key1, key2)` |
|
|
211
|
+
| `keys()` / `values()` | iterators | plain arrays |
|
|
212
|
+
| `getOrInsert` | proposal only | shipped, with a lazy `getOrInsertComputed` |
|
|
213
|
+
| Empty group cleanup | — | `key1` group dropped with its last `delete` |
|
|
214
|
+
| `.size` | O(1) | O(number of `key1` groups) |
|
|
215
|
+
|
|
216
|
+
## License
|
|
217
|
+
|
|
218
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`}),Array.isArray,Array.from,Array.prototype.concat,Array.prototype.filter,Array.prototype.push,Array.prototype.slice,Array.prototype.splice,Function.prototype.toString,Number.isNaN,Number.isInteger,Number.isSafeInteger,Number.isFinite;const e=(e,t,n)=>(e.has(t)||e.set(t,n),e.get(t)),t=(e,t,n)=>(e.has(t)||e.set(t,n(t)),e.get(t)),n=()=>/* @__PURE__ */ new Map;var r=class{map=/* @__PURE__ */ new Map;get size(){let e=0;return this.map.forEach(t=>e+=t.size),e}set(e,r,i){return t(this.map,e,n).set(r,i),this}get(e,t){return this.map.get(e)?.get(t)}getOrInsert(r,i,a){let o=t(this.map,r,n);return e(o,i,a)}getOrInsertComputed(e,r,i){let a=t(this.map,e,n);return t(a,r,()=>i(e,r))}has(e,t){return this.map.get(e)?.has(t)??!1}delete(e,t){let n=this.map.get(e);if(!n)return!1;let r=n.delete(t);return n.size===0&&this.map.delete(e),r}clear(){this.map.clear()}forEach(e){this.map.forEach((t,n)=>t.forEach((t,r)=>e(t,n,r)))}keys(){let e=[];return this.map.forEach((t,n)=>t.forEach((t,r)=>e.push([n,r]))),e}values(){let e=[];return this.map.forEach(t=>t.forEach(t=>e.push(t))),e}entries(){let e=[];return this.map.forEach((t,n)=>t.forEach((t,r)=>e.push([n,r,t]))),e}[Symbol.iterator](){let e=this.entries(),t=0;return{next(){return t<e.length?{value:e[t++],done:!1}:{value:void 0,done:!0}}}}};exports.DuoMap=r;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["_getOrInsert","map","key","value","_getOrInsertComputed","compute","MapConstruct","DuoMap","count","m","key1","key2","result","callback","entries","index"],"sources":["../../_shared/native/std.ts","../src/index.ts"],"sourcesContent":["// Cached native methods for performance optimization\n\n// # Array\nexport const $isArray = Array.isArray;\nexport const $arrayFrom = Array.from;\nexport const $concat = Array.prototype.concat;\nexport const $filter = Array.prototype.filter;\nexport const $push = Array.prototype.push;\nexport const $slice = Array.prototype.slice;\nexport const $splice = Array.prototype.splice;\n\n// # Object\nexport const $fnToString = Function.prototype.toString;\nexport const $toString = Object.prototype.toString;\nexport const $ObjectPrototype = Object.prototype;\nexport const $is = Object.is;\nexport const $assign = Object.assign;\nexport const $hasOwn = Object.prototype.hasOwnProperty;\nexport const $keys = Object.keys as <T>(o: T) => Array<keyof T>;\nexport const $define = Object.defineProperty;\nexport const $defines = Object.defineProperties;\nexport const $entries = Object.entries as <T>(o: T) => Array<[keyof T, T[keyof T]]>;\nexport const $jsonStringify = JSON.stringify;\nexport const $getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors;\nexport const $freeze = Object.freeze;\n\n// # Reflect\n\nexport const $set = Reflect.set;\nexport const $get = Reflect.get;\nexport const $has = Reflect.has;\nexport const $ownKeys = Reflect.ownKeys;\n// & Do not use `Object.defineProperty`\nexport const $reflectDefine = Reflect.defineProperty;\nexport const $delete = Reflect.deleteProperty;\nexport const $getPrototypeOf = Reflect.getPrototypeOf;\nexport const $setPrototypeOf = Reflect.setPrototypeOf;\n\n// # Math and related utilities\nexport const $max = Math.max;\nexport const $min = Math.min;\nexport const $floor = Math.floor;\nexport const $random = Math.random;\nexport const $randomInt = (min: number, max: number) => $floor($random() * (max - min + 1)) + min;\n\n// # Number\nexport const $isNaN = Number.isNaN;\nexport const $isInt = Number.isInteger;\nexport const $isSafeInt = Number.isSafeInteger;\nexport const $isFinite = Number.isFinite;\nexport const $MAX_INT = Number.MAX_SAFE_INTEGER;\nexport const $MIN_INT = Number.MIN_SAFE_INTEGER;\n\nexport const $isThenable = (o: any): o is Promise<any> => typeof o?.then === 'function';\n\nexport const $isPrimitive: (o: unknown) => boolean | null | undefined | symbol | number | bigint = (o) =>\n o === null || (typeof o !== 'object' && typeof o !== 'function');\n\n// # Map\nexport const _getOrInsert = <K, V>(map: Map<K, V>, key: K, value: V): V => {\n if (!map.has(key)) {\n map.set(key, value);\n }\n return map.get(key)!;\n};\n\nexport const _getOrInsertComputed = <K, V>(map: Map<K, V>, key: K, compute: (key: K) => V): V => {\n if (!map.has(key)) {\n map.set(key, compute(key));\n }\n return map.get(key)!;\n};\n\n// # WeakMap\nexport const _weakGetOrInsert = <K extends WeakKey, V>(map: WeakMap<K, V>, key: K, value: V): V => {\n if (!map.has(key)) {\n map.set(key, value);\n }\n return map.get(key)!;\n};\n\nexport const _weakGetOrInsertComputed = <K extends WeakKey, V>(\n map: WeakMap<K, V>,\n key: K,\n compute: (key: K) => V,\n): V => {\n if (!map.has(key)) {\n map.set(key, compute(key));\n }\n return map.get(key)!;\n};\n","import { _getOrInsertComputed, _getOrInsert } from '@shared';\n\nconst MapConstruct = () => new Map();\n\nexport class DuoMap<K1 = any, K2 = any, V = any> {\n private map = new Map<K1, Map<K2, V>>();\n\n get size(): number {\n let count = 0;\n this.map.forEach((m) => (count += m.size));\n return count;\n }\n\n set(key1: K1, key2: K2, value: V): this {\n _getOrInsertComputed(this.map, key1, MapConstruct).set(key2, value);\n return this;\n }\n\n get(key1: K1, key2: K2): V | undefined {\n return this.map.get(key1)?.get(key2);\n }\n\n getOrInsert(key1: K1, key2: K2, value: V): V {\n const m = _getOrInsertComputed(this.map, key1, MapConstruct);\n return _getOrInsert(m, key2, value);\n }\n\n getOrInsertComputed(key1: K1, key2: K2, compute: (key1: K1, key2: K2) => V): V {\n const m = _getOrInsertComputed(this.map, key1, MapConstruct);\n return _getOrInsertComputed(m, key2, () => compute(key1, key2));\n }\n\n has(key1: K1, key2: K2): boolean {\n return this.map.get(key1)?.has(key2) ?? false;\n }\n\n delete(key1: K1, key2: K2): boolean {\n const m = this.map.get(key1);\n if (!m) {\n return false;\n }\n const result = m.delete(key2);\n if (m.size === 0) {\n this.map.delete(key1);\n }\n return result;\n }\n\n clear(): void {\n this.map.clear();\n }\n\n forEach(callback: (value: V, key1: K1, key2: K2) => void): void {\n this.map.forEach((m, key1) => m.forEach((value, key2) => callback(value, key1, key2)));\n }\n\n keys(): [K1, K2][] {\n const result: [K1, K2][] = [];\n this.map.forEach((m, key1) => m.forEach((_, key2) => result.push([key1, key2])));\n return result;\n }\n\n values(): V[] {\n const result: V[] = [];\n this.map.forEach((m) => m.forEach((value) => result.push(value)));\n return result;\n }\n\n entries(): [K1, K2, V][] {\n const result: [K1, K2, V][] = [];\n this.map.forEach((m, key1) => m.forEach((value, key2) => result.push([key1, key2, value])));\n return result;\n }\n\n [Symbol.iterator](): Iterator<[K1, K2, V]> {\n const entries = this.entries();\n let index = 0;\n return {\n next(): IteratorResult<[K1, K2, V]> {\n if (index < entries.length) {\n return { value: entries[index++], done: false };\n } else {\n return { value: undefined as any, done: true };\n }\n },\n };\n }\n}\n"],"mappings":"mEAGwB,MAAM,QACJ,MAAM,KACT,MAAM,UAAU,OAChB,MAAM,UAAU,OAClB,MAAM,UAAU,KACf,MAAM,UAAU,MACf,MAAM,UAAU,OAGZ,SAAS,UAAU,SAkCxB,OAAO,MACP,OAAO,UACH,OAAO,cACR,OAAO,SAUhC,MAAaA,GAAsB,EAAgB,EAAQ,KACpDC,EAAI,IAAIC,CAAG,GACdD,EAAI,IAAIC,EAAKC,CAAK,EAEbF,EAAI,IAAIC,CAAG,GAGPE,GAA8B,EAAgB,EAAQ,KAC5DH,EAAI,IAAIC,CAAG,GACdD,EAAI,IAAIC,EAAKG,EAAQH,CAAG,CAAC,EAEpBD,EAAI,IAAIC,CAAG,GCpEdI,sBAAqB,IAAI,IAE/B,IAAaC,EAAb,KAAiD,CAC/C,oBAA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CAAI,CAAC,CAAC,CAAC,EACxEC,CACT,CAEA,QAAc,CACZ,IAAMA,EAAc,CAAC,EAErB,OADA,KAAK,IAAI,QAAS,GAAMH,EAAE,QAAS,GAAUG,EAAO,KAAKT,CAAK,CAAC,CAAC,EACzDS,CACT,CAEA,SAAyB,CACvB,IAAMA,EAAwB,CAAC,EAE/B,OADA,KAAK,IAAI,SAAS,EAAG,IAASH,EAAE,SAAS,EAAO,IAASG,EAAO,KAAK,CAACF,EAAMC,EAAMR,CAAK,CAAC,CAAC,CAAC,EACnFS,CACT,CAEA,CAAC,OAAO,WAAmC,CACzC,IAAME,EAAU,KAAK,QAAQ,EACzBC,EAAQ,EACZ,MAAO,CACL,MAAoC,CAIhC,OAHEA,EAAQD,EAAQ,OACX,CAAE,MAAOA,EAAQ,KAAU,KAAM,EAAM,EAEvC,CAAE,MAAO,IAAA,GAAkB,KAAM,EAAK,CAEjD,CACF,CACF,CACF"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
declare class DuoMap<K1 = any, K2 = any, V = any> {
|
|
3
|
+
private map;
|
|
4
|
+
get size(): number;
|
|
5
|
+
set(key1: K1, key2: K2, value: V): this;
|
|
6
|
+
get(key1: K1, key2: K2): V | undefined;
|
|
7
|
+
getOrInsert(key1: K1, key2: K2, value: V): V;
|
|
8
|
+
getOrInsertComputed(key1: K1, key2: K2, compute: (key1: K1, key2: K2) => V): V;
|
|
9
|
+
has(key1: K1, key2: K2): boolean;
|
|
10
|
+
delete(key1: K1, key2: K2): boolean;
|
|
11
|
+
clear(): void;
|
|
12
|
+
forEach(callback: (value: V, key1: K1, key2: K2) => void): void;
|
|
13
|
+
keys(): [K1, K2][];
|
|
14
|
+
values(): V[];
|
|
15
|
+
entries(): [K1, K2, V][];
|
|
16
|
+
[Symbol.iterator](): Iterator<[K1, K2, V]>;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { DuoMap };
|
|
20
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
declare class DuoMap<K1 = any, K2 = any, V = any> {
|
|
3
|
+
private map;
|
|
4
|
+
get size(): number;
|
|
5
|
+
set(key1: K1, key2: K2, value: V): this;
|
|
6
|
+
get(key1: K1, key2: K2): V | undefined;
|
|
7
|
+
getOrInsert(key1: K1, key2: K2, value: V): V;
|
|
8
|
+
getOrInsertComputed(key1: K1, key2: K2, compute: (key1: K1, key2: K2) => V): V;
|
|
9
|
+
has(key1: K1, key2: K2): boolean;
|
|
10
|
+
delete(key1: K1, key2: K2): boolean;
|
|
11
|
+
clear(): void;
|
|
12
|
+
forEach(callback: (value: V, key1: K1, key2: K2) => void): void;
|
|
13
|
+
keys(): [K1, K2][];
|
|
14
|
+
values(): V[];
|
|
15
|
+
entries(): [K1, K2, V][];
|
|
16
|
+
[Symbol.iterator](): Iterator<[K1, K2, V]>;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { DuoMap };
|
|
20
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
Array.isArray,Array.from,Array.prototype.concat,Array.prototype.filter,Array.prototype.push,Array.prototype.slice,Array.prototype.splice,Function.prototype.toString,Number.isNaN,Number.isInteger,Number.isSafeInteger,Number.isFinite;const e=(e,t,n)=>(e.has(t)||e.set(t,n),e.get(t)),t=(e,t,n)=>(e.has(t)||e.set(t,n(t)),e.get(t)),n=()=>/* @__PURE__ */ new Map;var r=class{map=/* @__PURE__ */ new Map;get size(){let e=0;return this.map.forEach(t=>e+=t.size),e}set(e,r,i){return t(this.map,e,n).set(r,i),this}get(e,t){return this.map.get(e)?.get(t)}getOrInsert(r,i,a){let o=t(this.map,r,n);return e(o,i,a)}getOrInsertComputed(e,r,i){let a=t(this.map,e,n);return t(a,r,()=>i(e,r))}has(e,t){return this.map.get(e)?.has(t)??!1}delete(e,t){let n=this.map.get(e);if(!n)return!1;let r=n.delete(t);return n.size===0&&this.map.delete(e),r}clear(){this.map.clear()}forEach(e){this.map.forEach((t,n)=>t.forEach((t,r)=>e(t,n,r)))}keys(){let e=[];return this.map.forEach((t,n)=>t.forEach((t,r)=>e.push([n,r]))),e}values(){let e=[];return this.map.forEach(t=>t.forEach(t=>e.push(t))),e}entries(){let e=[];return this.map.forEach((t,n)=>t.forEach((t,r)=>e.push([n,r,t]))),e}[Symbol.iterator](){let e=this.entries(),t=0;return{next(){return t<e.length?{value:e[t++],done:!1}:{value:void 0,done:!0}}}}};export{r as DuoMap};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["_getOrInsert","map","key","value","_getOrInsertComputed","compute","MapConstruct","DuoMap","count","m","key1","key2","result","callback","entries","index"],"sources":["../../_shared/native/std.ts","../src/index.ts"],"sourcesContent":["// Cached native methods for performance optimization\n\n// # Array\nexport const $isArray = Array.isArray;\nexport const $arrayFrom = Array.from;\nexport const $concat = Array.prototype.concat;\nexport const $filter = Array.prototype.filter;\nexport const $push = Array.prototype.push;\nexport const $slice = Array.prototype.slice;\nexport const $splice = Array.prototype.splice;\n\n// # Object\nexport const $fnToString = Function.prototype.toString;\nexport const $toString = Object.prototype.toString;\nexport const $ObjectPrototype = Object.prototype;\nexport const $is = Object.is;\nexport const $assign = Object.assign;\nexport const $hasOwn = Object.prototype.hasOwnProperty;\nexport const $keys = Object.keys as <T>(o: T) => Array<keyof T>;\nexport const $define = Object.defineProperty;\nexport const $defines = Object.defineProperties;\nexport const $entries = Object.entries as <T>(o: T) => Array<[keyof T, T[keyof T]]>;\nexport const $jsonStringify = JSON.stringify;\nexport const $getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors;\nexport const $freeze = Object.freeze;\n\n// # Reflect\n\nexport const $set = Reflect.set;\nexport const $get = Reflect.get;\nexport const $has = Reflect.has;\nexport const $ownKeys = Reflect.ownKeys;\n// & Do not use `Object.defineProperty`\nexport const $reflectDefine = Reflect.defineProperty;\nexport const $delete = Reflect.deleteProperty;\nexport const $getPrototypeOf = Reflect.getPrototypeOf;\nexport const $setPrototypeOf = Reflect.setPrototypeOf;\n\n// # Math and related utilities\nexport const $max = Math.max;\nexport const $min = Math.min;\nexport const $floor = Math.floor;\nexport const $random = Math.random;\nexport const $randomInt = (min: number, max: number) => $floor($random() * (max - min + 1)) + min;\n\n// # Number\nexport const $isNaN = Number.isNaN;\nexport const $isInt = Number.isInteger;\nexport const $isSafeInt = Number.isSafeInteger;\nexport const $isFinite = Number.isFinite;\nexport const $MAX_INT = Number.MAX_SAFE_INTEGER;\nexport const $MIN_INT = Number.MIN_SAFE_INTEGER;\n\nexport const $isThenable = (o: any): o is Promise<any> => typeof o?.then === 'function';\n\nexport const $isPrimitive: (o: unknown) => boolean | null | undefined | symbol | number | bigint = (o) =>\n o === null || (typeof o !== 'object' && typeof o !== 'function');\n\n// # Map\nexport const _getOrInsert = <K, V>(map: Map<K, V>, key: K, value: V): V => {\n if (!map.has(key)) {\n map.set(key, value);\n }\n return map.get(key)!;\n};\n\nexport const _getOrInsertComputed = <K, V>(map: Map<K, V>, key: K, compute: (key: K) => V): V => {\n if (!map.has(key)) {\n map.set(key, compute(key));\n }\n return map.get(key)!;\n};\n\n// # WeakMap\nexport const _weakGetOrInsert = <K extends WeakKey, V>(map: WeakMap<K, V>, key: K, value: V): V => {\n if (!map.has(key)) {\n map.set(key, value);\n }\n return map.get(key)!;\n};\n\nexport const _weakGetOrInsertComputed = <K extends WeakKey, V>(\n map: WeakMap<K, V>,\n key: K,\n compute: (key: K) => V,\n): V => {\n if (!map.has(key)) {\n map.set(key, compute(key));\n }\n return map.get(key)!;\n};\n","import { _getOrInsertComputed, _getOrInsert } from '@shared';\n\nconst MapConstruct = () => new Map();\n\nexport class DuoMap<K1 = any, K2 = any, V = any> {\n private map = new Map<K1, Map<K2, V>>();\n\n get size(): number {\n let count = 0;\n this.map.forEach((m) => (count += m.size));\n return count;\n }\n\n set(key1: K1, key2: K2, value: V): this {\n _getOrInsertComputed(this.map, key1, MapConstruct).set(key2, value);\n return this;\n }\n\n get(key1: K1, key2: K2): V | undefined {\n return this.map.get(key1)?.get(key2);\n }\n\n getOrInsert(key1: K1, key2: K2, value: V): V {\n const m = _getOrInsertComputed(this.map, key1, MapConstruct);\n return _getOrInsert(m, key2, value);\n }\n\n getOrInsertComputed(key1: K1, key2: K2, compute: (key1: K1, key2: K2) => V): V {\n const m = _getOrInsertComputed(this.map, key1, MapConstruct);\n return _getOrInsertComputed(m, key2, () => compute(key1, key2));\n }\n\n has(key1: K1, key2: K2): boolean {\n return this.map.get(key1)?.has(key2) ?? false;\n }\n\n delete(key1: K1, key2: K2): boolean {\n const m = this.map.get(key1);\n if (!m) {\n return false;\n }\n const result = m.delete(key2);\n if (m.size === 0) {\n this.map.delete(key1);\n }\n return result;\n }\n\n clear(): void {\n this.map.clear();\n }\n\n forEach(callback: (value: V, key1: K1, key2: K2) => void): void {\n this.map.forEach((m, key1) => m.forEach((value, key2) => callback(value, key1, key2)));\n }\n\n keys(): [K1, K2][] {\n const result: [K1, K2][] = [];\n this.map.forEach((m, key1) => m.forEach((_, key2) => result.push([key1, key2])));\n return result;\n }\n\n values(): V[] {\n const result: V[] = [];\n this.map.forEach((m) => m.forEach((value) => result.push(value)));\n return result;\n }\n\n entries(): [K1, K2, V][] {\n const result: [K1, K2, V][] = [];\n this.map.forEach((m, key1) => m.forEach((value, key2) => result.push([key1, key2, value])));\n return result;\n }\n\n [Symbol.iterator](): Iterator<[K1, K2, V]> {\n const entries = this.entries();\n let index = 0;\n return {\n next(): IteratorResult<[K1, K2, V]> {\n if (index < entries.length) {\n return { value: entries[index++], done: false };\n } else {\n return { value: undefined as any, done: true };\n }\n },\n };\n }\n}\n"],"mappings":"AAGwB,MAAM,QACJ,MA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|
+
"name": "KasukabeTsumugi",
|
|
6
|
+
"email": "futami16237@gmail.com"
|
|
7
|
+
},
|
|
8
|
+
"description": "A duo-key map, mapping two keys to a single value.",
|
|
9
|
+
"description_zh": "双键映射,由两个key映射到一个值",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
},
|
|
21
|
+
"default": "./dist/index.mjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "clear && vitest"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/baendlorel/tsumugi-lib.git",
|
|
33
|
+
"directory": "packages/duo-map"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"cache",
|
|
37
|
+
"caching",
|
|
38
|
+
"typescript",
|
|
39
|
+
"utility",
|
|
40
|
+
"javascript",
|
|
41
|
+
"npm"
|
|
42
|
+
],
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/baendlorel/tsumugi-lib/issues"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/baendlorel/tsumugi-lib/tree/main/packages/duo-map#readme",
|
|
48
|
+
"devDependencies": {}
|
|
6
49
|
}
|