mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-26 08:47:36 +08:00
impr: added more english punctuation replacements
Also refactored the way these are stored - using an object instead of a json file
This commit is contained in:
parent
1c359f5ff7
commit
2c74f752a7
2 changed files with 45 additions and 40 deletions
|
|
@ -1,21 +1,42 @@
|
|||
let pairsList: string[] = [];
|
||||
import { randomElementFromArray } from "../utils/misc";
|
||||
|
||||
export async function getList(): Promise<string[]> {
|
||||
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<boolean> {
|
||||
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<boolean> {
|
|||
}
|
||||
|
||||
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"))
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"]
|
||||
]
|
||||
Loading…
Add table
Reference in a new issue