Created
January 15, 2016 04:33
-
-
Save namnm/4385f44bb58906ea2995 to your computer and use it in GitHub Desktop.
js truncate method for string without cutting word
This file contains 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) { | |
if (this.length <= n) { | |
return this; | |
} | |
var truncated = this.substr(0, n); | |
if (this.charAt(n) === ' ') { | |
return truncated; | |
} | |
return truncated.substr(0, truncated.lastIndexOf(' ')); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment