https://github.com/lamansky/weakish-map
[Node.js] It’s like WeakMap but it supports non-objects.
https://github.com/lamansky/weakish-map
Last synced: about 2 months ago
JSON representation
[Node.js] It’s like WeakMap but it supports non-objects.
- Host: GitHub
- URL: https://github.com/lamansky/weakish-map
- Owner: lamansky
- License: mit
- Created: 2018-03-27T10:37:41.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-13T22:22:24.000Z (almost 8 years ago)
- Last Synced: 2025-10-08T12:58:13.667Z (9 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# WeakishMap
It’s like WeakMap but it supports non-objects.
## Installation
Requires [Node.js](https://nodejs.org/) 6.0.0 or above.
```bash
npm i weakish-map
```
## API
The module exposes a single class.
### Constructor
The constructor supports the following arguments:
1. Optional: `items` (iterable): Initial key-value pairs for the map.
2. Optional: Object argument:
* `StrongMap` (class): Set this if you have a custom Map class you want to use for storing non-objects. Defaults to the built-in `Map`.
* `strongMap` (function): A callback that creates a new map for storing non-objects. Defaults to a function that creates a new `StrongMap`.
* `WeakMap` (class): Set this if you have a custom WeakMap class you want to use for storing objects. Defaults to the built-in `WeakMap`.
* `weakMap` (function): A callback that creates a new weak map for storing objects. Defaults to a function that creates a new `WeakMap`.
### Methods
Instances of this class have the following methods, which behave just like the corresponding methods on `Map` and `WeakMap`:
* `get()`
* `set()`
* `has()`
* `delete()`
* `clear()`
Instances also have methods which only work on non-objects:
* `entries()`
* `forEach()`
* `keys()`
* `values()`
## Example
```javascript
// Before
const map1 = new WeakMap()
map1.set({}, 'value')
map1.set('key', 'value') // Uncaught TypeError: Invalid value used as weak map key
// After
const WeakishMap = require('weakish-map')
const map2 = new WeakishMap()
map2.set({}, 'value')
map2.set('key', 'value')
map2.get('key') // 'value'
Array.from(map2.keys()) // ['key']
```
## Related
* [weakish-set](https://github.com/lamansky/weakish-set)