handling hyphenated words in british english

This commit is contained in:
Miodec 2023-07-17 13:41:48 +02:00
parent 6d88c4f4e3
commit 448c1061b8

View file

@ -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;
}
}