JSFiddle - React, Tailwind, and code Playground
by emilcieslar
JavaScript
const test = `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.`
const align = (text, W) => {
if (text.length < W) {
return lines
}
const lines = text.split(/\s+/)
const rezult = []
for (let i = 0, str = ''; i < lines.length; i++) {
if (i == lines.length - 1) {
rezult.push(str + ' ' + lines[i])
break
}
if (str.length + lines[i].length > W) {
rezult.push(str + ' ' + lines[i])
str = ''
} else {
str = str + ' ' + lines[i]
}
}
return rezult.join('\n')
}
console.log(align(test, 25))