An open API service indexing awesome lists of open source software.

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.

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)