Enumify
A JavaScript library that helps with the enum pattern. Also supports TypeScript.
Installation:
npm install enumify
Basic usage
class Color extends Enumify {
static red = new Color();
static orange = new Color();
static yellow = new Color();
static green = new Color();
static blue = new Color();
static purple = new Color();
static _ = this.closeEnum();
}
assert.equal(
Color.red.enumKey, 'red');
assert.equal(
Color.red.enumOrdinal, 0);
assert.equal(
'Color: ' + Color.red,
'Color: Color.red');
assert.deepEqual(
Color.enumKeys,
['red', 'orange', 'yellow', 'green', 'blue', 'purple']);
assert.deepEqual(
Color.enumValues,
[ Color.red, Color.orange, Color.yellow,
Color.green, Color.blue, Color.purple ]);
assert.equal(
Color.enumValueOf('yellow'),
Color.yellow);
const result = [];
const iterated = [...Color];
for (const c of Color) {
result.push('Color: ' + c);
}
assert.deepEqual(
iterated, [
Color.red,
Color.orange,
Color.yellow,
Color.green,
Color.blue,
Color.purple,
]);
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