Giter Site home page Giter Site logo

enum-values's People

Contributors

sbosell avatar slavik57 avatar zorgatone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

enum-values's Issues

get value from name

i need get value from name :

enum StringEnum {
    VALUE4 = 'V4',
    VALUE5 = 'V5'
  }

for example :

// 'v4'
var nameFromValue1 = EnumValues.getValueFromName(StringEnum, StringEnum.VALUE4 )

Update readme

Now the EnumValues is exported as named export.

Current
import EnumValues from 'enum-values';

To
import {EnumValues} from 'enum-values';

Considerations about extending functionalities and fixing conceptual issues

Hi @slavik57 and everyone,

Nice work we've got here,
me and my colleagues have been using the enum-values npm package in a couple of our work projects and we kinda liked it.

I'm currently elaborating and working on a code solution for a pull request that spans over a couple new functionalities and fixing some conceptual issues.
I'll try to keep this short and simple, and if you want we can decide together over two possible approaches for the next version's release.

1.st Issue: getting the name(s) from a value

I've seen there is a conceptually flawed mapping between names and values: we have unique name keys and (possibly) non-unique values.
So if I want to get the value for an unique name (relation 1-to-1) I'll surely get one value as currently implemented (ok, then). But we don't necessarily have the opposite, that is getting names from non-unique values.

I'll post an example here...

TypeScript file:

enum MyEnum {
    VALUE1 = 1,
    VALUE2 = "VALUE2",
    VALUE_ONE = 1,
    VALUE_TWO = "VALUE2",
    VALUE_UNIQUE = "VALUE_UNIQUE"
}

Compiled JavaScript output:

var MyEnum;
(function (MyEnum) {
    MyEnum[MyEnum["VALUE1"] = 1] = "VALUE1";
    MyEnum["VALUE2"] = "VALUE2";
    MyEnum[MyEnum["VALUE_ONE"] = 1] = "VALUE_ONE";
    MyEnum["VALUE_TWO"] = "VALUE2";
    MyEnum["VALUE_UNIQUE"] = "VALUE_UNIQUE";
})(MyEnum || (MyEnum = {}));

which in the end boils down to this JavaScript object:

{
    "1": "VALUE_ONE",
    "VALUE1": 1,
    "VALUE2": "VALUE2",
    "VALUE_ONE": 1,
    "VALUE_TWO": "VALUE2",
    "VALUE_UNIQUE": "VALUE_UNIQUE"
}

As you can see here we have two names that "map" to the same value (1) and two names that "map" to another identical value ("VALUE2")

First possibility:

We can release a minor version update (1.2.x) with the deprecated old methods (for compatibility with existing projects' code) and add two new methods: getNamesFromValue (note the "names" is plural, which indicates the return type is string[]) and another one that fixes the existing getNamesAndValues with a different name (I can't decide a good wording for it, right now).
We can in a later major version release (1.y.x) remove the old methods and keep the new ones

Second possibility:

We could directly make a major version (1.y.x) with the renamed method getNamesFromValue and string[] return type (let's keep the implementation part separate for now, I'm still working on the code as we speak)

2.nd Proposal: name Map / values Map

This is a new feature request, that I'm trying to implement here:
https://github.com/t-ricci-molecle/enum-values/blob/enum-map/src/enumValues.ts

This methods would return an ES6/ES-2015 Map which, for one method, can be repeatedly used (with possibly a time complexity of O(1) if supported by the browser and not polyfilled) to access the values from the given name, or to access the name (which as issue 1 explains should be a names array) from the given value.

The use case of these two methods (we can name them getNameMap or getNamesMap and getValueMap or getValuesMap) can be critical when you don't want to call too many times the old (or fixed) methods: getNameFromValue(), getNames(), getValuesandgetNamesAndValues. Basically we'll be able to easily convert an Enum to one of the two Maps for fast access. I'd see well this feature in the enum-values` package instead of a separate npm module

3.rd consistency: getValueFromName

This is less practical, but for consistency if we have a getNameFromValue (fixed getNamesFromValue) we should also have the opposite method getValueFromName(MyEnum, myValue) (which would simply be the same as MyEnum[myValue]

Conclusion

@slavik57 let me know what you think about the 3 changes, I can either make separate Pull Requests or a single big one if we agree to bump the major version (we can create a branch for a feature release called 1.2.x, so we can do further development, changes and testing before releasing it on npm)

git-flow-style "Develop" branch?

You don't have a "develop" branch similar to how git-flow does?
I personally find it useful to merge in it PR you'll planning for a future release and still don't want to merge in master

Split getValues to get[Mixed/Numeric/String]Values

It's annoying to deal with a [number | string] array of enum values if I don't actually use mixed enums. Is it possible to split the getValues function into getMixedValues (the current version), getNumericValues (only returns number) and getStringValues (only returns string)?

For those last 2 versions, not sure if it's better to crash or ignore a mixed value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.