Skip to content

Instantly share code, notes, and snippets.

@wall-e-08
Created April 9, 2020 17:09
Show Gist options
  • Save wall-e-08/eb5cd9e72dd0d5e199c55adf6405c7d6 to your computer and use it in GitHub Desktop.
Save wall-e-08/eb5cd9e72dd0d5e199c55adf6405c7d6 to your computer and use it in GitHub Desktop.
const _slugify = str => {
str = str.toLowerCase();
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.trim() // remove starting or ending whitespace before replacing whitespace
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment