Skip to content

Commit

Permalink
feat: QuoteProcessor supports all cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed May 2, 2023
1 parent b65df78 commit 90ec91e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 15 deletions.
69 changes: 69 additions & 0 deletions src/common/__tests__/translate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down Expand Up @@ -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)
})
})
37 changes: 22 additions & 15 deletions src/common/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.quote}>`
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 ''
}
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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 -----')
Expand Down

0 comments on commit 90ec91e

Please sign in to comment.