create-enum
v0.2.0
Published
Creates a type safe enum from a list of strings
Downloads
10
Readme
create-enum
Creates a type-safe enum from a list of strings
It's mainly wrapper around enumify, but provides an additional API.
Install
$ npm install create-enum
Usage
import createEnum from "create-enum"
const Color = createEnum(["Red", "Green", "Blue"])
Color.size()
// => 3
Color.keys()
// => ['Red', 'Green', 'Blue']
Color.Red.key()
// => 'Red'
Color.Red.ord()
// => 0
API
createEnum(values)
Returns an enumified
object.
values
Type: Array
A list of strings which will act as the new enum properties
Instance
.size()
Returns the size of the enum
const Color = createEnum(["Red", "Green", "Blue"])
Color.size()
// => 3
.keys()
Returns a list of the enum's keys
const Color = createEnum(["Red", "Green", "Blue"])
Color.keys()
// => ['Red', 'Green', 'Blue']
.key()
Returns the <key>
's key as a string
const Color = createEnum(["Red", "Green", "Blue"])
Color.Red.key()
// => 'Red'
.ord()
Returns the <key>
's ordinal value
const Color = createEnum(["Red", "Green", "Blue"])
Color.Red.ord()
// => 0