fix(test): resolve I being replaced with i when english punctuation was enabled

closes #4763
This commit is contained in:
Miodec 2023-11-06 13:53:00 +00:00
parent 79d1b354a9
commit 04077fac61

View file

@ -1,4 +1,7 @@
import { randomElementFromArray } from "../utils/misc";
import {
capitalizeFirstLetterOfEachWord,
randomElementFromArray,
} from "../utils/misc";
type Pair = [string, string[]];
@ -54,6 +57,21 @@ export async function replace(word: string): Promise<string> {
return word.replace(
RegExp(`^(?:([\\W]*)(${replacement[0]})([\\W]*))$`, "gi"),
randomReplacement
(_, $1, $2, $3) =>
$1 +
($2.charAt(0) === $2.charAt(0).toUpperCase()
? shouldWholeReplacementWordBeCapitalised($2)
? randomReplacement.toUpperCase()
: capitalizeFirstLetterOfEachWord(randomReplacement)
: randomReplacement) +
$3
);
}
function shouldWholeReplacementWordBeCapitalised(
wordToBeReplaced: string
): boolean {
if (wordToBeReplaced === "I") return false;
if (wordToBeReplaced === wordToBeReplaced.toUpperCase()) return true;
return false;
}