From 21c5d2936c7f09b8c9972081cba890edda26d3bc Mon Sep 17 00:00:00 2001 From: DanGonite57 Date: Mon, 22 Nov 2021 16:49:51 +0000 Subject: [PATCH] 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 --- src/js/test/british-english.js | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/js/test/british-english.js b/src/js/test/british-english.js index 7e4f99991..0382c43bf 100644 --- a/src/js/test/british-english.js +++ b/src/js/test/british-english.js @@ -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; }