Replace ÅÄÖ (both capital and lowercase) with javascript - javascript

I'm trying to replace Å, Ä and Ö in a user input (a column name) variable. This variable need ex. x00e5 for "å" etc for using retrieving columns (by internalName) from SharePoint, therefore I need the correct format.
I'm checking if the input value has any of Å, Ä and Ö (both capital and lower):
switch (inputValue) {
case inputValue.indexOf('å') > -1:
inputValue = inputValue.replace(/å/g, '_x00e5_');
break;
case inputValue.indexOf('Å') > -1:
inputValue = inputValue.replace(/Å/g, '_x00c5_');
break;
case inputValue.indexOf('ä') > -1:
inputValue = inputValue.replace(/ä/g, '_x00e4_');
break;
case inputValue.indexOf('Ä') > -1:
inputValue = inputValue.replace(/Ä/g, '_x00c4_');
break;
case inputValue.indexOf('ö') > -1:
inputValue = inputValue.replace(/ö/g, '_x00e6_');
break;
case inputValue.indexOf('Ö') > -1:
inputValue = inputValue.replace(/Ö/g, '_x00c6_');
break;
default:
break;
}
Even if one case condition is true, it never enters the case.
This cannot be the simplest/best solution for this?

There is no harm in calling replace() if the target substring is not present. So no switch is needed:
inputValue = inputValue
.replace(/å/g, '_x00e5_')
.replace(/Å/g, '_x00c5_')
.replace(/ä/g, '_x00e4_')
.replace(/Ä/g, '_x00c4_')
.replace(/ö/g, '_x00e6_')
.replace(/Ö/g, '_x00c6_');

Related

JS: Why don't the switch statement cases `case 2` and `case day == 2` print out the same result?

