Created
March 10, 2014 16:19
-
-
Save weslleyaraujo/9468251 to your computer and use it in GitHub Desktop.
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
var truncate = function(string, n, terminator) { | |
if (typeof string !== 'string') { | |
return; | |
} | |
var lastIndex = string.lastIndexOf(' ', n), ret = string.toString(); | |
if(ret.length <= n) return ret; | |
if (lastIndex != -1) { | |
ret = string.substring(0, lastIndex) + (terminator || '...'); | |
} | |
return ret; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment