Last active
August 29, 2015 14:03
-
-
Save lgoldstien/e211e1771f3b47227f4b to your computer and use it in GitHub Desktop.
Javascript Text Methods: trimToWord
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
/** | |
* trimToWord | |
* | |
* Trim a string to one word less than @length characters | |
* @param {string} - The string to be trimmed | |
* @param {int} - The maximum length of the returned string | |
* @return {string} | |
*/ | |
trimToWord = function (string, len) { | |
if (string.length >= len) { | |
string = string.substring(0, len); | |
string = string.substring( | |
0, | |
Math.min( | |
string.length, | |
string.lastIndexOf(" ") | |
) | |
); | |
} | |
return string; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment