
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
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
The bundled version comes with Underscore 1.8.3 bundled in.
Html
<script src="static/js/vendor/enumerate/enumerate.min.js" ></script>
<script src="static/js/main.js"></script>
// main.js
(function(window, Enumerate){
// ...
})(window, Enumerate);
RequireJS
// standard define
define(["app/vendor/enumerate/enumerate"], function(Enumerate){
// ...
});
// or
// commonJS style imports
var Enumerate = require("app/vendor/enumerate/enumerate");
Node
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:
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.
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.
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!
// 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.
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
FAQs
Javascript Pseudo-Enums inspired by Swift
We found that enumerate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.