Created
October 11, 2016 20:47
-
-
Save brendomaciel/c7324f4e12a0eafc6ca3656c70a4552d to your computer and use it in GitHub Desktop.
Function to limit a string.
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.limit = function(size, cutWord, suspensionPoints) { | |
var str = this; | |
cutWord = typeof cutWord == "undefined" ? true : cutWord; | |
if (typeof size == "number" && size > 0 && str.length >= size) { | |
str = str.substr(0, size); | |
if (!cutWord) { | |
str = str.substr(0, Math.min(str.length, str.lastIndexOf(" "))); | |
} | |
str += !suspensionPoints ? '...' : suspensionPoints; | |
} | |
return str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment