Improve British English replacement for edge cases (#2104) by DanGonite57

* Replace words that include punctuation

* Avoid partial matches

* Only check case of text portion

* Match case for full caps words
This commit is contained in:
DanGonite57 2021-11-22 16:49:51 +00:00 committed by GitHub
parent 87a9e9aa25
commit 21c5d2936c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,12 +15,20 @@ export async function getList() {
export async function replace(word) {
let list = await getList();
var britishWord =
list[list.findIndex((a) => a[0] === word.toLowerCase())]?.[1];
if (typeof britishWord !== "undefined") {
if (word.charAt(0) === word.charAt(0).toUpperCase()) {
britishWord = capitalizeFirstLetter(britishWord);
}
}
return britishWord;
let 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()
: capitalizeFirstLetter(replacement[1])
: replacement[1]) +
$3
)
: word;
}