Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gfarrell/enum.js
Javascript enum factory
https://github.com/gfarrell/enum.js
Last synced: 13 days ago
JSON representation
Javascript enum factory
- Host: GitHub
- URL: https://github.com/gfarrell/enum.js
- Owner: gfarrell
- Created: 2014-05-15T13:04:10.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-29T18:47:51.000Z (about 10 years ago)
- Last Synced: 2024-12-16T15:08:42.152Z (about 1 month ago)
- Language: JavaScript
- Size: 188 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Enum.js
=======Sometimes we want immutable enums in Javascript, but standard objects are not really ideal for this, and can be accidentally modified.
Enum.js solves this problem by providing a factory function to generate immutable enums (where possible in the browser). If a browser does not support the methods used, then Enum.js will fall back to using mutable objects.
Using Enum.js is quite simple:
var Enum = require('enum');
var MyEnum = new Enum('First Value', 'Second', 'thirdVal', '4');This will give you an object with the following key-value pairings:
MyEnum.FIRST_VALUE; // 1
MyEnum.SECOND; // 2
MyEnum.THIRD_VAL; // 3
MyEnum[4]; // 4
Enum.js automatically underscores and capitalises the entire key, to make sure it's standard.If you should try to specify a key twice, Enum.js will only use the first instance of it.