mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2026-01-10 17:34:24 +08:00
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:
parent
2302710dd0
commit
20ec6679a7
3 changed files with 68 additions and 3 deletions
36
frontend/src/scripts/test/english-punctuation.ts
Normal file
36
frontend/src/scripts/test/english-punctuation.ts
Normal 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;
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
18
frontend/static/languages/english_punctuation.json
Normal file
18
frontend/static/languages/english_punctuation.json
Normal 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"]
|
||||
]
|
||||
Loading…
Add table
Reference in a new issue