JSFiddle - React, Tailwind, and code Playground
by emilcieslar
JavaScript
const texter = (w, text) => {
const trimed = text.toString().trim().split(' ')
const words = trimed.length
const linedWords = []
for (let i = 0; i < words; i++) {
const currentWord = trimed[i]
if (currentWord.length < w) {
let concatedWords = '';
concatedWords += currentWord;
for (let j = i + 1; j < words; j++) {
const nextWord = trimed[j]
if (nextWord && (concatedWords.length + nextWord.length) <= w) {
concatedWords += ' ' + nextWord
i += 1
} else {
break;
}
}
linedWords.push(concatedWords)
continue;
}
linedWords.push(currentWord)
}
return linedWords.join('\n')
}
console.log(texter(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. "))