Skip to content

Instantly share code, notes, and snippets.

@marcelo-ribeiro
Last active February 28, 2024 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelo-ribeiro/9ec35f336c470b73827b56a1f1cc389b to your computer and use it in GitHub Desktop.
Save marcelo-ribeiro/9ec35f336c470b73827b56a1f1cc389b to your computer and use it in GitHub Desktop.
useSlugify
import { useSlugify } from "./useSlugify";
const slugify = useSlugify();
console.log(slugify("São Paulo"));
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