Created
December 13, 2012 21:20
-
-
Save jordanthomas/4280005 to your computer and use it in GitHub Desktop.
"slugify" in coffee. Based on http://dense13.com/blog/2009/05/03/converting-string-to-slug-javascript/
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
String::slugify = -> | |
map = | |
'à':'a' | |
'á':'a' | |
'ä':'a' | |
'â':'a' | |
'è':'e' | |
'é':'e' | |
'ë':'e' | |
'ê':'e' | |
'ì':'i' | |
'í':'i' | |
'ï':'i' | |
'î':'i' | |
'ò':'o' | |
'ó':'o' | |
'ö':'o' | |
'ô':'o' | |
'ù':'u' | |
'ú':'u' | |
'ü':'u' | |
'û':'u' | |
'ñ':'n' | |
'ç':'c' | |
'·':'-' | |
'/':'-' | |
'_':'-' | |
',':'-' | |
':':'-' | |
';':'-' | |
'ƒ':'f' | |
'œ':'oe' | |
str = this.replace(/^\s+|\s+$/g, '').toLowerCase() | |
for character, replacement of map | |
str = str.replace(new RegExp(character, 'g'), replacement) | |
str = str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment