All Projects → seangenabe → Symbol Enum

seangenabe / Symbol Enum

Licence: mit
Enum with symbols.

Programming Languages

javascript
184084 projects - #8 most used programming language
enum
40 projects
symbols
36 projects

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].