Created
September 4, 2012 01:03
-
-
Save lstebner/3615433 to your computer and use it in GitHub Desktop.
Underscore Mixin to create Slugs
This file contains hidden or 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
_.mixin({ | |
slugify: function(title){ | |
var replace = '-'; | |
var str = title.toString() | |
.replace(/[\s\.]+/g,replace) | |
.toLowerCase() | |
.replace(new RegExp('[^a-z0-9'+replace+']','g'), replace) | |
.replace(new RegExp(replace+'+','g'),replace) | |
; | |
if( str.charAt(str.length-1) == replace ) str = str.substring(0,str.length-1); | |
if ( str.charAt(0) == replace ) str = str.substring(1); | |
return str; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I didn't write this originally, but I've been using it so long I forget where it came from. All I did was turn it into a mixin, but sorry to whomever wrote this I can't give proper credit.