https://github.com/enterwell/js-enum-helper
Helper for creating JS enums.
https://github.com/enterwell/js-enum-helper
Last synced: about 1 month ago
JSON representation
Helper for creating JS enums.
- Host: GitHub
- URL: https://github.com/enterwell/js-enum-helper
- Owner: Enterwell
- License: mit
- Created: 2018-02-02T13:32:45.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-25T03:05:45.000Z (about 1 year ago)
- Last Synced: 2025-04-19T23:33:55.971Z (about 2 months ago)
- Language: TypeScript
- Size: 416 KB
- Stars: 4
- Watchers: 5
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: license
Awesome Lists containing this project
README
We use this *enum-helper* module to easier create and manipulate enums in JavaScript.
Features:
- Create Enum object
- Access Enum objects by value or by name
- Return Enum array## Install
```
$ npm install --save @enterwell/enum-helper
```
or with yarn
```
$ yarn add @enterwell/enum-helper
```## Usage
```js
import Enum from '@enterwell/enum-helper';
```## API
### Enum
#### enumArray
Type: `array`
Array of enum objects.
#### enumObject
Type: `object`
Enum object.
Has this shape: *{ value: Number, name: String, label: String }*### get(enumValue)
Find Enum object by value and return it.
### enumValue
Type: `number`
Enum object value
## Code usage
```javascript
import Enum from '@enterwell/enum-helper';// Enum data
const packagingData = [
{value: 0, name: 'Small', label: 'Small size'},
{value: 1, name: 'Large', label: 'Large size'}
];// Create packagingEnum object (enum representation) from enum data
const packagingEnum = new Enum(packagingData);// Get Enum value by name property
packagingEnum.Small.value; // 0
packagingEnum.Small.label; // 'Small size'// Get Enum object by name property
packagingEnum.Small; // {value: 0, name: 'Small', label: 'Small size'}// Get Enum object by enum value with .get method
packagingEnum.get(0); // {value: 0, name: 'Small', label: 'Small size'}// Return original array with which Enum object was initalized
packagingEnum.toArray(); // The same as packagingData const// Get to not existing value throws an exception
packagingEnum.get(100); // ReferenceError ('Enum object not found');```
## License
MIT © [Enterwell](http://enterwell.net)