Created
November 3, 2023 00:42
-
-
Save syehoonkim/ee96860039becef58473a7ff0d4c912f to your computer and use it in GitHub Desktop.
slugify
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
| 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