Last active
August 4, 2021 14:23
-
-
Save eek/9c4887e80b3ede05c0e39fee4dce3747 to your computer and use it in GitHub Desktop.
Vanilla JavaScript Slugify + Accent removal - Just another JavaScript Slugifier with an extra line for Accent Removal
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
| function slugify(text) { | |
| return text.toString().toLowerCase().trim() | |
| .normalize('NFD') // separate accent from letter | |
| .replace(/[\u0300-\u036f]/g, '') // remove all separated accents | |
| .replace(/\s+/g, '-') // replace spaces with - | |
| .replace(/&/g, '-and-') // replace & with 'and' | |
| .replace(/[^\w\-]+/g, '') // remove all non-word chars | |
| .replace(/--+/g, '-') // replace multiple '-' with single '-' | |
| } |
Author
Finally it made click and I believe to understand, what NFD does! Thank you very much for your efforts, @eek ! :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NFD takes care of all diacritics. So anything that's above or below the character.
It doesn't modify the actual component of the word. So
Äpfelhas the diacritic removed and becomesApfel.TürkçebecomesTurkce. It doesn't changeätoaeit just removes the diacritics that are above and below the character. I've used it mainly for French and Romanian to generate URLs from Titles. Places where the written word without diacritics is exactly the same:mămăligăis writtenmamaliga.So yeah
ßdoesn't get converted to anything because it doesn't have any upper or lower accents.