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
const Enum = require('enum')
import Enum from 'enum'
import Enum from 'https://deno.land/x/enum/index.js'
<script src="enum.js"></script>
Enum.register()
const myEnum = new Enum(['A', 'B', 'C'])
const myEnum = new Enum(['Black', 'Red', 'Green', 'Blue'])
myEnum
myEnum.isFlaggable
for (let i=1; i<8; i++){ console.log(myEnum.get(i).value + '=> '+ myEnum.get(i).key)}
1=> Black
2=> Red
3=> Black | Red
4=> Green
5=> Black | Green
6=> Red | Green
7=> Black | Red | Green
const myEnum = new Enum({'A': 1, 'B': 2, 'C': 4})
const myEnum = new Enum(['A', 'B', 'C'], { separator: ' | ' })
const myEnum = new Enum(['A', 'B', 'C'], { name: 'MyEnum' })
const myEnum = new Enum(['A', 'B', 'C'], 'MyEnum')
const myEnum = new Enum(['A', 'B', 'C'], { endianness: 'BE' })
const myEnum = new Enum(['One', 'tWo', 'ThrEE'], { ignoreCase: true })
myEnum.get('one')
myEnum.get('TWO')
myEnum.ThrEE.is('three')
const myEnum = new Enum(['ONE', 'TWO', 'THREE'], { freeze: true })
const myEnum = new Enum({'None': 0, 'Black':1, 'Red': 2, 'Red2': 3, 'Green': 4, 'Blue': 5})
myEnum
myEnum.isFlaggable
myEnum.toJSON()
JSON.stringify(myEnum)
for(const i=0 i<=5 i++){ console.log(myEnum.get(i).value + '=> '+ myEnum.get(i).key)}
0=> None
1=> Black
2=> Red
3=> Red2
4=> Green
5=> Blue
myEnum.enums.forEach(function(enumItem) {
console.log(enumItem.key)
})
myEnum.A
myEnum.get('A')
myEnum.get(1)
myEnum.get('A | B')
myEnum.get(3)
myEnum.A.value
myEnum.A.key
myEnum.enums
myEnum.isDefined(myEnum.A)
myEnum.isDefined('A')
myEnum.isDefined(1)
myEnum.A.is(myEnum.A)
myEnum.A.is('A')
myEnum.A.is(1)
myEnum.A == myEnum.A
myEnum.A === myEnum.A
const myItem = myEnum.get(3)
myItem.has(myEnum.A)
myItem.has('A')
myItem.has(1)
myItem.toString()
myItem.toJSON()
myItem.valueOf()
JSON.stringify(myItem)
Object.getOwnPropertyDescriptor(myEnum, 'Red')
Object.isExtensible(myEnum)
myEnum instanceof Enum
myEnum1=new Enum({'A':1, 'B':2, 'C':4})
myEnum2=new Enum({'A':1, 'B':2, 'C':4})
myEnum1 == myEnum2
myEnum1.A == myEnum2.A
myEnum1.A.value == myEnum2.A.value
myEnum.B.value
myEnum.B = 5
delete myEnum.B
myEnum.D = 6
myEnum.D
Object.defineProperty(myEnum, D, { value:6, writable:false, enumerable:true })