Last active
February 28, 2024 05:37
-
-
Save marcelo-ribeiro/9ec35f336c470b73827b56a1f1cc389b to your computer and use it in GitHub Desktop.
useSlugify
This file contains hidden or 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
import { useSlugify } from "./useSlugify"; | |
const slugify = useSlugify(); | |
console.log(slugify("São Paulo")); |
This file contains hidden or 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 useSlugify = (text: string) => { | |
const accentsMap = new Map([ | |
["-", "\\s|\\.|/|_"], | |
["a", "á|à|ã|â|ä"], | |
["e", "é|è|ê|ë"], | |
["i", "í|ì|î|ï"], | |
["o", "ó|ò|ô|õ|ö"], | |
["u", "ú|ù|û|ü"], | |
["c", "ç"], | |
["n", "ñ"] | |
]); | |
const reducer = (text, [key]) => { | |
return text.replace(new RegExp(accentsMap.get(key), "gi"), key); | |
} | |
const slugify = (text) => { | |
return [...accentsMap].reduce(reducer, text.toLowerCase()); | |
} | |
return slugify; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment