Enumify
A JavaScript library that helps with the enum pattern. Also supports TypeScript.
Installation:
npm install enumify
Basic usage
// Instance properties assert.equal Color.red.enumKey, 'red'; assert.equal Color.red.enumOrdinal, 0; // Prototype methods assert.equal 'Color: ' + Color.red, // .toString() 'Color: Color.red'; // Static `.enumKeys` and static `.enumValues` assert.deepEqual Color.enumKeys, ; assert.deepEqual Color.enumValues, ; // Static `.enumValueOf()` assert.equal Color.enumValueOf'yellow', Color.yellow; // Iterability ; ; for of Color assert.deepEqual iterated, ;
More examples
See:
ts/test/index_test.ts
ts/test/state.ts
Run tests like this (after compiling TypeScript, e.g. via npm run build
):
npm t dist/test/index_test.js