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

[RFC] Add enum utility function #2606

Closed
wants to merge 1 commit into from

Conversation

colindecarlo
Copy link

Initially this PR is not intended for merge for but primarily as a Request for Comments.

A few days ago I was poking around the code base with a friend and I came across the keyMirror utility function. I could see from how it was being used what the intention behind the function was but I had the feeling that the implementation (and api) of the function wasn't carried through enough to be useable in a practical sense.

When I'm thinking of creating an enum what I want to do is simply define the set of valid values for it, I don't want to be hassled into first constructing an object literal whose keys represent my values. If I'm going to do that I might as well make a macro in my editor which transforms some structured list into that object.

I'd rather do this:

var COLORS = enum('red', 'blue');

than:

var COLORS = keyMirror({
  red: null,
  blue: null
});

I've implemented enum in terms of keyMirror but it really doesn't need to be. If keyMirror is only being used to build enumerations then I think it makes more sense to replace it entirely with enum however I'm not sure how important BC is. I've given some thought on how to bring the two functions together but I'm interested in knowing your opinion on it.

I'm also using the spread operator from ES6, I'm not sure if that's allowed or not.

Note I'm waiting on feedback before adding tests for this, no need to waste time on something that's not going anywhere :)

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@sophiebits
Copy link
Contributor

The goal of the keyMirror function is to be compatible with the advanced mode of Google's Closure Compiler, which means that the keys have to appear as unquoted property keys in the JS source, so we can't use arrays and strings as you're suggesting. Since this is only an internal API anyway, I think we're going to leave it as is – but thank you for sending in the idea!

@sophiebits sophiebits closed this Nov 26, 2014
@zpao
Copy link
Member

zpao commented Nov 26, 2014

What @spicyj said. When I first saw keyMirror I actually wrote something very similar before being told the same thing. It's weird but it has a purpose. I encourage you to release any tools you find useful for yourself on npm / wherever. I'm sure others might find this useful.

@colindecarlo
Copy link
Author

Thanks for the feedback, much appreciated. 👍

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.

4 participants