Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save N0NameF0und/703a3a6d6e2ca921924fb7316a8f772d to your computer and use it in GitHub Desktop.
Save N0NameF0und/703a3a6d6e2ca921924fb7316a8f772d to your computer and use it in GitHub Desktop.
An Javascript function to remove accents, spaces and set lower case from an input string.
const slugify = str => {
const map = {
'-' : ' ',
'-' : '_',
'a' : 'á|à|ã|â|ä|À|Á|Ã|Â|Ä',
'e' : 'é|è|ê|ë|É|È|Ê|Ë',
'i' : 'í|ì|î|ï|Í|Ì|Î|Ï',
'o' : 'ó|ò|ô|õ|ö|Ó|Ò|Ô|Õ|Ö',
'u' : 'ú|ù|û|ü|Ú|Ù|Û|Ü',
'c' : 'ç|Ç',
'n' : 'ñ|Ñ'
};
for (var pattern in map) {
str = str.replace(new RegExp(map[pattern], 'g'), pattern);
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment