Skip to content

Instantly share code, notes, and snippets.

@PavelVanecek
Created May 13, 2013 10:12
Show Gist options
  • Save PavelVanecek/5567350 to your computer and use it in GitHub Desktop.
Save PavelVanecek/5567350 to your computer and use it in GitHub Desktop.
A coffeescript function to trim sentence, while preserving full words
###*
@param {String} input
@param {number=} length
###
trimWithWordsPreserve = (input, length = 15) ->
if input.length <= length
input
else
regexp = new RegExp '^(.{' + length + '}[^\\s]*).*'
shorter = input.replace regexp, '$1'
shorter + '&hellip;'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment