Skip to content

Commit

Permalink
fix(text-chunk): Fix infinite loop checking
Browse files Browse the repository at this point in the history
  • Loading branch information
eheikes committed Apr 28, 2024
1 parent 12428cd commit cc1e2b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/tts-cli/lib/text-chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const splitIntoSentences = (text) => {
*/
const chunkText = (text, maxLength) => {
const sentences = splitIntoSentences(text)
const epsilon = 100

// Loop through the sentences, putting them into chunks.
const chunks = []
Expand Down Expand Up @@ -45,7 +46,7 @@ const chunkText = (text, maxLength) => {
j++
}
safety3++
if (safety3 > 100) { throw new Error('Infinite loop') }
if (safety3 > words.length + epsilon) { throw new Error('Infinite loop') }
}

// If there is an unfilled chunk remaining, add it to the list.
Expand All @@ -64,12 +65,12 @@ const chunkText = (text, maxLength) => {
chunk += newChunk
i++
safety2++
if (safety2 > 100) { throw new Error('Infinite loop') }
if (safety2 > sentences.length + epsilon) { throw new Error('Infinite loop') }
}
chunks.push(chunk)
}
safety++
if (safety > 100) { throw new Error('Infinite loop') }
if (safety > sentences.length + epsilon) { throw new Error('Infinite loop') }
}

return chunks
Expand Down
7 changes: 7 additions & 0 deletions packages/tts-cli/test/text-chunk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ describe('chunkText()', () => {
'people, for the people, shall not perish from the earth.'
])
})

it('should work for long texts', () => {
const text = new Array(200).fill(0).map(_ => 'This is a sentence.').join(' ')
expect(() => {
chunkText(text, 20)
}).not.toThrow()
})
})

describe('splitIntoSentences()', () => {
Expand Down

0 comments on commit cc1e2b2

Please sign in to comment.