1.0.0 • Published 7 years ago
crenum v1.0.0
Crenum
A tiny utility for creating Enums in JavaScript. Works in any ES5+ compliant environment.
- AMD build is not provided yet.
- ESM export is not provided yet.
Installation
$ npm install crenum
# or
$ yarn add crenum
Usage example
var Enum = require('crenum');
var Color = new Enum(['red', 'blue', 'green']);
/*
{
RED: 'RED',
BLUE: 'BLUE',
GREEN: 'GREEN'
}
*/
var myColor = Color.RED; // 'RED'
API
new Enum(items, options?);
items
- Array of at least 2 unique non-empty string constants. Required.
options
- Options object.
options.strict
(boolean) - Apply type and integrity checks to the first argument, may throw TypeError. Defaulttrue
. Usefalse
when you are sure about your input and need to create many objects on the fly.options.transform
((value: string) => string) - Transform string representations of enum elements. Defaultvalue => UPPER_SNAKE_CASE_VALUE
. Use it to transform your enum values to different cases. Make sure that the values after transforming stay compliant withoptions.strict
.options.numericValues
(boolean) - Use integer indexes (starting from 1) as enum element values instead of strings. Defaultfalse
.
Returned object is frozen (cannot be modified) and prototype-less (no builtin properties).
License
1.0.0
7 years ago