slugify.ls 622 B

12345678910111213141516171819
  1. # source: https://stackoverflow.com/a/5782563
  2. export slugify = (string) ->
  3. string = string.replace(/^\s+|\s+$/g, '')
  4. string = string.to-lower-case!
  5. accents = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;"
  6. accent-safe = "aaaaaeeeeeiiiiooooouuuunc------"
  7. for i from 0 to accents.length by 1
  8. string = string.replace(new RegExp(accents.char-at(i), 'g'), accent-safe.char-at(i))
  9. # remove invalid chars
  10. string = string.replace(/[^a-z0-9 -]/g, '')
  11. # collapse whitespace and replace by -
  12. string = string.replace(/\s+/g, '-')
  13. # collapse dashes
  14. string = string.replace(/-+/g, '-');