Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
Last active November 4, 2021 23:05
Show Gist options
  • Save andersonbosa/d3693a3760c14e0564230d1535dd0adc to your computer and use it in GitHub Desktop.
Save andersonbosa/d3693a3760c14e0564230d1535dd0adc to your computer and use it in GitHub Desktop.
/**
* Removes the accent from a String
* @see {@link https://gist.github.com/andersonbosa/d3693a3760c14e0564230d1535dd0adc}
* @param {String} rawString
* @returns {String}
*/
function normalizeString (rawString) {
const replaceMap = {
'[ãâàáä]': 'a',
'[êèéë]': 'e',
'[îìíï]': 'i',
'[õôòóö]': 'o',
'[ûúùü]': 'u',
ç: 'c',
ñ: 'n'
}
return Object.keys(replaceMap)
.reduce(
(string, currentRegex) =>
string.replace(
RegExp(currentRegex, 'ig'),
replaceMap[currentRegex]
),
String(rawString)
)
.toLowerCase()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment