Skip to content

Instantly share code, notes, and snippets.

@bitboxx
Created January 13, 2014 16:34
Show Gist options
  • Save bitboxx/8403343 to your computer and use it in GitHub Desktop.
Save bitboxx/8403343 to your computer and use it in GitHub Desktop.
Trims long text and add ellipsis
// trim long text
function trimText(text, maxlength, ellipsis) {
var output = '',
i = 0;
if (text.length >= maxlength) {
ellipsis = ellipsis || '…';
text = text.split(' ');
while (output.length + text[i].length < maxlength) {
output = output + (i === 0 ? '' : ' ') + text[i];
i++;
}
return output + ellipsis;
} else {
return text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment