Create an enum from a object of key/values.
Install with npm.
npm install enumjs --save
Use with node.js, browserify or webpack, etc:
var Enum = ;
Or you can use a
<script> tag to include the
index.js and it will create a global
Enum object, or define that object for require.js if it exists.
Once the package is installed you can create instances by passing the key/value object to the constructor.
var Enum = ;const Suits =;
Your new Enum is a plain object that's been frozen so no one can edit the fields or values on it. It is enumerable, immutable, and you can the
hasOwnProperty() just like normal.
Also provided are helper methods
Enum.coalesce(enum: Enum, field: string) and
Enum.enforce(enum: Enum, field: string). Each of these test whether the field provided is a member of the enum and return the value if it is. If the value is not a member then coalesce returns null, while enforce throws an error.
console-> 'HEART'console-> nullconsole-> 'SPADE'console // throws Error
Configure flow by adding the path to Enumjs/interfaces into your .flowconfig file:
To turn Enum objects into flow types simply create a flow type using the
type SuitType = $Keys<typeof Suits>;
If your values are not the same as the keys then you'll probably just want to type out the flow type as a union of values.
const Suits =;type SuitType ='suit_diamonds' | 'suit_diamonds' |'suit_spades' | 'suit_clubs';
Similarly the test file also requires
Array.isArray: see MDN for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill.
Object.keys: see MDN for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill. This is only used in