Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/renehernandez/enums
Enums module for easy creation of enum types in javascript
https://github.com/renehernandez/enums
Last synced: 21 days ago
JSON representation
Enums module for easy creation of enum types in javascript
- Host: GitHub
- URL: https://github.com/renehernandez/enums
- Owner: renehernandez
- License: mit
- Created: 2015-04-11T18:43:01.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-04-13T14:10:17.000Z (over 9 years ago)
- Last Synced: 2024-10-15T22:14:59.370Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 156 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# enums.js
enums is a 2 KB (minified) standalone module for getting a supported Enum instance.
## Enum instance
The Enum instance contains a property for each valid string passed as argument of the create method.#### enums.create()
Use `enums.create` to get a new instance of Enum type.
```js
var someVar = 'Red';
var colors = enums.create('Red', 'Blue', 'Yellow');
if (someVar === colors.Red){
alert('Yeah!! I am Red.');
}
```
Instead, it is possible just to pass a single array with all the desired enum keys
```js
var someVar = 'Blue';
var colors = enums.create(['Red', 'Blue', 'Yellow']);
if (someVar === colors.Blue){
alert('Well Yeah!! Now, I am Blue.');
}
```
Or even pass an object to read their properties
```js
var someVar = 3;
var colors = enums.create({ Red: 1, Blue: 2, Yellow: 3 });
if (someVar === colors.Yellow){
alert('Oh!! Now, it is Yellow time.');
}
```
**Notice:**
- At least one argument must be passed as part of the method call.
- Arguments must be of string type or a single array of string values or a single object.
- All keys must be of distinct values.
- When creating an Enum using an object as argument, only the own object properties will be used as keys for the Enum instance.
- The newly created Enum instance is frozen as part of the `enums.create` call.
- The difference when creating an Enum using an object as argument: It is possible to set a specific value for each key using this way.## Specs
Tested using Jasmine (>= v2.0.0).
See enums-specs.js file to review the actual specs suite.## Manual Installation
Ensure you're using the files from the `dist` directory (it contains production-ready code).## Contributing
In lieu of a formal styleguide, please take care to follow the existing code style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.