Message from JavaScript discussions

July 2017

— Has to keep words intact

— 

function wordWrap(input, maxLen) {
const words = input.split(' ');
const lines = [];
let line = [];
words.forEach(word =>
while(line.join(' ').length <= maxLen) {
line.push(word);
}
lines.push(line);
line = [ line.shift() ];
});
return lines.map(line => line.join(' ')).join('\n');
}

— Something like that?

— I guess you could get rid of the .shift by using a do..while instead

— Why avoid regex though

— Because this:
.{1,' + maxLen + '}(\\s|$)|\\S+?(\\s|$)
is WAY less readable than the above

— I disagree

— Doesn't str.match return the original string in index 0?

— No, all

— Oh wait.. it might behave differently for g flag

— I don't know, this just seems better for me

— Yessir