Last active
March 30, 2018 12:44
-
-
Save spences10/27ee6e716ba31de495987bec356a2aae to your computer and use it in GitHub Desktop.
Slug helper!
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
export const slugIt = text => { | |
if (!text) return | |
return text | |
.toString() | |
.toLowerCase() | |
.replace(/\s+/g, '-') // Replace spaces with - | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\-\-+/g, '-') // Replace multiple - with single - | |
.replace(/^-+/, '') // Trim - from start of text | |
.replace(/-+$/, '') // Trim - from end of text | |
} | |
console.log(slugIt('!" NO CAPS or funky symbols !£%£$%^@: hello£$%^ world!')); | |
// no-caps-or-funky-symbols-hello-world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment