slavik57 / enum-values Goto Github PK
View Code? Open in Web Editor NEWLibrary for getting the names and values of typescript enum
License: MIT License
Library for getting the names and values of typescript enum
License: MIT License
i need get value from name :
enum StringEnum {
VALUE4 = 'V4',
VALUE5 = 'V5'
}
for example :
// 'v4'
var nameFromValue1 = EnumValues.getValueFromName(StringEnum, StringEnum.VALUE4 )
Any plans to implement a solution for string enums?
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.
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)
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(),
getValuesand
getNamesAndValues. 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
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]
@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)
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.
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
Now the EnumValues is exported as named export.
Current
import EnumValues from 'enum-values';
To
import {EnumValues} from 'enum-values';
Angular 10 is now throwing this warning:
WARNING in some.component.ts depends on 'enum-values'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
Just wanted to put this on your radar.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.