Skip to content

Instantly share code, notes, and snippets.

@brendomaciel
Created October 11, 2016 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendomaciel/c7324f4e12a0eafc6ca3656c70a4552d to your computer and use it in GitHub Desktop.
Save brendomaciel/c7324f4e12a0eafc6ca3656c70a4552d to your computer and use it in GitHub Desktop.
Function to limit a string.
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