symbol-enum
v4.0.3
Published
Enum with symbols
Downloads
1,175
Readme
SymbolEnum
Enum with symbols.
Usage
const SymbolEnum = require('symbol-enum')
#constructor(...keys)
Creates a new Enum with the specified keys.
const MyEnum = new SymbolEnum('a', 'b', 'c')
#[
key]
Retrieves the symbol corresponding to the key.
const val = MyEnum.a
val // Symbol(a)
#[
symbol]
Retrieves the key corresponding to the symbol.
MyEnum[val] // "a"
#[SymbolEnum.keys]()
Returns an iterator that can be used to iterate through the keys.
Array.from(MyEnum[SymbolEnum.keys]) // "[ a, b, c ]"
#[SymbolEnum.values]()
Returns an iterator that can be used to iterate through the values.
Array.from(MyEnum[SymbolEnum.values]) // "[ Symbol(a), Symbol(b), Symbol(c) ]"
#[SymbolEnum.has](key)
Returns whether the enum contains the specified key.
MyEnum[SymbolEnum.has]('b') // true
#[SymbolEnum.hasValue](value)
Returns whether the enum contains the specified value.
MyEnum[Symbol.hasValue](MyEnum.c) // true
#[SymbolEnum.size]
Returns the number of keys passed to the constructor.
'Underscore'd properties
For your convenience, the following properties are also available directly on the object itself, but only if you don't specify keys
and values
as a member of the enum itself.
#[SymbolEnum.keys]
askeys
#[SymbolEnum.values]
asvalues
#[SymbolEnum.has]
ashas
#[SymbolEnum.hasValue]
ashasValue
If you do, underscores will be prepended to the property name until it becomes available. For example, if you added both keys
and _keys
in the Enum, #[SymbolEnum.keys]
will also be available at #__keys
.
Extends from null
SymbolEnum
extends from null and does not have any string prototype methods so we have a clean slate. This means we don't inherit from Object
and have any of its properties.
Exception: #constructor
Since we're using classes here, that means constructor
will still be defined by default. No worries, if you specify constructor
as a key, it will be overridden.
License
MIT