symbol-enum

4.0.3 • Public • Published

SymbolEnum

Enum with symbols.

npm Build Status Coverage Status Dependency Status devDependency Status node

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] as keys
  • #[SymbolEnum.values] as values
  • #[SymbolEnum.has] as has
  • #[SymbolEnum.hasValue] as hasValue

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

Readme

Keywords

Package Sidebar

Install

npm i symbol-enum

Weekly Downloads

177

Version

4.0.3

License

MIT

Unpacked Size

7.35 kB

Total Files

4

Last publish

Collaborators

  • seangenabe