Skip to content

Instantly share code, notes, and snippets.

@jordanthomas
Created December 13, 2012 21:20
Show Gist options
  • Save jordanthomas/4280005 to your computer and use it in GitHub Desktop.
Save jordanthomas/4280005 to your computer and use it in GitHub Desktop.
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