Skip to content

Instantly share code, notes, and snippets.

@juristr
Created October 31, 2012 16:17
Show Gist options
  • Save juristr/3987994 to your computer and use it in GitHub Desktop.
Save juristr/3987994 to your computer and use it in GitHub Desktop.
Ellipsis function
function(text, maxLength){
if(typeof maxLength === 'undefined'){
maxLength = 9000; //a large number
}
if (text.length <= maxLength)
return text;
var xMaxFit = maxLength - 3;
var xTruncateAt = text.lastIndexOf(' ', xMaxFit);
if (xTruncateAt == -1 || xTruncateAt < maxLength / 2)
xTruncateAt = xMaxFit;
return text.substr(0, xTruncateAt) + "...";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment