Introduction
Enum is a javascript module that introduces the Enum Type. It works for node.js, in the browser and for deno.
...and ref compatible Known Types
Download
Releases for a browser are available for download from GitHub.
Version | Description | Size |
---|---|---|
enum-3.0.4.js |
uncompressed, with comments | Download |
enum-3.0.4.min.js |
compressed, without comments | Download |
Installation (node.js)
$ npm install enum
Installation (browser)
download the standalone file
Usage
// use it as moduleconst Enum = // or with import // or in deno // or in browser<script src="enum.js"></script> // or extend node.js, deno or browser global/window with this new typeEnum // 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 = 3const myEnum = 'A' 'B' 'C' //define a flagged enum object to create a multicolor option just pass an arrayconst myEnum = 'Black' 'Red' 'Green' 'Blue'myEnum //=> Enum {_options: Object, enums: Array[4], Black: EnumItem, Red: EnumItem, Green: EnumItem….....}myEnumisFlaggable //=> true for let i=1; i<8; i++ console Black Red Black | Red Green Black | Green Red | Green Black | Red | Green // define an enum with own valuesconst myEnum = 'A': 1 'B': 2 'C': 4 // if defining a flaggable enum, you can define your own separator (default is ' | ')const myEnum = 'A' 'B' 'C' separator: ' | ' // if you want your enum to have a name define it in the optionsconst myEnum = 'A' 'B' 'C' name: 'MyEnum' // orconst myEnum = 'A' 'B' 'C' 'MyEnum' // if you want your enum to have an explicit "endianness", define it in the options// (defaults to `os.endianness()`)const myEnum = 'A' 'B' 'C' endianness: 'BE' // if you want your enum to be not case sensitive// (defaults to `false`)const myEnum = 'One' 'tWo' 'ThrEE' ignoreCase: true myEnum // => myEnum.OnemyEnum // => myEnum.tWomyEnumThrEE // => true // this option will make instances of Enum non-extensible// (defaults to `false`)const myEnum = 'ONE' 'TWO' 'THREE' freeze: true //define enum type without flagconst myEnum = 'None': 0 'Black':1 'Red': 2 'Red2': 3 'Green': 4 'Blue': 5myEnum //=> Enum {_options: Object, enums: Array[6], None: EnumItem, Black: EnumItem, Red: EnumItem…........}myEnumisFlaggable //=> false myEnum // returns {'None': 0, 'Black':1, 'Red': 2, 'Red2': 3, 'Green': 4, 'Blue': 5}JSON // returns '{"None":0,"Black":1,"Red":2,"Red2":3,"Green":4,"Blue":5}' forconst i=0 i<=5 i++ console None Black Red Red2 Green Blue // 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 // check if it's definedmyEnum // returns truemyEnum // returns truemyEnum // returns true // comparemyEnumA // ormyEnumA // ormyEnumA // ormyEnumA == myEnumA // ormyEnumA === myEnumA // check flagconst 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':4myEnum2='A':1 'B':2 'C':4myEnum1 == 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.