Message from JavaScript discussions
July 2017
— Abandon ship
["Ultrahard",
"SUPERLONGWORDTHATSHOULDN'T WORK AT ALL BOOOOOMSHAKALAAKKAAA",
"SUPERLONGWORDTHATSHOULDN'T\nWORK AT\nALL\nBOOOOOMSHAKALAAKKAAA"],
["Multispace",
"This sentence tests if multiple spaces are at all preserved",
"This\nsentence\ntests if\nmultiple\nspaces are\nat all\npreserved"]
— These are my tests
— I added two just like that :D
— Awesome!
— looooooooooooooooooooooooooooong
and these words are tooooooooooooo looooooooooooooooooong
— My function is now a monster
—
function wordWrap(input, maxLen, wordSep = ' ', lineDelim = '\n') {
if (
typeof input !== 'string' ||
input.length < maxLen
) {
return input;
}
const lines = [];
const lastLine = input.split(wordSep).reduce((line, word) => {
if (word.length > 0) {
line.push(word);
}
if (line.join(wordSep).length > maxLen) {
const next = line.pop(word);
lines.push(line);
return [ next ];
}
return line;
}, []);
if (lastLine.length > 0) {
lines.push(lastLine);
}
return lines
.filter(line => line.length > 0)
.map(line => line.join(wordSep).trim())
.join(lineDelim);
}
— How many lines is that?
— ¯\_(ツ)_/¯
— 35, pretty good
— The smallest ones I saw were at least 80