Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

primitive-pool

dy14.9kMIT2.0.0

Pool of objects for primitives. (Make WeakMap accept primitive keys)

primitive, primitives, weakmap, pool, keys, object, primitive-object, singleton

readme

primitive-pool unstable Build Status

Get object for a primitive value. Useful to make WeakMap store strings/numbers and other primitives.

Usage

npm install primitive-pool

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.