diff --git a/frontend/src/ts/test/english-punctuation.ts b/frontend/src/ts/test/english-punctuation.ts index f6bbdd02d..8c79176d3 100644 --- a/frontend/src/ts/test/english-punctuation.ts +++ b/frontend/src/ts/test/english-punctuation.ts @@ -1,21 +1,42 @@ -let pairsList: string[] = []; +import { randomElementFromArray } from "../utils/misc"; -export async function getList(): Promise { - if (pairsList.length === 0) { - return $.getJSON("languages/english_punctuation.json", function (data) { - pairsList = data; - return pairsList; - }); - } - return pairsList; -} +type Pair = [string, string[]]; + +const pairs: Pair[] = [ + ["are", ["aren't"]], + ["can", ["can't"]], + ["could", ["couldn't"]], + ["did", ["didn't"]], + ["does", ["doesn't"]], + ["do", ["don't"]], + ["had", ["hadn't"]], + ["has", ["hasn't"]], + ["have", ["haven't"]], + ["is", ["isn't"]], + ["it", ["it's", "it'll"]], + ["i", ["i'm", "i'll", "i've", "i'd"]], + ["you", ["you'll", "you're", "you've", "you'd"]], + ["that", ["that's", "that'll", "that'd"]], + ["must", ["mustn't", "must've"]], + ["there", ["there's", "there'll", "there'd"]], + ["he", ["he's", "he'll", "he'd"]], + ["she", ["she's", "she'll", "she'd"]], + ["we", ["we're", "we'll", "we'd"]], + ["they", ["they're", "they'll", "they'd"]], + ["should", ["shouldn't", "should've"]], + ["was", ["wasn't"]], + ["were", ["weren't"]], + ["will", ["won't"]], + ["would", ["wouldn't", "would've"]], + ["going", ["goin'"]], +]; // Check if word is in the group of pairs so it can be replaced export async function check(word: string): Promise { - const list = await getList(); if ( - list.find((a) => word.match(RegExp(`^([\\W]*${a[0]}[\\W]*)$`, "gi"))) === - undefined + pairs.find((pair) => + word.match(RegExp(`^([\\W]*${pair[0]}[\\W]*)$`, "gi")) + ) === undefined ) { return false; } @@ -23,14 +44,16 @@ export async function check(word: string): Promise { } export async function replace(word: string): Promise { - const list = await getList(); - const replacement = list.find((a) => - word.match(RegExp(`^([\\W]*${a[0]}[\\W]*)$`, "gi")) + const replacement = pairs.find((pair) => + word.match(RegExp(`^([\\W]*${pair[0]}[\\W]*)$`, "gi")) + ); + + if (replacement === undefined) return word; + + const randomReplacement = randomElementFromArray(replacement[1]); + + return word.replace( + RegExp(`^(?:([\\W]*)(${replacement[0]})([\\W]*))$`, "gi"), + randomReplacement ); - return replacement - ? word.replace( - RegExp(`^(?:([\\W]*)(${replacement[0]})([\\W]*))$`, "gi"), - replacement[1] - ) - : word; } diff --git a/frontend/static/languages/english_punctuation.json b/frontend/static/languages/english_punctuation.json deleted file mode 100644 index b523b3f44..000000000 --- a/frontend/static/languages/english_punctuation.json +++ /dev/null @@ -1,18 +0,0 @@ -[ - ["are", "aren't"], - ["can", "can't"], - ["could", "couldn't"], - ["did", "didn't"], - ["does", "doesn't"], - ["do", "don't"], - ["had", "hadn't"], - ["has", "hasn't"], - ["have", "haven't"], - ["is", "isn't"], - ["must", "mustn't"], - ["should", "shouldn't"], - ["was", "wasn't"], - ["were", "weren't"], - ["will", "won't"], - ["would", "wouldn't"] -]