I understand the following code.
var day = 2;
switch (day) {
case 1:
document.write("Monday");
break;
case 2:
document.write("Tuesday!!");
break;
case 3:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
It prints out "Tuesday!!".
However, why doesn't the following work? I though it should have printed the same answer, but it keeps printing "Another Day"!?
var day = 2;
switch (day) {
case day == 1:
document.write("Monday");
break;
case day == 2:
document.write("Tuesday!!");
break;
case day == 3:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
A case in a switch statement tries to match with the switch condition directly. So your snippet:
var day = 2;
switch (day) {
case day == 1:
document.write("Monday");
break;
case day == 2:
document.write("Tuesday!!");
break;
case day == 3:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
Is actually equivalent to:
var day = 2;
switch (day) {
case false:
document.write("Monday");
break;
case true:
document.write("Tuesday!!");
break;
case false:
document.write("Wednesday");
break;
default:
document.write("Another day");
}
And since day is not equal to either true or false (since it is, in fact, 2), the switch falls through to its default case.
You can see that cases use strict equality from the docs (emphasis mine):
A switch statement first evaluates its expression. It then looks for
the first case clause whose expression evaluates to the same value as
the result of the input expression (using strict comparison, ===) and
transfers control to that clause, executing the associated statements.
It's because switch case statements compare the supplied arguments automatically. So on calling day == 3, it's actually running day == 3 == 3 which isn't a correct syntax.
Hope it clear your doubts.

Switch case match with string

function getFieldType(value){
var type = "";
switch(value){
case "TEXT_BOX":
type=FieldTypeEnum.TEXT_BOX;
break;
case "PASSWORD":
type=FieldTypeEnum.PASSWORD;
break;
case "CHECK_BOX":
type=FieldTypeEnum.CHECK_BOX;
break;
case "RADIO_BUTTON":
type=FieldTypeEnum.RADIO_BUTTON;
break;
case "DROP_DOWN":
type=FieldTypeEnum.DROP_DOWN;
break;
case "SEARCH_CARD_BOX":
type=FieldTypeEnum.SEARCH_CARD_BOX;
break;
case "STATE_DROP_DOWN":
type=FieldTypeEnum.STATE_DROP_DOWN;
break;
case "ID_DROP_DOWN":
type=FieldTypeEnum.ID_DROP_DOWN;
break;
case "GENDER_DROP_DOWN":
type = FieldTypeEnum.GENDER_DROP_DOWN;
break;
default:
type = "";
}
return type;
}
I want to combined three drop down box (STATE_DROP_DOWN, ID_DROP_DOWN, GENDER_DROP_DOWN)into one .
Means I want something like below :
case String.endwith("_DROP_DOWN"):
type=FieldTypeEnum.ID_DROP_DOWN;
break;
Is it possible in JavaScript? If yes then how?
It is possible, in a javascript switch/case statement you can compare for string equality (which evaluates to true or false) or more complex statements which just as when testing for string equality, also evaluate to true or false (e.g. String.endwith("_DROP_DOWN")). However personally I would prefer the following:
STATE_DROP_DOWN:
// intentional fall through
ID_DROP_DOWN:
// intentional fall through
GENDER_DROP_DOWN:
//.. do your stuff
break;
I believe this way it is more clearly through which switch case you are falling in a certain situation.
You can put a ternary operator in the case, and test if it ends with "DROP_DOWN" return value otherwise blank string.
switch(value){
case value.endsWith("_DROP_DOWN") ? value : '' {
type=FieldTypeEnum.ID_DROP_DOWN;
break;
}
}

Why "≠" is converted to "≠", "≥" to "≥", "≤" to "≤" in browsers developers tool(javascript)?

I am working on Javascript/angularjs. From UI, i get symbols as below:
'>' Greater Than
'<' Less Than
'=' Equal
'≠' Not Equal
'≥' Greater Than or Equal to
'≤' Less Than or Equal to
I am using switch case on received symbol
switch (operand) {
case '>':
value = blabla1; break;
case '<':
value = blabla2; break;
case '=':
value = blabla3; break;
case '≠':
value = blabla4; break;
case '≥':
value = blabla5; break;
case '≤':
value = blabla6; break;
default:
value = xyz;
}
My issue is the above gets converted to below in browser("≠" is converted to "≠", "≥" to "≥", "≤" to "≤" ). From which I am not able to do calculations.
switch (operand) {
case '>':
value = blabla1; break;
case '<':
value = blabla2; break;
case '=':
value = blabla3; break;
case '≠':
value = blabla4; break;
case '≥':
value = blabla5; break;
case '≤':
value = blabla6; break;
default:
value = xyz;
}
Can somebody tell me why this is happening? I have used charset="utf-8" in html. Also i tried in online to change the above symbols to encode and decode but none of them resulted in the exact symbol.
Thanks in advance.
Make sure your browser is also using UTF-8 encoding.
Here's a link that will help you check and change your browser's encoding.

Switch combining cases string regex and number

Is there a way to create multiple cases in a single Javascript switch statement?
In my code I receive the value of a field via jQuery.
Is it possible that one case checks for string regex and another for number of the same variable?
I am thinking along the lines of:
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: Switch statement for string matching in JavaScript
That the way to use switch on strings is by sending the switch statement a "true" value.
What would be the most concise (and correct!) way to achieve this?
OK, compiling both answers above my code that worked and was most elegant IMO is:
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 since we are evaluating whether or not the case returns true but in a clearer and more concise way that could give us the option to add totally disparate test statements in one switch.
the reason it works is probably that the case expression evaluated is interpreted as a true or false value and then checked against the main -
switch(true)
You can't the case need to single value, that's compared to switch expression, but you can put multiple 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 test as case you can pass true to switch (I found that 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
Note: you're using test() incorrectly, it's a method of a regex object, so you need /Yes/.test(field) rather than field.test('Yes'). Anyway...
If you've only got two cases as shown then I'd use an 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'd just add extra if else {} branches on the end.
If you have several specific numeric cases you might consider putting them in a switch with the regex tests in a 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 alluded to in the question is really just a messier version of an if/else/else if, so it doesn't really make sense unless you have some fall-through cases:
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 an if with multiple conditions joined by || arguably just as tidy with appropriate newlines, and combining multiple regexes into a single one is probably a lot neater.

Replace with the regular expression

What regular expression I need to use to correct
if (text.indexOf("+") != -1) {
action = "+";
} else if (text.indexOf("-") != -1) {
action = "-";
} else if (text.indexOf("*") != -1) {
action = "*";
} else if (text.indexOf("/") != -1) {
action = "/";
}
this code
?
EDIT:
and how can I improve this code:
switch (action) {
case "+":
result = parseInt(array[0]) + parseInt(array[1]);
break;
case "-":
result = parseInt(array[0]) - parseInt(array[1]);
break;
case "*":
result = parseInt(array[0]) * parseInt(array[1]);
break;
case "/":
result = parseInt(array[0]) / parseInt(array[1]);
break;
default:
break;
}
Sorry for dull questions I am new in js.
You can use either of these:
var action = text.replace(/.*([+*/-]).*/,'$1');
var match = /[*+/-]/.exec(text);
var action = match && match[0];
If there's the possibility of newlines in your text then change the first to:
var action = text.replace(/[\d\D]*([+*/-])[\d\D]*/,'$1');
Edit: You can improve your switch statement by using, e.g.
// allow floating point values, and also
// don't accidentally interpret "011" as octal
array[0]*1 + array[1]*1;
For more details on why parseInt is probably bad, see this answer.
You can also remove the superfluous default section of your case statement. However, I suspect that your desired "improvement" was making fewer lines. Given that =-*/ are operators in JavaScript (and not methods), I cannot think of any way to avoid having four calculations (i.e. a switch or if/else if).
Whereas in Ruby you could, for example, use array[0].send(action,array[1]) to cover all four cases ;)

Categories

Resources