Created
November 27, 2018 03:59
-
-
Save galvez/338d046eaa0b966c6996de834a2999c6 to your computer and use it in GitHub Desktop.
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
function wrapAt (str, max) { | |
if (str.length > max) { | |
const parts = str.split(' ') | |
let i = 0 | |
for (let w = 0; i < parts.length; i++) { | |
w += parts[i].length | |
if (w >= max) { | |
parts[i] = `\n${parts[i]}` | |
w = 0 | |
} | |
} | |
return parts.join(' ') | |
} else { | |
return str | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment