@streetsidesoftware/auto-resolve-map 0.0.0-stage → 0.0.1-alpha.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/LICENSE +21 -0
- package/README.md +154 -2
- package/dist/index.d.ts +629 -0
- package/dist/index.js +936 -0
- package/package.json +41 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Street Side Software
|
|
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,155 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @streetsidesoftware/auto-resolve-map
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Maps that compute and store a missing value, including maps that don't keep their values alive, and a function that
|
|
4
|
+
does the same for a map you already have.
|
|
5
|
+
|
|
6
|
+
Node.js 22 and 24 don't have `getOrInsert` and `getOrInsertComputed` on `Map` and `WeakMap`. The classes in this package
|
|
7
|
+
have them on every supported version.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @streetsidesoftware/auto-resolve-map
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### A map you already have
|
|
18
|
+
|
|
19
|
+
Use `autoResolve` with any object that has `get`, `has` and `set`, such as a `Map` or a `WeakMap`:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { autoResolve } from '@streetsidesoftware/auto-resolve-map';
|
|
23
|
+
|
|
24
|
+
const lengths = new Map<string, number>();
|
|
25
|
+
|
|
26
|
+
autoResolve(lengths, 'apple', (key) => key.length); // 5, computed and stored
|
|
27
|
+
autoResolve(lengths, 'apple', (key) => key.length); // 5, from the map
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
A stored `undefined` counts as a value. Nothing is stored when the callback throws.
|
|
31
|
+
|
|
32
|
+
### A new map
|
|
33
|
+
|
|
34
|
+
Pick a class by what it should keep alive:
|
|
35
|
+
|
|
36
|
+
| Class | Keeps its keys alive | Keeps its values alive | Use it as |
|
|
37
|
+
| -------------------- | -------------------- | ---------------------------- | --------- |
|
|
38
|
+
| `AutoResolveMap` | Yes | Yes | `Map` |
|
|
39
|
+
| `AutoResolveWeakMap` | No | As long as the key is in use | `WeakMap` |
|
|
40
|
+
| `StrongWeakMap` | Yes | No | `Map` |
|
|
41
|
+
| `WeakWeakMap` | No | No | `WeakMap` |
|
|
42
|
+
|
|
43
|
+
Each has all the methods of the built-in it's used as, `getOrInsert` and `getOrInsertComputed` included. The two with
|
|
44
|
+
weak keys also have `clear`.
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { AutoResolveMap } from '@streetsidesoftware/auto-resolve-map';
|
|
48
|
+
|
|
49
|
+
const words = new AutoResolveMap<string, string[]>();
|
|
50
|
+
|
|
51
|
+
words.getOrInsert('a', []).push('apple');
|
|
52
|
+
words.getOrInsert('a', []).push('avocado');
|
|
53
|
+
words.getOrInsertComputed('b', loadWords);
|
|
54
|
+
|
|
55
|
+
words.get('a'); // ['apple', 'avocado']
|
|
56
|
+
words.get('b'); // The result of `loadWords('b')`
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Values that can be released
|
|
60
|
+
|
|
61
|
+
Use `StrongWeakMap` or `WeakWeakMap` to share a value for as long as something uses it, and to let it go after
|
|
62
|
+
that:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { StrongWeakMap } from '@streetsidesoftware/auto-resolve-map';
|
|
66
|
+
|
|
67
|
+
const dictionaries = new StrongWeakMap<string, Dictionary>();
|
|
68
|
+
|
|
69
|
+
const a = dictionaries.getOrInsertComputed('colors', loadDictionary);
|
|
70
|
+
const b = dictionaries.getOrInsertComputed('colors', loadDictionary); // The same dictionary as `a`.
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Once nothing else uses a value, it can be garbage collected at any time:
|
|
74
|
+
|
|
75
|
+
- After that, `get` and `has` act as if its key had never been there.
|
|
76
|
+
- Looping over the entries or values of a `StrongWeakMap` skips it.
|
|
77
|
+
- `StrongWeakMap` removes the key itself some time later. Until then, `size` and `keys()` include it.
|
|
78
|
+
- Any look at a value, `has` included, keeps it alive until the current turn of the event loop ends.
|
|
79
|
+
|
|
80
|
+
Each entry of a `StrongWeakMap` has a fixed cost on top of a normal map's, whatever the size of its value. It suits a
|
|
81
|
+
small number of large objects, such as dictionaries, and not many small ones.
|
|
82
|
+
|
|
83
|
+
### Caches with stats
|
|
84
|
+
|
|
85
|
+
Each class has a version that counts how it's used: `AutoResolveCache`, `AutoResolveWeakCache`, `StrongWeakCache` and
|
|
86
|
+
`WeakWeakCache`.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { AutoResolveCache } from '@streetsidesoftware/auto-resolve-map';
|
|
90
|
+
|
|
91
|
+
const cache = new AutoResolveCache<string, number>();
|
|
92
|
+
|
|
93
|
+
cache.getOrInsertComputed('apple', (key) => key.length);
|
|
94
|
+
cache.getOrInsertComputed('apple', (key) => key.length);
|
|
95
|
+
|
|
96
|
+
cache.stats(); // { hits: 1, misses: 1, resolved: 1, sets: 0, deletes: 0, clears: 0 }
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
| Count | Goes up when |
|
|
100
|
+
| ---------- | ----------------------------------------------- |
|
|
101
|
+
| `hits` | A lookup finds a value |
|
|
102
|
+
| `misses` | A lookup finds none |
|
|
103
|
+
| `resolved` | A value a callback computed is stored |
|
|
104
|
+
| `sets` | A value is stored by `set`, or by `getOrInsert` |
|
|
105
|
+
| `deletes` | `delete` is called |
|
|
106
|
+
| `clears` | `clear` is called |
|
|
107
|
+
|
|
108
|
+
A lookup is `get`, `getOrInsert` or `getOrInsertComputed`. Calling `has` doesn't count, and neither do the entries a cache
|
|
109
|
+
starts with.
|
|
110
|
+
|
|
111
|
+
To start counting again, call `resetStats()`. It leaves the entries alone, as `clear()` leaves the earlier counts alone.
|
|
112
|
+
|
|
113
|
+
## Requirements
|
|
114
|
+
|
|
115
|
+
<!--- @@inject: ../../static/requirements.md#values-file=:package.json --->
|
|
116
|
+
|
|
117
|
+
Requires Node.js >=22.18.0. ESM only.
|
|
118
|
+
|
|
119
|
+
<!--- @@inject-end: ../../static/requirements.md#values-file=:package.json --->
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
<!--- @@inject: ../../static/licenseTpl.md#values-file=:package.json --->
|
|
124
|
+
|
|
125
|
+
MIT
|
|
126
|
+
|
|
127
|
+
<!--- @@inject-end: ../../static/licenseTpl.md#values-file=:package.json --->
|
|
128
|
+
|
|
129
|
+
## Support Future Development
|
|
130
|
+
|
|
131
|
+
<!--- @@inject: ../../static/sponsor.md --->
|
|
132
|
+
|
|
133
|
+
If our tools save you time, please consider supporting their development.
|
|
134
|
+
|
|
135
|
+
Please show your support through one of the following sites:
|
|
136
|
+
|
|
137
|
+
<p align="left">
|
|
138
|
+
<a href="https://github.com/sponsors/streetsidesoftware" title="GitHub Sponsor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://streetsidesoftware.com/img/sponsor/github-sponsor-dark.png" /><img alt="GitHub Sponsor" src="https://streetsidesoftware.com/img/sponsor/github-sponsor.png" width="180" /></picture></a> <a href="https://thanks.dev/u/gh/streetsidesoftware" title="thanks.dev"><picture><source media="(prefers-color-scheme: dark)" srcset="https://streetsidesoftware.com/img/sponsor/thanks-dev-dark.png" /><img alt="thanks.dev" src="https://streetsidesoftware.com/img/sponsor/thanks-dev.png" width="180" /></picture></a> <a href="https://opencollective.com/cspell" title="Open Collective"><picture><source media="(prefers-color-scheme: dark)" srcset="https://streetsidesoftware.com/img/sponsor/open-collective-dark.png" /><img alt="Open Collective" src="https://streetsidesoftware.com/img/sponsor/open-collective.png" width="180" /></picture></a> <a href="https://streetsidesoftware.com/sponsor/" title="Street Side Software"><picture><source media="(prefers-color-scheme: dark)" srcset="https://streetsidesoftware.com/img/sponsor/cspell-dark.png" /><img alt="CSpell" src="https://streetsidesoftware.com/img/sponsor/cspell.png" width="180" /></picture></a>
|
|
139
|
+
</p>
|
|
140
|
+
|
|
141
|
+
<!--- @@inject-end: ../../static/sponsor.md --->
|
|
142
|
+
|
|
143
|
+
<!--- @@inject: ../../static/footer.md --->
|
|
144
|
+
|
|
145
|
+
<br/>
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
<p align="center">
|
|
150
|
+
Brought to you by <a href="https://streetsidesoftware.com" title="Street Side Software">
|
|
151
|
+
<img width="16" alt="Street Side Software Logo" src="https://i.imgur.com/CyduuVY.png" /> Street Side Software
|
|
152
|
+
</a>
|
|
153
|
+
</p>
|
|
154
|
+
|
|
155
|
+
<!--- @@inject-end: ../../static/footer.md --->
|