primitive-pool
v2.0.0
Published
Pool of objects for primitives. (Make WeakMap accept primitive keys)
Downloads
16,949
Readme
primitive-pool
Get object for a primitive value. Useful to make WeakMap store strings/numbers and other primitives.
Usage
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'
Alternative can be done via weak refs.