mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2024-09-20 23:36:37 +08:00
added a third entry to the british english list
this list contains a list of possible previous words - when matched, the replacement will not happen
This commit is contained in:
parent
6d2e2e7939
commit
c19ec62ffc
|
@ -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<string[]> {
|
||||
let list: BritishEnglishReplacement[] = [];
|
||||
|
||||
export async function getList(): Promise<BritishEnglishReplacement[]> {
|
||||
if (list.length === 0) {
|
||||
return $.getJSON("languages/britishenglish.json", function (data) {
|
||||
list = data;
|
||||
|
@ -13,31 +21,50 @@ export async function getList(): Promise<string[]> {
|
|||
}
|
||||
}
|
||||
|
||||
export async function replace(word: string): Promise<string> {
|
||||
export async function replace(
|
||||
word: string,
|
||||
previousWord: string
|
||||
): Promise<string> {
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -307,9 +307,12 @@ function applyFunboxesToWord(word: string): string {
|
|||
return word;
|
||||
}
|
||||
|
||||
async function applyBritishEnglishToWord(word: string): Promise<string> {
|
||||
async function applyBritishEnglishToWord(
|
||||
word: string,
|
||||
previousWord: string
|
||||
): Promise<string> {
|
||||
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");
|
||||
|
|
|
@ -504,7 +504,7 @@
|
|||
["misbehavior", "misbehaviour"],
|
||||
["behavior", "behaviour"],
|
||||
["color", "colour"],
|
||||
["tire", "tyre"],
|
||||
["tire", "tyre", ["will"]],
|
||||
["gray", "grey"],
|
||||
["grays", "greys"],
|
||||
["theater", "theatre"],
|
||||
|
|
Loading…
Reference in a new issue