JSFiddle - React, Tailwind, and code Playground
by emilcieslar
JavaScript
const lineSplitter = (w, textLines) => {
let result = [];
const splittedLines = textLines.split('\n');
splittedLines.forEach(line => {
const splittedWords = line.split(' ');
result = [...result, ...splittedWords];
});
debugger
result.forEach((word, index) => {
if (result[index + 1] && (word.length + result[index + 1].length) > w) {
result[index] = result[index] += '\n';
} else {
result[index] = result[index] += ' ';
}
});
return result.join('');
};
/* console.log(lineSplitter(25, `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.`)); */
console.log(lineSplitter(10, `dkdakfadf jadjagj jdafj
adggdg lacfdalf
dgagdagdg
dfdj ajdgja`));