Skip to content

Instantly share code, notes, and snippets.

@weslleyaraujo
Created March 10, 2014 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weslleyaraujo/9468251 to your computer and use it in GitHub Desktop.
Save weslleyaraujo/9468251 to your computer and use it in GitHub Desktop.
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