Skip to content

Instantly share code, notes, and snippets.

@weslleyaraujo
Created March 10, 2014 16:19
Show Gist options
  • Select an option

  • Save weslleyaraujo/9468251 to your computer and use it in GitHub Desktop.

Select an option

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