Skip to content

Instantly share code, notes, and snippets.

@Feawel
Last active March 19, 2017 12:02
Show Gist options
  • Save Feawel/e8ca0858ae5677a7ad316414fda9431b to your computer and use it in GitHub Desktop.
Save Feawel/e8ca0858ae5677a7ad316414fda9431b to your computer and use it in GitHub Desktop.
Divide a text in parts, to be handle by Polly API
const someReallyUsefullFunction = (text) => {
...
// Break in parts small enough to be handle by Polly API
const parts = divideTextEnoughToBeHandleByPolly(text)
...
}
const divideTextEnoughToBeHandleByPolly = (text, charSeparator = '<br/>', maxLenght = 1000) => {
return text.split(charSeparator).reduce((memo, p) => {
if(memo[memo.length - 1].length < maxLenght) {
memo[memo.length - 1] += charSeparator + p
return memo
}
else {
memo[memo.length] = p
return memo
}
}, [[]])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment