Created
July 26, 2012 20:12
-
-
Save keenahn/3184241 to your computer and use it in GitHub Desktop.
String to Slug (coffeescript)
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: translate more funky characters