This my actual scenario
const cleanString=( string )=>{
let city = getAccentedCity(string);
if( city.indexOf(' ')<0 ) return city;
return city.replaceAll(' ', ' ');
}
I have now to add an other case when a city contains the string "ß"
and i want to replace it with 's'
How can i add it in my current function?
CodePudding user response:
First of all you should be aware that String.replaceAll()
function is not supported on IE.
If you're ok with it and I assume you are since you're already using it, then I would just create an array of pairs that you can spread into the replaceAll
function.
Something like this:
const replaceMap = [
[' ', ' '],
['ß', 's'],
];
const cleanString = (string) => {
let city = getAccentedCity(string);
replaceMap.forEach(pair => city = city.replaceAll(...pair));
return city;
}
CodePudding user response:
Just add more tests
const cleanString= string => {
let city = getAccentedCity(string);
if (city.toLowerCase().indexOf('ß') >= 0 ) return city.replaceAll('ß', 's');
if (city.indexOf(' ') < 0) return city
return city.replaceAll(' ', ' ');
}