JSFiddle - React, Tailwind, and code Playground
by emilcieslar
JavaScript
function treatString(str, len) {
let input = str.trim().split(' ');
let [index, output] = [0, []]
let result = "";
output[index] = '';
input.forEach(word => {
let temp = `${output[index]} ${word}`.trim()
if (temp.length <= len) {
output[index] = temp;
} else {
index++;
output[index] = word;
}
})
output.forEach(sentence => {
sentenceWithoutDots = sentence.split('.').join("");
if (sentenceWithoutDots.slice(-1) === ",") {
sentenceWithoutDots = sentenceWithoutDots.slice(0, -1);
}
if (sentenceWithoutDots.length > 0) {
result += sentenceWithoutDots + ".<br>";
}
})
return result;
}
console.log(treatString("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.", 25));