Introduction
This library is inspired by Java Enums, enumify, and enum. The goal was to create a JavaScript enum library that is safe, extensible, flexible, and JSON parsable. This project also uses the latest ES spec wherever possible.
Installation
Files
All distribution files can be found in the dist
folder
Version | Description |
---|---|
enum-nxt.umd.min.js | Minified version that uses UMD. Most common option. |
enum-nxt.umd.js | Unminified version that uses UMD. |
enum-nxt.js | Source code with comments. Useful if you want to use it for transpiling. |
Example Usage
; //Creating enum from arguments listlet argsEnum = 'RED' 'GREEN' 'BLUE'; //Creating enum from string arraylet arrayEnum = 'RED'; //Creating enum from object with custom attributeslet objEnum = 'RED': foo: 1 'BLUE': {} 'GREEN': {} 'YELLOW': foo: 2; //Member examplesobjEnumREDfoo === 1;objEnum"RED"foo === 1;objEnumREDname === 'RED';objEnumsize === 4;argsEnumGREENname === 'GREEN';argsEnumsize === 3; //Function examplesforlet key value of objEnum {}forlet key value of objEnum {}forlet key of objEnum {}forlet value of objEnum {} var enumArray = Array; objEnum; let value = objEnumRED;objEnum === false;objEnum === true;objEnum === false;objEnum === true;objEnum === false;objEnum; //Throws TypeError 'key must be of type string or Object' objEnum;Object === true;objEnumfoo = 23; // throws a TypeError under strict mode //JSON stringify and parsevar json = JSON;var otherEnum = JSON;
See also
- The source file (
src/enum.es6
) for comments - The unit tests (
src/enum.spec.es6
) for more example usage