Introduction
enum-props wraps the excellent enum implementation adrai/enum. It exposes an Enum factory that allows additional metadata to be added to each enum member.
Installation
npm install -S enum-props
Usage
var Enum = ; // define a simple enum (automatically flaggable -> A: 0x01, B: 0x02, C: 0x04)//Uses bitwise 'OR' operation in between the values and creates enumerated constants. For example, if 'Read':1, 'Write':2, then ReadWrite= Read | Write = 1 | 2 = 3;var myEnum = 'A' 'B' 'C'; //define a flagged enum object to create a multicolor option; just pass an arrayvar myEnum = 'Black' 'Red' 'Green' 'Blue';myEnum; //=> Enum {_options: Object, enums: Array[4], Black: EnumItem, Red: EnumItem, Green: EnumItem….....}myEnumisFlaggable; //=> true forvar i=1; i<8; i++ console Black Red Black | Red Green Black | Green Red | Green Black | Red | Green // define an enum with own valuesvar myEnum = 'A': 1 'B': 2 'C': 4; //define enum type without flagvar myEnum = 'None': 0 'Black':1 'Red': 2 'Red2': 3 'Green': 4 'Blue': 5;myEnum; //=> Enum {_options: Object, enums: Array[6], None: EnumItem, Black: EnumItem, Red: EnumItem...}myEnumisFlaggable; //=> false forvar i=0; i<=5; i++ console None Black Red Red2 Green Blue // define an enum with own custom propertiesvar myEnum = 'A': value: 1 descrip: 'first' 'B': value: 2 descrip: 'second' 'C': value: 4 descrip: 'third' ; myEnumAvalue // => 1myEnumAdescription // => 'first' // enums and their members are frozenmyEnumAdescription = 'this is ignored'myEnumAdescription // => 'first' // iterating over an enummyEnumenums;// => None// => Black// => Red// => Red2// => Green// => Blue // get your itemmyEnumA // ormyEnum // ormyEnum // ormyEnum // ormyEnum // get your valuemyEnumAvalue // get your keymyEnumAkey // get all itemsmyEnumenums // returns all enums in an array // comparemyEnumA // ormyEnumA // ormyEnumA // ormyEnumA == 'A' // ormyEnumA == myEnumA // ormyEnumA === myEnumA // check flagvar myItem = myEnum; // or [myEnum.get('A | B')]myItem // ormyItem // ormyItem // other functionsmyItem // returns 'A | C'myItem // returns '"A | C"'myItem // returns 3 JSON // returns '"A | C"' //Type Safety://Newly created enumerable objects are Type-Safe in a way that it's non-configurable and no longer extensible.//Each EnumItem has a beack-reference to a constructor and they are implicitly final.Object; //=> Object {value: EnumItem, writable: false, enumerable: true, configurable: false}Object; //=> falsemyEnum instanceof Enum; //=> true //Instances of Enum created with 'new' from similar objects are not equalmyEnum1='A':1 'B':2 'C':4;myEnum2='A':1 'B':2 'C':4;myEnum1 == myEnum2 //=> falsemyEnum1A == myEnum2A //=> falsemyEnum1Avalue == myEnum2Avalue //=> true //This enum object has no properties other than those defined during its creation. Existing Data is 'Persistent' and preserves the original versionmyEnumBvalue; //=> 2myEnumB = 5; //=> Throws TypeErrordelete myEnumB; //=> falsemyEnumD = 6; //=> doesn't add to the enum object, silently ignoresmyEnumD; // undefined //Try to define new property throws TypeErrorObject;//=>TypeError: Cannot define property:D, object is not extensible.