Skip to content

Instantly share code, notes, and snippets.

@lgoldstien
Last active August 29, 2015 14:03
Show Gist options
  • Save lgoldstien/e211e1771f3b47227f4b to your computer and use it in GitHub Desktop.
Save lgoldstien/e211e1771f3b47227f4b to your computer and use it in GitHub Desktop.
Javascript Text Methods: trimToWord
/**
* 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