Skip to content

Instantly share code, notes, and snippets.

@hallojoe
Created August 16, 2020 09:03
Show Gist options
  • Save hallojoe/41b38b7db11319d2f0922009677b0740 to your computer and use it in GitHub Desktop.
Save hallojoe/41b38b7db11319d2f0922009677b0740 to your computer and use it in GitHub Desktop.
const slug = s => s
.toString() // ensure string
.normalize("NFD") // convert to unicode normalization form using (N)normalization (F)form canonical (D)decomposition
// aka. remove accent and leave base
.replace(/[\u0300-\u036f]/g, "") // remove range combining grave accent to combining lating small letter x
.toLowerCase() // lower chars
.trim() // trim whitespace
.replace(/\s+/g, '-') // replace space(s) with dash
.replace(/[^\w-]+/g, "") // remove things not a-z A-Z 0-9 or -
.replace(/--+/g, '-') // replace double dash with single dash
// 90% SO copy/paste
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment