https://github.com/dy/primitive-pool
Get unique object for a primitive value to store primitives in WeakMap etc.
https://github.com/dy/primitive-pool
Last synced: about 1 year ago
JSON representation
Get unique object for a primitive value to store primitives in WeakMap etc.
- Host: GitHub
- URL: https://github.com/dy/primitive-pool
- Owner: dy
- Created: 2018-06-23T20:11:30.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-02-17T17:53:59.000Z (over 3 years ago)
- Last Synced: 2025-05-11T13:16:12.268Z (about 1 year ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# primitive-pool [](http://github.com/badges/stability-badges) [](https://travis-ci.org/dy/primitive-pool)
Get object for a primitive value. Useful to make WeakMap store strings/numbers and other primitives.
## Usage
[](https://npmjs.org/package/primitive-pool/)
```js
import p from 'primitive-pool'
let map = new WeakMap()
map.set(p('abc'), 123)
map.get(p('abc')) // 123
map.set(p(null), 789)
map.get(p(null)) // 789
map.set(p(123), 'abc')
map.get(p(123)) // 'abc'
// non-primitives are stored as is
let a = {}
map.set(p(a), 'xyz')
map.get(a) // 'xyz'
```
## Alternatives
* [weak-value](https://github.com/WebReflection/weak-value)
* [not-so-weak](https://github.com/WebReflection/not-so-weak)
* [weakish-map](https://github.com/lamansky/weakish-map)