Created
January 13, 2014 16:34
-
-
Save bitboxx/8403343 to your computer and use it in GitHub Desktop.
Trims long text and add ellipsis
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
// 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