Skip to content

Instantly share code, notes, and snippets.

@lstebner
Created September 4, 2012 01:03
Show Gist options
  • Save lstebner/3615433 to your computer and use it in GitHub Desktop.
Save lstebner/3615433 to your computer and use it in GitHub Desktop.
Underscore Mixin to create Slugs
_.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;
}
});
@lstebner
Copy link
Author

lstebner commented Sep 4, 2012

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.

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