https://github.com/lyonbot/nd-weakmap
WeakMap with multiple dimensions, use a "weak" pair as WeakMap
https://github.com/lyonbot/nd-weakmap
Last synced: 5 months ago
JSON representation
WeakMap with multiple dimensions, use a "weak" pair as WeakMap
- Host: GitHub
- URL: https://github.com/lyonbot/nd-weakmap
- Owner: lyonbot
- Created: 2021-12-16T12:14:22.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-12-17T07:32:32.000Z (over 4 years ago)
- Last Synced: 2025-09-22T16:53:03.925Z (9 months ago)
- Language: TypeScript
- Size: 75.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NDWeakMap
WeakMap with multiple dimensions. Use array of objects as WeakMap's key.
```
npm i nd-weakmap
yarn add nd-weakmap
```
## Quick Start
```js
import NDWeakMap from "nd-weakmap";
const map = new NDWeakMap();
// ---------------------
map.set([aaa, bbb], "this works");
map.set([aaa, aaa], "this works too");
map.set([aaa], "second stuff");
map.get([aaa]) === "second stuff";
map.get([aaa, bbb]) === "this works";
map.get([aaa, aaa]) === "this works too";
// ---------------------
map.delete([aaa]); // will NOT affect [aaa, bbb] & [aaa, aaa]
map.has([aaa]) === false;
map.has([aaa, aaa]) === true;
map.has([aaa, bbb]) === true;
// ---------------------
map.deleteCascade([aaa]); // will delete [aaa, *] and [aaa, *, *] ...
map.has([aaa]) === false;
map.has([aaa, aaa]) === false; // deleted
map.has([aaa, bbb]) === false; // deleted
// ---------------------
map.clear();
```
### Strict KeyTuple Length
By default, keyTuple's length may vary.
You can apply constraint to their length.
```js
import NDWeakMap from "nd-weakmap";
// optional constructor parameter
// if is provided, `set()` and `delete()` will strictly check the keyTuple's length
const map = new NDWeakMap(2);
// _
// |
// +--- Strict KeyTuple Length
map.set([aaa, bbb], "this works");
map.set([aaa], "not work"); // this throws Error
map.get([aaa]) === undefined; // this won't throw
```
### TypeScript
You may constraint the keyTuple's type. Meanwhile don't forget to set Strict KeyTuple Length!
```ts
import NDWeakMap from "nd-weakmap";
type KeyTupleType = [any, any];
type ValueType = string;
const map = new NDWeakMap(2);
// _
// |
// (required) Strict KeyTuple Length ---+
map.set([aaa, bbb], "this works");
map.set([aaa], "not work"); // this throws Error
map.get([aaa]) === undefined; // this won't throw
```