-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paired punctuation checking initial implementation.
- Loading branch information
Ben King
committed
Dec 5, 2024
1 parent
d6a7223
commit ef0d0dc
Showing
17 changed files
with
2,220 additions
and
369 deletions.
There are no files selected for viewing
439 changes: 439 additions & 0 deletions
439
packages/punctuation-checker/src/paired-punctuation/paired-punctuation-checker.ts
Large diffs are not rendered by default.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
packages/punctuation-checker/src/paired-punctuation/paired-punctuation-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { PairedPunctuationRule } from '../rule-set/rule-utils'; | ||
import { CharacterClassRegexBuilder } from '../utils'; | ||
|
||
export class PairedPunctuationConfig { | ||
private readonly standardRules: PairedPunctuationRule[] = []; | ||
private readonly quotationRules: PairedPunctuationRule[] = []; | ||
private openingMarkRegex = /[([{]/; | ||
private closingMarkRegex = /[)}\]]/; | ||
private correspondingMarkMap: Map<string, string> = new Map<string, string>(); | ||
private quotationMarks: Set<string> = new Set<string>(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
private constructor() {} | ||
|
||
private intializeAfterBuilding(): void { | ||
this.openingMarkRegex = this.createOpeningMarkRegex(); | ||
this.closingMarkRegex = this.createClosingMarkRegex(); | ||
this.correspondingMarkMap = this.createCorrespondingMarkMap(); | ||
this.quotationMarks = this.createQuotationMarkSet(); | ||
} | ||
|
||
private createOpeningMarkRegex(): RegExp { | ||
const quoteRegexBuilder: CharacterClassRegexBuilder = new CharacterClassRegexBuilder(); | ||
for (const punctuationPair of this.standardRules) { | ||
quoteRegexBuilder.addCharacter(punctuationPair.openingPunctuationMark); | ||
} | ||
for (const punctuationPair of this.quotationRules) { | ||
quoteRegexBuilder.addCharacter(punctuationPair.openingPunctuationMark); | ||
} | ||
return quoteRegexBuilder.build(); | ||
} | ||
|
||
private createClosingMarkRegex(): RegExp { | ||
const quoteRegexBuilder: CharacterClassRegexBuilder = new CharacterClassRegexBuilder(); | ||
for (const punctuationPair of this.standardRules) { | ||
quoteRegexBuilder.addCharacter(punctuationPair.closingPunctuationMark); | ||
} | ||
for (const punctuationPair of this.quotationRules) { | ||
quoteRegexBuilder.addCharacter(punctuationPair.closingPunctuationMark); | ||
} | ||
return quoteRegexBuilder.build(); | ||
} | ||
|
||
private createCorrespondingMarkMap(): Map<string, string> { | ||
const correspondingMarkMap: Map<string, string> = new Map<string, string>(); | ||
for (const rule of this.standardRules) { | ||
correspondingMarkMap.set(rule.openingPunctuationMark, rule.closingPunctuationMark); | ||
correspondingMarkMap.set(rule.closingPunctuationMark, rule.openingPunctuationMark); | ||
} | ||
for (const rule of this.quotationRules) { | ||
correspondingMarkMap.set(rule.openingPunctuationMark, rule.closingPunctuationMark); | ||
correspondingMarkMap.set(rule.closingPunctuationMark, rule.openingPunctuationMark); | ||
} | ||
return correspondingMarkMap; | ||
} | ||
|
||
private createQuotationMarkSet(): Set<string> { | ||
const quotationMarkSet: Set<string> = new Set<string>(); | ||
for (const rule of this.quotationRules) { | ||
quotationMarkSet.add(rule.openingPunctuationMark); | ||
quotationMarkSet.add(rule.closingPunctuationMark); | ||
} | ||
return quotationMarkSet; | ||
} | ||
|
||
public createAllPairedMarksRegex(): RegExp { | ||
const quoteRegexBuilder: CharacterClassRegexBuilder = new CharacterClassRegexBuilder(); | ||
for (const punctuationPair of this.standardRules) { | ||
quoteRegexBuilder | ||
.addCharacter(punctuationPair.openingPunctuationMark) | ||
.addCharacter(punctuationPair.closingPunctuationMark); | ||
} | ||
for (const punctuationPair of this.quotationRules) { | ||
quoteRegexBuilder | ||
.addCharacter(punctuationPair.openingPunctuationMark) | ||
.addCharacter(punctuationPair.closingPunctuationMark); | ||
} | ||
return quoteRegexBuilder.makeGlobal().build(); | ||
} | ||
|
||
public isOpeningMark(punctuationMark: string): boolean { | ||
return this.openingMarkRegex.test(punctuationMark); | ||
} | ||
|
||
public isClosingMark(punctuationMark: string): boolean { | ||
return this.closingMarkRegex.test(punctuationMark); | ||
} | ||
|
||
public doMarksConstituteAPair(openingMark: string, closingMark: string): boolean { | ||
return this.correspondingMarkMap.get(openingMark) === closingMark; | ||
} | ||
|
||
public findCorrespondingMark(punctuationMark: string): string | undefined { | ||
if (this.correspondingMarkMap.has(punctuationMark)) { | ||
return this.correspondingMarkMap.get(punctuationMark); | ||
} | ||
return undefined; | ||
} | ||
|
||
public shouldErrorForUnmatchedMarks(punctuationMark: string): boolean { | ||
return !this.quotationMarks.has(punctuationMark); | ||
} | ||
|
||
public static Builder = class { | ||
pairedPunctuationConfig: PairedPunctuationConfig = new PairedPunctuationConfig(); | ||
|
||
public addRule(pairedPunctuationRule: PairedPunctuationRule): this { | ||
this.pairedPunctuationConfig.standardRules.push(pairedPunctuationRule); | ||
return this; | ||
} | ||
|
||
public addQuotationRule(pairedPunctuationRule: PairedPunctuationRule): this { | ||
this.pairedPunctuationConfig.quotationRules.push(pairedPunctuationRule); | ||
return this; | ||
} | ||
|
||
public build(): PairedPunctuationConfig { | ||
this.pairedPunctuationConfig.intializeAfterBuilding(); | ||
return this.pairedPunctuationConfig; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.