Skip to content

dotproto/sym

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

For some reason I recently found myself wanting to use something like Symbol.for(), but within a well-defined namespace or symbol registry. Probably to load multiple versions of a library on a single page.

Anyway, this lib extends Symbol to add support for registries. To create a new registry, call var myRegistry = Sym.registry('REGISTRY-NAME').

Examples

Working with a registry

// You can assign your registry to a var to ``use`` Symbols
const registry = Sym.registry('collection');
const s1 = registry.for('name');

// Or using a fluent API
const s2 = Sym.registry('collection').for('name');

s1 === s2 // true

NOTE: Sym registries are not cross-realm. Practically speaking, this means you can't share a registry across windows or frames in a browser.

Sym as a Symbol replacement

const s1 = Sym.for('foobar')
const s2 = Symbol.for('foobar')

s1 === s2 // true

const s3 = Sym('test')
typeof s3 === 'symbol' // true