Class: Enum

Enum(enums, auto)

Base enum class

Constructor

new Enum(enums, auto)

Parameters:
Name Type Default Description
enums Object.<String, *> | Array.<String>

Data to build the enum from

auto boolean false

Auto generate enum from data making assumptions about the data, requires enums to be of type array.

Source:
Example
const Colors = new Enum(['RED', 'BLACK', 'GREEN', 'WHITE', 'BLUE']);

const Answers = new Enum({
  YES: true,
  NO: false,
  // Passing functions as values will turn them into getters
  // Getter results will appear in ::values
  MAYBE: () => Math.random() >= 0.5,
});

const FontStyles = new Enum(['italic', 'bold', 'underline', 'regular'], true);
FontStyles.ITALIC === 'italic'
FontStyles.BOLD   === 'bold'

// etc...

Methods

findForValue(value) → {string}

Find key name for value

Parameters:
Name Type Description
value string | number | *

Enum value

Source:
Returns:
  • key name
Type
string

hasKey(name) → {boolean}

Find if a key exists

Parameters:
Name Type Description
name string | number | *

Enum value name

Source:
Returns:
  • key exists
Type
boolean

hasValue(value) → {boolean}

Find if a key exists

Parameters:
Name Type Description
value string | number | *

Enum value

Source:
Returns:
  • value exists
Type
boolean

keys() → {Array}

List enum keys

Source:
Returns:
  • Enum keys
Type
Array

toString() → {string}

Get a string representation of the enum

Source:
Returns:
  • String representation of the enum
Type
string

values() → {Array.<*>}

List enum values

Source:
Returns:
  • Enum values
Type
Array.<*>