Skip to content

Instantly share code, notes, and snippets.

@schmidtsonian
Created April 2, 2014 18:07
Show Gist options
  • Save schmidtsonian/9939704 to your computer and use it in GitHub Desktop.
Save schmidtsonian/9939704 to your computer and use it in GitHub Desktop.
Truncate text
String.prototype.trunc =
function( n, useWordBoundary ){
var toLong = this.length > n,
s_ = toLong ? this.substr(0,n-1) : this;
s_ = useWordBoundary && toLong ? s_.substr( 0, s_.lastIndexOf( ' ' ) ) : s_;
return toLong ? s_ + ' ...' : s_;
};
var s = "this is a text"
s.trunc( "6", true ); // "this ..."
s.trunc( "6" ); //"this i ..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment