Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jgoslow
Last active September 29, 2018 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgoslow/f6d008f9a3b6b1a9ebb1b9eec49fdcc8 to your computer and use it in GitHub Desktop.
Save jgoslow/f6d008f9a3b6b1a9ebb1b9eec49fdcc8 to your computer and use it in GitHub Desktop.
Limits the length of a string but ensures you end on a word
// javascript word cut
function cut(n) {
return function textCutter(i, text) {
var short = text.substr(0, n);
if (/^\S/.test(text.substr(n)))
return short.replace(/\s+\S*$/, "");
return short;
};
}
// credit: https://stackoverflow.com/questions/10751102/cut-a-string-after-n-characters-but-if-its-in-the-middle-of-a-word-cut-the-who
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment