diff --git a/frontend/src/ts/test/british-english.ts b/frontend/src/ts/test/british-english.ts index 2bc64285f..77f40fb6b 100644 --- a/frontend/src/ts/test/british-english.ts +++ b/frontend/src/ts/test/british-english.ts @@ -1,8 +1,16 @@ +import Config from "../config"; import { capitalizeFirstLetterOfEachWord } from "../utils/misc"; +import * as CustomText from "../test/custom-text"; -let list: string[] = []; +interface BritishEnglishReplacement { + 0: string; + 1: string; + 2?: string[]; +} -export async function getList(): Promise { +let list: BritishEnglishReplacement[] = []; + +export async function getList(): Promise { if (list.length === 0) { return $.getJSON("languages/britishenglish.json", function (data) { list = data; @@ -13,31 +21,50 @@ export async function getList(): Promise { } } -export async function replace(word: string): Promise { +export async function replace( + word: string, + previousWord: string +): Promise { const list = await getList(); if (word.includes("-")) { //this handles hyphenated words (for example "cream-colored") to make sure //we don't have to add every possible combination to the list return ( - await Promise.all(word.split("-").map(async (w) => replace(w))) + await Promise.all( + word.split("-").map(async (w) => replace(w, previousWord)) + ) ).join("-"); } else { const replacement = list.find((a) => word.match(RegExp(`^([\\W]*${a[0]}[\\W]*)$`, "gi")) ); - return replacement - ? word.replace( - RegExp(`^(?:([\\W]*)(${replacement[0]})([\\W]*))$`, "gi"), - (_, $1, $2, $3) => - $1 + - ($2.charAt(0) === $2.charAt(0).toUpperCase() - ? $2 === $2.toUpperCase() - ? replacement[1].toUpperCase() - : capitalizeFirstLetterOfEachWord(replacement[1]) - : replacement[1]) + - $3 - ) - : word; + + console.log(list); + + if (!replacement) return word; + + if ( + (Config.mode === "quote" || + (Config.mode === "custom" && + !CustomText.isTimeRandom && + !CustomText.isWordRandom && + !CustomText.isSectionRandom)) && + replacement[2]?.includes(previousWord) + ) { + return word; + } + + return word.replace( + RegExp(`^(?:([\\W]*)(${replacement[0]})([\\W]*))$`, "gi"), + (_, $1, $2, $3) => + $1 + + ($2.charAt(0) === $2.charAt(0).toUpperCase() + ? $2 === $2.toUpperCase() + ? replacement[1].toUpperCase() + : capitalizeFirstLetterOfEachWord(replacement[1]) + : replacement[1]) + + $3 + ); } } diff --git a/frontend/src/ts/test/words-generator.ts b/frontend/src/ts/test/words-generator.ts index 66fd3bbf3..7cf9b0e8e 100644 --- a/frontend/src/ts/test/words-generator.ts +++ b/frontend/src/ts/test/words-generator.ts @@ -307,9 +307,12 @@ function applyFunboxesToWord(word: string): string { return word; } -async function applyBritishEnglishToWord(word: string): Promise { +async function applyBritishEnglishToWord( + word: string, + previousWord: string +): Promise { if (Config.britishEnglish && /english/.test(Config.language)) { - word = await BritishEnglish.replace(word); + word = await BritishEnglish.replace(word, previousWord); } return word; } @@ -718,7 +721,7 @@ export async function getNextWord( randomWord = randomWord.replace(/ +/gm, " "); randomWord = randomWord.replace(/(^ )|( $)/gm, ""); randomWord = applyLazyModeToWord(randomWord, language); - randomWord = await applyBritishEnglishToWord(randomWord); + randomWord = await applyBritishEnglishToWord(randomWord, previousWordRaw); if (Config.language === "swiss_german") { randomWord = randomWord.replace(/ß/g, "ss"); diff --git a/frontend/static/languages/britishenglish.json b/frontend/static/languages/britishenglish.json index a53261b28..994124408 100644 --- a/frontend/static/languages/britishenglish.json +++ b/frontend/static/languages/britishenglish.json @@ -504,7 +504,7 @@ ["misbehavior", "misbehaviour"], ["behavior", "behaviour"], ["color", "colour"], - ["tire", "tyre"], + ["tire", "tyre", ["will"]], ["gray", "grey"], ["grays", "greys"], ["theater", "theatre"],