Skip to content

Instantly share code, notes, and snippets.

@fervisa
Created July 2, 2013 22:33
Show Gist options
  • Select an option

  • Save fervisa/5913845 to your computer and use it in GitHub Desktop.

Select an option

Save fervisa/5913845 to your computer and use it in GitHub Desktop.
CoffeeScript truncate method for strings. Replaces the end of string with ellipsis (...)
String::truncate = (n) ->
@substr(0, n - 1) + ((if @length > n then "…" else ""))
@marksiemers
Copy link
Copy Markdown

If you don't want any whitespace between the substring and the ... add a call to trim:

String::truncate = (n) ->
  @substr(0, n - 1).trim() + ((if @length > n then "…" else ""))

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