From 90ec91e6d64aaacca6941bf123e9b8ba5303dcd2 Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 2 May 2023 18:18:32 +0800 Subject: [PATCH] feat: QuoteProcessor supports all cases --- src/common/__tests__/translate.test.ts | 69 ++++++++++++++++++++++++++ src/common/translate.ts | 37 ++++++++------ 2 files changed, 91 insertions(+), 15 deletions(-) diff --git a/src/common/__tests__/translate.test.ts b/src/common/__tests__/translate.test.ts index 49ec49ad00b..c1d20a32a30 100644 --- a/src/common/__tests__/translate.test.ts +++ b/src/common/__tests__/translate.test.ts @@ -32,6 +32,38 @@ describe('QuoteProcessor', () => { expect(targetText).toEqual('This is a test.') }) + it('should return the string without quote', () => { + const quoteProcessor = new QuoteProcessor() + const deltas = [ + ...quoteProcessor.quoteStart.split(''), + 'T', + 'h', + 'i', + 's', + ' ', + 'i', + 's', + ' ', + 'a', + ' ', + 't', + 'e', + 's', + 't', + '.', + '(', + ')' + quoteProcessor.quoteEnd.split('')[0], + ...quoteProcessor.quoteEnd.split('').slice(1), + ] + + let targetText = '' + for (const delta of deltas) { + targetText += quoteProcessor.processText(delta) + } + + expect(targetText).toEqual('This is a test.()') + }) + it('should return the string without quote', () => { const quoteProcessor = new QuoteProcessor() const text = 'This is a test.' @@ -81,4 +113,41 @@ describe('QuoteProcessor', () => { const targetText = quoteProcessor.processText(text) expect(targetText).toEqual(text) }) + + it('should return the same string if no quote exists', () => { + const quoteProcessor = new QuoteProcessor() + const text = `This is${quoteProcessor.quoteStart.slice(0, quoteProcessor.quoteStart.length - 1)} a test.` + const targetText = quoteProcessor.processText(text) + expect(targetText).toEqual(text) + }) + + it('do not remove the sub part of quote', () => { + const quoteProcessor = new QuoteProcessor() + const text = `This is${quoteProcessor.quoteStart.slice(0, quoteProcessor.quoteStart.length - 1)} a test.` + const targetText = quoteProcessor.processText(quoteProcessor.quoteStart + text + quoteProcessor.quoteEnd) + expect(targetText).toEqual(text) + }) + + it('do not remove the sub part of quote', () => { + const quoteProcessor = new QuoteProcessor() + const text = `This is${quoteProcessor.quoteEnd.slice(0, quoteProcessor.quoteEnd.length - 1)} a test.` + const targetText = quoteProcessor.processText(quoteProcessor.quoteStart + text + quoteProcessor.quoteEnd) + expect(targetText).toEqual(text) + }) + + it('do not remove the sub part of quote', () => { + const quoteProcessor = new QuoteProcessor() + const text = `This is${quoteProcessor.quoteStart.slice( + 0, + quoteProcessor.quoteStart.length - 1 + )} a${quoteProcessor.quoteStart.slice( + 0, + quoteProcessor.quoteStart.length - 2 + )} te${quoteProcessor.quoteEnd.slice(0, quoteProcessor.quoteEnd.length - 1)}st${quoteProcessor.quoteEnd.slice( + 0, + quoteProcessor.quoteEnd.length - 2 + )}.` + const targetText = quoteProcessor.processText(quoteProcessor.quoteStart + text + quoteProcessor.quoteEnd) + expect(targetText).toEqual(text) + }) }) diff --git a/src/common/translate.ts b/src/common/translate.ts index 0474e691e81..837785bb498 100644 --- a/src/common/translate.ts +++ b/src/common/translate.ts @@ -52,18 +52,25 @@ export class QuoteProcessor { private quote: string public quoteStart: string public quoteEnd: string - private quoteStartBuffer: string - private quoteEndBuffer: string + private prevQuoteStartBuffer: string + private prevQuoteEndBuffer: string constructor() { this.quote = uuidv4().replace(/-/g, '').slice(0, 4) this.quoteStart = `<${this.quote}>` this.quoteEnd = `` - this.quoteStartBuffer = '' - this.quoteEndBuffer = '' + this.prevQuoteStartBuffer = '' + this.prevQuoteEndBuffer = '' } - public processText(textDelta: string): string { + public processText(text: string): string { + const deltas = text.split('') + console.log(deltas) + const targetPieces = deltas.map((delta) => this.processTextDelta(delta)) + return targetPieces.join('') + } + + private processTextDelta(textDelta: string): string { if (textDelta === '') { return '' } @@ -72,7 +79,7 @@ export class QuoteProcessor { } let result = textDelta // process quote start - let quoteStartBuffer = this.quoteStartBuffer + let quoteStartBuffer = this.prevQuoteStartBuffer // console.debug('\n\n') // console.debug('---- process quote start -----') // console.debug('textDelta', textDelta) @@ -86,13 +93,13 @@ export class QuoteProcessor { // console.debug('quoteStartBuffer', quoteStartBuffer) // console.debug('result', result) if (char === this.quoteStart[quoteStartBuffer.length]) { - if (this.quoteStartBuffer.length > 0) { + if (this.prevQuoteStartBuffer.length > 0) { if (i === startIdx) { quoteStartBuffer += char result = textDelta.slice(i + 1) startIdx += 1 } else { - result = this.quoteStartBuffer + textDelta + result = this.prevQuoteStartBuffer + textDelta quoteStartBuffer = '' break } @@ -106,20 +113,20 @@ export class QuoteProcessor { break } if (quoteStartBuffer.length > 0) { - result = this.quoteStartBuffer + textDelta + result = this.prevQuoteStartBuffer + textDelta quoteStartBuffer = '' break } } } // console.debug('end loop!') - this.quoteStartBuffer = quoteStartBuffer + this.prevQuoteStartBuffer = quoteStartBuffer // console.debug('result', result) // console.debug('this.quoteStartBuffer', this.quoteStartBuffer) // console.debug('---- end of process quote start -----') textDelta = result // process quote end - let quoteEndBuffer = this.quoteEndBuffer + let quoteEndBuffer = this.prevQuoteEndBuffer // console.debug('\n\n') // console.debug('---- start process quote end -----') console.debug('textDelta', textDelta) @@ -133,13 +140,13 @@ export class QuoteProcessor { console.debug('quoteEndBuffer', quoteEndBuffer) console.debug('result', result) if (char === this.quoteEnd[quoteEndBuffer.length]) { - if (this.quoteEndBuffer.length > 0) { + if (this.prevQuoteEndBuffer.length > 0) { if (i === endIdx) { quoteEndBuffer += char result = textDelta.slice(i + 1) endIdx += 1 } else { - result = this.quoteEndBuffer + textDelta + result = this.prevQuoteEndBuffer + textDelta quoteEndBuffer = '' break } @@ -153,14 +160,14 @@ export class QuoteProcessor { break } if (quoteEndBuffer.length > 0) { - result = this.quoteEndBuffer + textDelta + result = this.prevQuoteEndBuffer + textDelta quoteEndBuffer = '' break } } } // console.debug('end loop!') - this.quoteEndBuffer = quoteEndBuffer + this.prevQuoteEndBuffer = quoteEndBuffer console.debug('totally result', result) // console.debug('this.quoteEndBuffer', this.quoteEndBuffer) // console.debug('---- end of process quote end -----')