Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Enum To Corresponding String Value

So I have this in the javascript for my page:

var TEST_ERROR  = {         'SUCCESS'   :   0,         'FAIL'      :   -1,         'ID_ERROR'  :   -2       }; 

And perform tests on functions in the page like so:

function test() {     // Get the paragraph from the DOM     var testResultParagraph = document.getElementById('testResult');      // Check the paragraph is valid     if(!testResultBox)     {         // Update the web page         testResultParagraph.value = TEST_ERROR.ID_ERROR;         return TEST_ERROR.ID_ERROR;     }      // Something to store the results     var testResult = TEST_ERROR.SUCCESS;      // Test the calculation     testResult = testCalculate()      // Update the web page     testResultParagraph.value = testResult;      // The test succeeded     return TEST_ERROR.SUCCESS; } 

The result of testCalculate() and the value of the paragraph will be either 0, -1, -2 depending on the outcome.

Now I want to map this to a string so that the paragraph shows 'Success', 'Fail' or 'ID Error'

I could do this a few ways I have figured:

var TEST_ERROR  = {         'SUCCESS'   :   {VALUE : 0 , STRING: 'Success' },         'FAIL'      :   {VALUE : -1, STRING: 'Fail'    },         'ID_ERROR'  :   {VALUE : -2, STRING: 'Id Error'},       }; 

would require a modification to the enum dot accessors, or

var TEST_ERROR  = {         'SUCCESS'   :   0,         'FAIL'      :   1,         'ID_ERROR'  :   2       };  var TEST_STRING = [         'Success',         'Fail',         'ID Error'       ]; 

Which would require changes to the logic (result > TEST_ERROR.SUCCESS seems wierd tho!)

My question is how would you go about mapping an enumerator value to a string value in Javascript? I'm thinking the second way is the most sensible, but would like the enumerator to be positive for successes and negative for fails. I also like the idea of the first containing the strings and values in the object structure.

Any ideas?

Thanks!

Matt

PS. I'm going to be doing the testing in a Web Worker, so that the page doesn't hang and the results will be put into a table, not a paragraph like above.

PPS. I'm pretty new to Javascript programming, but do a lot in ASM, C, C++, C#.

like image 507
Matt Clarkson Avatar asked Nov 26 '10 16:11

Matt Clarkson


People also ask

Are TypeScript enums bad?

The they are useless at runtime argument This is a false argument for typescript in general, let alone Enums. and agree, if at runtime some code tries to change the values of one of your enums, that would not throw an error and your app could start behaving unexpectedly ( that is why Object.

What is string enum?

String enums are similar to numeric enums, except that the enum values are initialized with string values rather than numeric values. The benefits of using string enums is that string enums offer better readability. If we were to debug a program, it is easier to read string values rather than numeric values.

Can I use enum as a type in TypeScript?

Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.


2 Answers

Do you actually need the numeric values at all? If not then you could use something like this:

var TEST_ERROR  = {     SUCCESS  : 'Success',     FAIL     : 'Fail',     ID_ERROR : 'ID Error' }; 
like image 53
LukeH Avatar answered Oct 05 '22 12:10

LukeH


Not quite optimal, but the cleanest you can get without pre-computing the reverse dictionary (and besides, this shouldn't be too much of an issue if you only have a few enumeration values):

function string_of_enum(enum,value)  {   for (var k in enum) if (enum[k] == value) return k;   return null; } 
like image 31
Victor Nicollet Avatar answered Oct 05 '22 11:10

Victor Nicollet