Skip to content

Instantly share code, notes, and snippets.

@dtw
Created May 23, 2022 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtw/fb8ed3975ccf7fee2b9815e4e0b29afa to your computer and use it in GitHub Desktop.
Save dtw/fb8ed3975ccf7fee2b9815e4e0b29afa to your computer and use it in GitHub Desktop.
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 -
}
@dtw
Copy link
Author

dtw commented May 23, 2022

In my implementation I ended up with a trailing dash slugify('Jurassic Park III, 2001 - ★★★') so I added the final replace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment