enumjs
This is an attempt at realizing type-safe enums in JavaScript. I'm most familiar with the way enums work in Java, and so I modeled this library after that. JavaScript doesn't have true enums. Most workarounds to this problem involve using a map where the keys represent the enum constants, and the values are integers or string-representations of the enum constants. This is a convenient solution, but the main problem is that you don't really get any type-safety since the values are just regular JavaScript types. This means that you can't even do instanceof
checks, and you have to resort to checking the value against all defined-values to see if it is valid.
I figured there must be a better way to realize enums in JavaScript while addressing these shortcomings, and this is my attempt to do that. As far as I can tell, this works like one would expect, but I'm sure there are things I haven't considered. So here's what you get with enumjs:
- The ability to define your own enum and its constants.
- Your custom enum is its own type, and all its constants are instances of the enum itself. This means that you can do
instanceof
checks. - The custom enum has a
values
and afromName
* method. The former returns an array of all constants defined on the enum, and the latter will attempt to return an enum consant with the same name as the string that is passed in. If one does not exist, an exception is thrown. - Each enum constant has a
name()
and anordinal()
method. Thename()
method returns a string representing the name of the constant (as defined), andordinal()
returns the position of the constant (as defined). - Once defined, the enum type and its constants are immutable.
*In Java the method is actually called valueOf
and that's what I named it here originally as well. However, JavaScript has its own valueOf
method on objects that does something else entirely, and I didn't want to override that behavior.
This works on the browser, in Nashorn, and on Node. The library exposes a single object (called Enum
if you're on the browser or have loaded the file in Nashorn) with a method called define
. The signature is Enum.define(<string>, <array> | <object>)
.
The current version is 1.0.2 and is available here. The library is also available as a node package called node-enumjs. To install, just run npm install node-enumjs
. Then you can use it like so:
var Enum = ;
Examples
var Days = Enum; DaysMonday instanceof Days; // true DaysFridayname; // "Friday"DaysFriday; // 4 DaysSunday === DaysSunday; // trueDaysSunday === DaysFriday; // false DaysSunday; // "Sunday" Days // "Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } " Days; //["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]Days4name; //"Friday" Days === DaysThursday // trueDaysname // "Wednesday"DaysFridayname // "Saturday"
You can also attach behavior to each constant, just like in Java. To do that, you need to pass in a definition object that looks like this:
var Days = Enum; DaysMonday; // "Mondays are bad!"DaysFriday; // "Friday, Friday, Gettin' down on Friday!"
Sometimes you may want to have similar behavior that is shared among all constants. But doing that in the above manner is tedious and repetitive. Instead, you can pass in the optional attribute methods
. All values defined in this object must be functions, and these functions will be attached to every constant of the enum. To demonstrate this, here's an example that's based on the Planet example from the Java documentation on enums:
var Planet = Enum; var mass = 175 / PlanetEARTH;Planet;
This returns the output:
Your weight on MERCURY is 66.10758266016366
Your weight on VENUS is 158.37484247218296
Your weight on EARTH is 174.99999999999997
Your weight on MARS is 66.27900720649754
Your weight on JUPITER is 442.8475669617546
Your weight on SATURN is 186.55271929202414
Your weight on URANUS is 158.39725989314937
Your weight on NEPTUNE is 199.20741268219012
That's pretty much it. Please try it out and let me know what you think, and if there are any issues, etc.