Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to select an enum fromName and from matching value #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

brianmgray
Copy link

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");
  1. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant