Skip to content

Instantly share code, notes, and snippets.

@appden
Created May 20, 2009 19:59
Show Gist options
  • Save appden/115053 to your computer and use it in GitHub Desktop.
Save appden/115053 to your computer and use it in GitHub Desktop.
String.implement({
truncate: function(max, trail, separator){
var s = this.trim();
if (s.length <= max) return s;
separator = $pick(separator, ' ');
if (separator)
s = s.substr(0, max + 1).substr(0, s.lastIndexOf(separator));
else
s = s.substr(0, max);
return s + $pick(trail, '...');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment