Created
October 31, 2012 16:17
-
-
Save juristr/3987994 to your computer and use it in GitHub Desktop.
Ellipsis function
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
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