Created
May 23, 2022 14:46
-
-
Save dtw/fb8ed3975ccf7fee2b9815e4e0b29afa to your computer and use it in GitHub Desktop.
I stole this https://gist.github.com/codeguy/6684588?permalink_comment_id=3243980#gistcomment-3243980
This file contains 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) { | |
// from https://gist.github.com/codeguy/6684588?permalink_comment_id=3243980#gistcomment-3243980 | |
return text | |
.toString() // Cast to string (optional) | |
.normalize('NFKD') // The normalize() using NFKD method returns the Unicode Normalization Form of a given string. | |
.toLowerCase() // Convert the string to lowercase letters | |
.trim() // Remove whitespace from both sides of a string (optional) | |
.replace(/\s+/g, '-') // Replace spaces with - | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\-\-+/g, '-') // Replace multiple - with single - | |
.replace(/\-$/g, ''); // Remove trailing - | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In my implementation I ended up with a trailing dash
slugify('Jurassic Park III, 2001 - ★★★')
so I added the final replace.