New punctuation functionality allowing words to end with -n't (#2792) from DenelDuck

* Initial functionality and dictionary with pairs

* functional (not optimal) solution

* remove unused import

* avoid promise callbacks, remove unnecessary else and use strict equals

* camel case
This commit is contained in:
DenelDuck 2022-04-05 18:09:03 +02:00 committed by GitHub
parent 2302710dd0
commit 20ec6679a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 3 deletions

View file

@ -0,0 +1,36 @@
let pairsList: string[] = [];
export async function getList(): Promise<string[]> {
if (pairsList.length === 0) {
return $.getJSON("languages/english_punctuation.json", function (data) {
pairsList = data;
return pairsList;
});
}
return pairsList;
}
// Check if word is in the group of pairs so it can be replaced
export async function check(word: string): Promise<boolean> {
const list = await getList();
if (
list.find((a) => word.match(RegExp(`^([\\W]*${a[0]}[\\W]*)$`, "gi"))) ===
undefined
) {
return false;
}
return true;
}
export async function replace(word: string): Promise<string> {
const list = await getList();
const replacement = list.find((a) =>
word.match(RegExp(`^([\\W]*${a[0]}[\\W]*)$`, "gi"))
);
return replacement
? word.replace(
RegExp(`^(?:([\\W]*)(${replacement[0]})([\\W]*))$`, "gi"),
replacement[1]
)
: word;
}

View file

@ -35,6 +35,7 @@ import * as Wordset from "./wordset";
import * as ChallengeContoller from "../controllers/challenge-controller";
import * as QuoteRatePopup from "../popups/quote-rate-popup";
import * as BritishEnglish from "./british-english";
import * as EnglishPunctuation from "./english-punctuation";
import * as LazyMode from "./lazy-mode";
import * as Result from "./result";
import * as MonkeyPower from "../elements/monkey-power";
@ -70,12 +71,12 @@ export function setNotSignedInUid(uid: string): void {
}
let spanishSentenceTracker = "";
export function punctuateWord(
export async function punctuateWord(
previousWord: string,
currentWord: string,
index: number,
maxindex: number
): string {
): Promise<string> {
let word = currentWord;
const currentLanguage = Config.language.split("_")[0];
@ -229,11 +230,21 @@ export function punctuateWord(
const specials = ["{", "}", "[", "]", "(", ")", ";", "=", "+", "%", "/"];
word = Misc.randomElementFromArray(specials);
} else if (
Math.random() < 0.5 &&
currentLanguage === "english" &&
(await EnglishPunctuation.check(word))
) {
word = await applyEnglishPunctuationToWord(word);
}
}
return word;
}
async function applyEnglishPunctuationToWord(word: string): Promise<string> {
return EnglishPunctuation.replace(word);
}
export function startTest(): boolean {
if (PageTransition.get()) {
return false;
@ -681,7 +692,7 @@ async function getNextWord(
randomWord = applyFunboxesToWord(randomWord, wordset);
if (Config.punctuation) {
randomWord = punctuateWord(
randomWord = await punctuateWord(
TestWords.words.get(TestWords.words.length - 1),
randomWord,
TestWords.words.length,

View file

@ -0,0 +1,18 @@
[
["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", "shoudln't"],
["was", "wasn't"],
["were", "weren't"],
["will", "won't"],
["would", "wouldn't"]
]