Created
May 13, 2013 10:12
-
-
Save PavelVanecek/5567350 to your computer and use it in GitHub Desktop.
A coffeescript function to trim sentence, while preserving full words
This file contains hidden or 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
###* | |
@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 + '…' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment