Last active
September 29, 2018 22:42
-
-
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
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
// 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