rauschma/enums — pull request #7 by brianmgray (2014-10-27 21:34:56+00:00)
Title: Add methods to select an enum fromName and from matching value

Hi! Great and very useful library. I have added some improvements that I needed for my project and wanted to share them back if you found them useful. I added tests for all.

1) I added a package.json file to make it easier to pull in for those using browserify (also updated to the latest karma and added a test script so you can test using 'npm test')

2) I added a fromName function to support this:

var color = new enums.Enum("red", "green", "blue");
var red = color.fromName("red");

3) More interestingly (to me), I added a fromValue function. We are pulling these values from a DB and I would like to convert to enums on the client side. So the ability to do so is useful for me. Here is an example from the tests:

var color = new enums.Enum({
red: { r: 255, g: 2, b: 0 },
green: { r: 0, g: 255, b: 0 },
blue: { r: 0, g: 0, b: 255 }
});
var red = color.fromValue({ r: 255 }); // returns the single enum
var red = color.fromValue({ r: 255, g: 2 }); // returns red more specifically
var noReds = color.fromValue({ r: 0 }); // returns a list containing green and blue
var value = color.fromValue({ foo: "bar" }); // returns undefined