Created
April 2, 2014 18:07
-
-
Save schmidtsonian/9939704 to your computer and use it in GitHub Desktop.
Truncate text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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