Skip to content

Instantly share code, notes, and snippets.

@Nick-Gabe
Last active June 21, 2022 13:44
Show Gist options
  • Save Nick-Gabe/95469bbc02768591b642abb8dc3ef673 to your computer and use it in GitHub Desktop.
Save Nick-Gabe/95469bbc02768591b642abb8dc3ef673 to your computer and use it in GitHub Desktop.
Code to make pages out of strings
function getPages(base, maxChars, splitTerm = '\n') {
const pages = []
// generates an array of items based in the string
const contentItems = base.split(splitTerm)
let content = ''
for (let i = 0; i < contentItems.length; i++) {
let part = contentItems[i] + splitTerm
// if content + part will be more than the max chars allowed,
// add the current content as a page and then empty it
if (content.length + part.length > maxChars
&& i + 1 !== contentItems.length) {
pages.push(content)
content = ''
}
content += part
if (i + 1 === contentItems.length) pages.push(content)
}
return pages
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment