Skip to content

Instantly share code, notes, and snippets.

@syehoonkim
Created November 3, 2023 00:42
Show Gist options
  • Select an option

  • Save syehoonkim/ee96860039becef58473a7ff0d4c912f to your computer and use it in GitHub Desktop.

Select an option

Save syehoonkim/ee96860039becef58473a7ff0d4c912f to your computer and use it in GitHub Desktop.
slugify
const slugify = (text) => {
const from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;";
const to = "aaaaaeeeeeiiiiooooouuuunc------";
const newText = text
.split("")
.map((letter, i) =>
letter.replace(new RegExp(from.charAt(i), "g"), to.charAt(i))
);
return newText
.toString() // Cast to string
.toLowerCase() // Convert the string to lowercase letters
.trim() // Remove whitespace from both sides of a string
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/&/g, "-y-") // Replace & with 'and'
.replace(/[^\w\-]+/g, "") // Remove all non-word chars
.replace(/\-\-+/g, "-"); // Replace multiple - with single -
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment