Javascript Pseudo-Enums inspired by Swift
npm install enumerate> An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way withing your code.
Enumerate is JS Pseudo-Enums inspired by Swift.
While we aren't very concerned with type-safety in JS it is sometimes still useful to have a small convention around defining a common type for a group of related values.
Enumerate provides this convention.
v0.0.1
- Underscore or Lodash ~latest
- Development Version of enumerate.js
- Production Version of enumerate.js
- Bundled Production Version of enumerate.js
> The bundled version comes with Underscore 1.8.3 bundled in.
Html
``html`
`javascript
// main.js
(function(window, Enumerate){
// ...
})(window, Enumerate);
`
RequireJS
`javascript
// standard define
define(["app/vendor/enumerate/enumerate"], function(Enumerate){
// ...
});
// or
// commonJS style imports
var Enumerate = require("app/vendor/enumerate/enumerate");
`
Node
`javascript`
var Enumerate = require("enumerate");
> Check out the specs for more usage examples!
> Check out src/enumerate.js for docs / comments!
There are essentially two ways to construct an Enumerate.Enum type:
- As an associative enum
- As an enum with a raw value
Sometimes it's useful to store associated values alongside enum values. This enables the storage of additional custom information and permits this information to vary each time.
`javascript
var ProductCode = new Enumerate.Enum([
"Barcode",
"QrCode",
"ISBN"
]);
var Product = function(productCode) {
this.productCode = productCode;
}
var toy = new Product( ProductCode.Barcode("85909-51226") );
console.log(toy.productCode.value()); // => 85909-51226
`
Sometimes you just need a simple data structure to store some basic values for a small set of read-only object keys. This is where raw value enums come in handy.
> Raw value enums can store any valid JS type.
> Raw value enums should share the same data type by convention.
`javascript
var direction = new Enumerate.Enum({
North: "north",
South: "south",
East: "east",
West: "west"
});
var dir = direction.North;
switch(dir) {
case direction.North:
console.log("To Canada!");
break;
case direction.South:
console.log("To Mexico!");
break;
case direction.East:
console.log("To Europe!");
break;
case direction.West:
console.log("To Japan!");
default:
console.log("Going Nowhere!");
}
// => To Canada!
`
`javascript
// iterate over enum as a list of EnumValues
_.each(direction.values(), function(enumValue){
alert(enumValue.value());
});
// iterate over enum as a list of keys
_.each(direction.keys(), function(key){
alert(key);
});
// iterate over enum as a list of {key, EnumValue} pairs
_.each(direction.keyValues(), function(kv){
alert(kv.key + ": " + kv.enumValue.value())
});
`
Enumerate.EnumValue and Enumerate.Enum are extendable via a familiar .extend method.
`javascript
var DirectionEnumValue = Enumerate.EnumValue.extend({
toString: function() {
var orig = Enumerate.EnumValue.prototype.toString.call(this);
return "[DirectionEnumValue] " + orig;
}
});
var DirectionEnum = Enumerate.Enum.extend({
ValueType: DirectionEnumValue,
toString: function() {
var all = _(this.values()).map(function(enumValue){
return enumValue.toString();
});
return all.join(" | ");
}
});
var direction = new DirectionEnum({
north: "north",
south: "south",
east: "east",
west: "west"
});
console.log(direction.toString());
// => [DirectionEnumValue] north | [DirectionEnumValue] south | [DirectionEnumValue] east | [DirectionEnumValue] west
``