Created
March 10, 2023 18:02
-
-
Save mrfzd/6f2396f9204aae8250c9eaa184fcfa38 to your computer and use it in GitHub Desktop.
Generate Safe Slug strig for different purposes
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 generateSafeSlugString = (s) => s?.toString().normalize('NFD').replace(/[\u0300-\u036f]/g, "") //remove diacritics | |
.toLowerCase() | |
.replace(/\s+/g, '-') //spaces to dashes | |
.replace(/&/g, '-and-') //ampersand to and | |
.replace(/[^\w\-]+/g, '') //remove non-words | |
.replace(/\-\-+/g, '-') //collapse multiple dashes | |
.replace(/^-+/, '') //trim starting dash | |
.replace(/-+$/, ''); //trim ending dash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment