Skip to content

Instantly share code, notes, and snippets.

@whalesalad
Created July 19, 2009 09:43
Show Gist options
  • Select an option

  • Save whalesalad/149866 to your computer and use it in GitHub Desktop.

Select an option

Save whalesalad/149866 to your computer and use it in GitHub Desktop.
String.prototype.shorten = function(n, i) {
var indicator = i || '…';
return (this.length > n) ? String(this.substr(0, n)+indicator) : String(this);
};
// Examples
var foo = 'Snuggleuffagus';
alert(foo.shorten(10)); // Snuggleuff…
alert(foo.shorten(3, '»')); // Snu»
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment