JSFiddle - React, Tailwind, and code Playground
by emilcieslar
JavaScript
const split = (w, string) => {
const words = string.split(' ')
const splittedString = [[]]
let symbolsNumber = 0
for (let i = 0; i < words.length; i += 1) {
if (words[i].length + symbolsNumber < w) {
splittedString[splittedString.length - 1].push(words[i])
symbolsNumber += words[i].length
continue
}
splittedString.push([words[i]])
symbolsNumber = 0
}
return splittedString
}
console.log(split(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."));