1.0.4 • Published 6 years ago
@calmdownval/enums v1.0.4
Enums
This module uses the ES modules feature and requires Node v8.15.0+. Please refer to Node's documentation to read more on how to enable this functionality in your environment.
Provides utility functions to simplify working with enum-like objects. To consider object to be enum-like it:
- must only contain unique values
- must use either numeric or string values, but not both
- should use uppercase letters with underscores for names
Example:
const Enum =
{
ITEM_ONE : 1,
ITEM_TWO : 2,
ITEM_THREE : 3
// ...
};
Installation
npm i @calmdownval/enums
Functions
parse(Enum, str)
Returns the value of the best matching member of the enum. Exact match is always preferred. The lookup is case insensitive. Returns null when no suitable match is found.stringify(Enum, value)
Returns the key of the enum to which the value corresponds or null if the enum does not contain the value.coerce(Enum, value)
Checks if value is a member of the enumeration. If so returns it, returns null otherwise.match(Enum, hint)
Similar to coerce, except ifhint
is a string and is not a member of the enumeration parsing is attempted. Handy when dealing with user input.
Example
import * as enums from '@calmdownval/enums';
const Enum =
{
ITEM_ONE : 1,
ITEM_TWO : 2
};
// returns 'ITEM_ONE'
enums.stringify(Enum, Enum.ITEM_ONE);
// returns 2 (value of Enum.ITEM_TWO)
enums.parse(Enum, 'TWO');
Testing
Make sure to first install dev dependencies.
npm test