Skip to content

Instantly share code, notes, and snippets.

@galvez
Created November 27, 2018 03:59
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 galvez/338d046eaa0b966c6996de834a2999c6 to your computer and use it in GitHub Desktop.
Save galvez/338d046eaa0b966c6996de834a2999c6 to your computer and use it in GitHub Desktop.
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