Enumer8
A Simple Node.js Enumeration Library
Table of Contents
Installation
$ npm i enumer8
Usage
import Enum from 'enumer8'
let Pets = new Enum()
Pets.case('Dog', 'Cat', 'Fish')
console.log(Pets.Dog) // { id:'Dog' , rawValue:'Dog' }
Pets.case('Lizard')
console.log(Pets.Lizard) // { id:'Lizard' , rawValue:'Lizard' }
console.log(Pets.Cat) // { id:'Cat' , rawValue:'Cat' }
Configuration
Each enumeration can be configured on instantiation:
new Enum({ /* ... */ })
Alternatively, the config parameter can be used to initialize an enumeration of a set type (see type
configuration):
new Enum('string')
{option: default}
)
Options ({ type: 'string' }
The type
option can be set to a string type ('string'
, 'number'
, ...) or false
. If raw values are not provided, they will be created according to the specified type.
Example:
let PetNames = new Enum('string')
PetNames.case({
Dog: 'Bolt',
Cat: 'Simba',
Mouse: 'Mickey'
})
PetNames.case({ Snake: true }) // Throws TypeError
API
.case()
- The main method for enumerations.
- The case(s) provided must be strings, an array of strings, or an object
.freeze()
- Freezes the current enumeration, preventing the modification of values.
License
MIT © Andrew Wiggin