Created
September 29, 2017 07:40
-
-
Save andreasvirkus/bb7f2aa45cd844e5e4b7a66b979293d1 to your computer and use it in GitHub Desktop.
Truncate strings
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 = String.prototype.trunc || function(n) { | |
return (this.length > n) ? this.substr(0, n-1) + '…' : this; | |
}; |
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
// Usage | |
let s = 'not very long'; | |
s.trunc(25); //=> not very long | |
s.trunc(5); //=> not ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment