Namibian Pyjama Merchant
Introducing npm Teams! Private packages + team management tools.Learn more »

symbol-enum

4.0.3-0 • 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

Keywords

Install

npm i symbol-enum

DownloadsWeekly Downloads

628

Version

4.0.3-0

License

MIT

Last publish

Collaborators

  • avatar