Toggle combining string regex and number cases

Is there a way to create multiple cases in a single Javascript statement?

In my code, I get the field value through jQuery.

Is it possible that one case checks a string regular expression and another for the number of the same variable?

I think line by line:

var field = $(this).val(); var msg; switch (field) { case field.test('Yes'): msg = "FOO\n"; break; case 10: msg = "BAR\n"; break; } 

Although I saw here: The switch statement for matching strings in JavaScript

To use the switch in strings, we send the switch statement a "true" value.

What will be the shortest (and correct!) Way to achieve this?

+6
source share
3 answers

OK, compiling both answers above my code that worked and (in my opinion) was the most elegant:

 var fieldVal = $(this).val(); var msg; switch (true) { case /Yes/.test(fieldVal): msg = "FOO"; break; case fieldVal > 10 : msg = "BAR"; break; } 

this works as separate if statements, as we evaluate whether it will return true, but in a clearer and more concise way, which can enable us to add completely disparate test statements to the same switch.

the reason it works is likely that the case expression evaluates to true or false, and then checks for the main - switch (true)

+11
source

You cannot have a random value, which is compared to the switch statement, but you can put several cases to execute the same code:

 switch (field) { case 'Yes': case 'yes': case 'YES': msg = "FOO\n"; break; case 10: msg = "BAR\n"; break; } 

but in order to use the test as a case, you can pass true to switch (I found this trick in some open source project):

 switch (true) { case field.test('Yes'): msg = "FOO\n"; break; case field == 10: msg = "BAR\n"; break; } 

but this is the same as if/else

0
source

Note. You are using test() incorrectly, this is a regular expression object method, so you need /Yes/.test(field) , not field.test('Yes') . Anyway...

If you have only two cases, as shown, I would use the if/else/else if structure:

 var field = $(this).val(); var msg; if(/Yes/.test(field)) { msg = "FOO\n"; } else if (field === 10) { msg = "BAR\n"; } 

If you need to add additional cases, I will simply add additional if else {} branches at the end.

If you have several specific numeric cases, you can put them in a switch using the regex tests at the end of default at the end:

 switch (field) { case 10: msg = "BAR\n"; break; case 30: msg = "whatever\n"; break; case 50: msg = "hi\n"; break; default: if (/Yes/.test(field)) { msg = "FOO\n"; else if (/No|N|False|/i.test(field)) { msg = "blah\n"; } break; } 

The switch (true) option you are referring to in the question is actually just a more messy version of if/else/else if , so that doesn't make sense if you don't have some errors:

 switch(true) case /Yes/.test(field): case /Y/.text(field): case /Whatever/.text(field): msg = "FOO\n"; break; case field == 10: msg = "BAR\n"; break; } 

... and even then a if with several conditions related to || may be exactly the same neat with the appropriate newlines, and combining multiple regular expressions into one of them is probably a lot neater.

0
source

Source: https://habr.com/ru/post/951708/


All Articles