Master Enumerator
A JavaScript library for psuedo-enums (enumerators)
The Basics
Install:
npm install master-enumerator
Use:
This constructor takes in an array which houses the string enum values, it can also optionally be constructed with a custom properties object (as of right now caseSensitive is the only configurable property) - caseSensitive === true
will preserve the value of the string in the array - by default the key AND value will always be uppercase - caseSensitive === false
then the value is not manipulated in any way (i.e. with toUpperCase() like in the default scenario).
array options? caseSensitive: boolean );
Examples:
No custom options are specified - defaulting caseSensitive = false
const Enumerator = ; const enumerator = 'one' 'two' 'three' ; Enumerator getter: return 'ONE' getter: return 'TWO' getter: return 'THREE' enumerations: key: 'ONE' value: 'ONE' key: 'TWO' value: 'TWO' key: 'THREE' value: 'THREE' } enumeratorONE // output: 'ONE'enumeratorTWO // output: 'TWO'enumeratorTHREE // output: 'THREE'
The one custom option is specified - making caseSensitive = true
const Enumerator = ; const enumerator = 'one' 'tWo' 'THREE' caseSensitive: true ; Enumerator getter: return 'one' getter: return 'tWo' getter: return 'THREE' enumerations: key: 'ONE' value: 'one' key: 'TWO' value: 'tWo' key: 'THREE' value: 'THREE' } enumeratorONE // output: 'one'enumeratorTWO // output: 'tWo'enumeratorTHREE // output: 'THREE'
Methods of Enumerator
validValue:
This method allows you to check a string value against your psude-enumerator data type, returning true
if the string value is a correlating type of this enumerator and false
if it is not.
const enumerator = array options? caseSensitive: boolean ); enumerator;
Example:
const Enumerator = ; const enumerator = 'one' 'two' 'three' ; enumerator; // output: trueenumerator; // output: trueenumerator; // output: true
const Enumerator = ; const enumerator = 'one' 'tWo' 'THREE' caseSensitive: true ; enumerator; // output: trueenumerator; // output: falseenumerator; // output: false
validValues:
This method allows you to check an array of values against your psude-enumerator data type, returning an array with booleans - true
if the value in the same exact index location in the passed in array is a correlating type/value of this enumerator and false
if it is not.
const enumerator = array options? caseSensitive: boolean ); enumerator;
Example:
const Enumerator = ; const enumerator = 'one' 'two' 'three' ; enumerator; // output: [true, true, true]
const Enumerator = ; const enumerator = 'one' 'two' 'three' caseSensitive: true ; enumerator; // output: [true, false, false]
enumerationKeys():
This method returns the Enumerator's getter keys/properties that are used to get the values behind its enums.
const enumerator = array options? caseSensitive: boolean ); enumerator;
Example:
const Enumerator = ; const enumerator = 'one' 'two' 'three' ; enumerator; // output: [ 'ONE', 'TWO', THREE' ]
const Enumerator = ; const enumerator = 'one' 'tWo' 'TRHEE' caseSensitive: true ; enumerator; // output: [ 'ONE', 'TWO', THREE' ]
enumerationValues():
This method returns an array of the Enumerator's values behind its enums/getter keys/properties.
const enumerator = array options? caseSensitive: boolean ); enumerator;
Example:
const Enumerator = ; const enumerator = 'one' 'two' 'three' ; enumerator; // output: [ 'ONE', 'TWO', THREE' ]
const Enumerator = ; const enumerator = 'one' 'tWo' 'TRHEE' caseSensitive: true ; enumerator; // output: [ 'one', 'tWo', THREE' ]
More information
- The directory
test/
contains very thorough TDD and BDD examples/tests.