(
text: string,
)
| 10 | } |
| 11 | |
| 12 | public process( |
| 13 | text: string, |
| 14 | ): |
| 15 | | { text: string; endOfStream: boolean } |
| 16 | | { text: null; endOfStream: true } { |
| 17 | if (this.finished) { |
| 18 | return { |
| 19 | text: null, |
| 20 | endOfStream: true, |
| 21 | } |
| 22 | } |
| 23 | this.buffer += text |
| 24 | let longestOverlap = '' |
| 25 | |
| 26 | for (const stopSequence of this.stopSequences) { |
| 27 | const index = this.buffer.indexOf(stopSequence) |
| 28 | if (index !== -1) { |
| 29 | this.finished = true |
| 30 | return { |
| 31 | text: this.buffer.slice(0, index), |
| 32 | endOfStream: true, |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | for (const stopSequence of this.stopSequences) { |
| 38 | const overlap = suffixPrefixOverlap(this.buffer, stopSequence) |
| 39 | longestOverlap = |
| 40 | overlap.length > longestOverlap.length ? overlap : longestOverlap |
| 41 | } |
| 42 | |
| 43 | const index = this.buffer.length - longestOverlap.length |
| 44 | const processed = this.buffer.slice(0, index) |
| 45 | this.buffer = this.buffer.slice(index) |
| 46 | |
| 47 | return { |
| 48 | text: processed, |
| 49 | endOfStream: false, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public flush(): string { |
| 54 | if (this.finished) { |
no test coverage detected