mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2026-02-04 14:39:02 +08:00
handling hyphenated words in british english
This commit is contained in:
parent
6d88c4f4e3
commit
448c1061b8
1 changed files with 25 additions and 16 deletions
|
|
@ -15,20 +15,29 @@ export async function getList(): Promise<string[]> {
|
|||
|
||||
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"),
|
||||
(_, $1, $2, $3) =>
|
||||
$1 +
|
||||
($2.charAt(0) === $2.charAt(0).toUpperCase()
|
||||
? $2 === $2.toUpperCase()
|
||||
? replacement[1].toUpperCase()
|
||||
: capitalizeFirstLetterOfEachWord(replacement[1])
|
||||
: replacement[1]) +
|
||||
$3
|
||||
)
|
||||
: word;
|
||||
|
||||
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)))
|
||||
).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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue