Created
January 27, 2012 23:00
-
-
Save jhuston/1691425 to your computer and use it in GitHub Desktop.
truncate a word at a word break. optimized for naturally occurring word breaks. with optional length argument.
This file contains 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
truncateAtWord = function(str){ | |
var tail = "...", | |
len = arguments[1] || 10; | |
if(str.length <= len){ | |
return str; | |
} | |
if(str.charAt(len) === " "){ | |
return str.slice(0,len)+tail; | |
} | |
return str.slice(0,len).split(" ").slice(0,-1).join(" ")+tail; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment