Skip to content

Instantly share code, notes, and snippets.

@keenahn
Created July 26, 2012 20:12
Show Gist options
  • Save keenahn/3184241 to your computer and use it in GitHub Desktop.
Save keenahn/3184241 to your computer and use it in GitHub Desktop.
String to Slug (coffeescript)
to_slug = (str) ->
str = str.replace(/^\s+|\s+$/g, "").toLowerCase() # trim and force lowercase
from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;"
to = "aaaaeeeeiiiioooouuuunc------"
for i in [i..from.length]
str = str.replace(new RegExp(from.charAt(i), "g"), to.charAt(i))
# remove accents, swap ñ for n, etc
str = str.replace(/[^a-z0-9 -]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-")
# remove invalid chars, collapse whitespace and replace by -, collapse dashes
return str # unnecessary line, but for clarity
@keenahn
Copy link
Author

keenahn commented Jul 26, 2012

TODO: translate more funky characters

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