Forked from fabiofdsantos/angularJS_removeAccents.js
Last active
August 13, 2024 19:35
-
-
Save marcelo-ribeiro/abd651b889e4a20e0bab558a05d38d77 to your computer and use it in GitHub Desktop.
An Javascript function to remove accents and others characters from an input string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example: https://codepen.io/marcelo-ribeiro/pen/OJmVOyW | |
const accentsMap = new Map([ | |
["A", "Á|À|Ã|Â|Ä"], | |
["a", "á|à|ã|â|ä"], | |
["E", "É|È|Ê|Ë"], | |
["e", "é|è|ê|ë"], | |
["I", "Í|Ì|Î|Ï"], | |
["i", "í|ì|î|ï"], | |
["O", "Ó|Ò|Ô|Õ|Ö"], | |
["o", "ó|ò|ô|õ|ö"], | |
["U", "Ú|Ù|Û|Ü"], | |
["u", "ú|ù|û|ü"], | |
["C", "Ç"], | |
["c", "ç"], | |
["N", "Ñ"], | |
["n", "ñ"] | |
]); | |
const reducer = (acc, [key]) => acc.replace(new RegExp(accentsMap.get(key), "g"), key); | |
export const removeAccents = (text) => [...accentsMap].reduce(reducer, text); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const slugify = text => text | |
.replace(/\s|_|\(|\)/g, "-") | |
.normalize("NFD").replace(/\p{Diacritic}/gu, "") | |
.toLowerCase() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example: https://codepen.io/marcelo-ribeiro/pen/PomqOvE | |
const accentsMap = new Map([ | |
["-", "\\s|\\.|_"], | |
["a", "á|à|ã|â|ä"], | |
["e", "é|è|ê|ë"], | |
["i", "í|ì|î|ï"], | |
["o", "ó|ò|ô|õ|ö"], | |
["u", "ú|ù|û|ü"], | |
["c", "ç"], | |
["n", "ñ"] | |
]); | |
const reducer = (acc, [key]) => acc.replace(new RegExp(accentsMap.get(key), "gi"), key); | |
export const slugify = (text) => [...accentsMap].reduce(reducer, text.toLowerCase()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const removeAccents = text => text.normalize("NFD").replace(/\p{Diacritic}/gu, "") |
Thanks @marcelo,
I found this interesting topic on the stackoverflow using String.prototype.normalize()
const accentsMap = { a: 'á|à|ã|â|À|Á|Ã|Â', e: 'é|è|ê|É|È|Ê', i: 'í|ì|î|Í|Ì|Î', o: 'ó|ò|ô|õ|Ó|Ò|Ô|Õ', u: 'ú|ù|û|ü|Ú|Ù|Û|Ü', c: 'ç|Ç', n: 'ñ|Ñ', }; export const slugify = text => Object.keys(accentsMap).reduce((acc, cur) => acc.replace(new RegExp(accentsMap[cur], 'g'), cur), text);
thanks! and thanks to the author @marcelo-ribeiro
New ES6 version! 😎
Thanks a lot for this snippet.
I improved for ES6 use, in case anyone might want it:const accentsMap = { a: 'á|à|ã|â|À|Á|Ã|Â', e: 'é|è|ê|É|È|Ê', i: 'í|ì|î|Í|Ì|Î', o: 'ó|ò|ô|õ|Ó|Ò|Ô|Õ', u: 'ú|ù|û|ü|Ú|Ù|Û|Ü', c: 'ç|Ç', n: 'ñ|Ñ', }; export const slugify = text => Object.keys(accentsMap).reduce((acc, cur) => acc.replace(new RegExp(accentsMap[cur], 'g'), cur), text);
Thank you for sharing! I used part of the your code to improve the ES6 version.
I hope you enjoy 😃
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent work Marcelo, thanks you for sharing.
It didn't replace whitespace well, so I mixed the two lines together and it works like a charm.
``
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;
}
``