https://github.com/chuckterry/enumjs
Enum Implementation in Javascript
https://github.com/chuckterry/enumjs
Last synced: 10 months ago
JSON representation
Enum Implementation in Javascript
- Host: GitHub
- URL: https://github.com/chuckterry/enumjs
- Owner: ChuckTerry
- License: agpl-3.0
- Created: 2022-04-01T19:09:16.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-09T12:16:05.000Z (about 4 years ago)
- Last Synced: 2025-03-20T09:42:10.590Z (over 1 year ago)
- Language: JavaScript
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EnumJS
Enum Implementation in Javascript with ability to lock and unlock.
Version: 1.2.1
## Usage
### Instantiation
```javascript
// The Starting Value will always be the first vaue passed to the constructor
new Enum(/*Array of States*/, /*Surpress Errors*/ [optional]);
const myEnum = new Enum(['OFF', 'LOW', 'MID', 'HIGH'], false);
// Or use the static from() method
const myEnum = Enum.from('OFF', 'LOW', 'MID', 'HIGH']);
```
## Check if object is Enum
```javascript
Enum.isEnum(myEnum) // --> True
```
### Get Current State
```javascript
const currentState = myEnum.getState(); // --> 'OFF'
// Or use the accessor
const currentState = myEnum.state; // --> 'OFF'
```
### Set Current State
```javascript
myEnum.setState('MID'); // --> 'MID'
// Or use the accessor
myEnum.state = 'MID'; // --> 'MID'
```
### Get Number of Valid States
```javascript
myEnum.length; // --> 4
```
### Get All Valid States
```javascript
myEnum.getValidStates(); // --> ['OFF', 'LOW', 'MID', 'HIGH']
```
### Test if State is Valid
```javascript
myEnum.isValidState('MAX'); // --> false
```
### Locking
```javascript
// Locking an Enum will return a key (Symbol) used to unlock it again
const myKey = myEnum.lock();
myEnum.setState('LOW'); // Throws an error (Or nothing if surpressed);
myEnum.unlock(myKey);
myEnum.setState('LOW'); // Succeeds
// Locking Enum with a "true" parameter locks it permanently and returns true
myEnum.lock(true); // --> true
```
### Checking Lock Status
```javascript
myEnum.isLocked(); // --> true
```