Message from JavaScript discussions

July 2017

— 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

— Wat

— How

— Hmm, lemme see though, yours can go down to like 20

— Pretty nice imo

— Just increased line count by just moving stuff

— I like having methods all lined up