Search code examples
javascriptstringfunctionreplaceindexof

Clean string javascript function


This my actual scenario

const cleanString=( string )=>{
  let city = getAccentedCity(string);
  if( city.indexOf('%20')<0 ) return city;
  return city.replaceAll('%20', ' ');
  
}

I have now to add an other case when a city contains the string "%c3%9f" and i want to replace it with 's'

How can i add it in my current function?


Solution

  • Just add more tests if you do not want to rewrite the code

    const cleanString= string => {
      let city = getAccentedCity(string);
      if (city.toLowerCase().indexOf('%c3%9f') >= 0 ) return city.replaceAll('%c3%9f', 's');
      if (city.indexOf('%20') < 0) return city;
      return city.replaceAll('%20', ' ');
    }