simple-enum
Simple class for Enum type in JavaScript
You can use it as typical enum or as two-way access map
- Only 17 lines (15 sloc) | 464 Bytes unzipped 🔥
- Simple for use
- No dependencies
Install
You can install this library from npm or yarn:
npm install simple-js-enum
yarn add simple-js-enum
If you have some questions, problems or suggestions you can create an issue, pull request or connect me
Example
Import
const Enum = ;
Creating from Array
const LoadStatus = 'Loading' 'Loaded' 'Error';
Access ways:
- Default way:
console; // 0
- Reverted way:
console; // 'Loading'
Creating from Object
If you want to use numbers different to default you can pass object:
const LoadStatus = Loading: 10 Loaded: 20 Error: 30;console; // 30console; // 'Error'
Get keys or values arrays
- Get array with constants names
const LoadStatus = 'Loading' 'Loaded' 'Error';console; // ['Loading', 'Loaded', 'Error']
- Get array with number values
const LoadStatus = 'Loading' 'Loaded' 'Error';console; // [0, 1, 2]
Unfroze enum
Enum is frozen by default. But you can pass { frozen: false }
and it'll be unfrozen:
const LoadStatus = 'Loading' 'Loaded' 'Error' frozen: false ;LoadStatusSomething = 111
It is not recommended and can leads to errors. For example You can rewrite existed property etc
Licence
Yevhen Diachenko 2018 | MIT