Skip to content

Commit

Permalink
impr(typing): lazy mode now keeps the word casing
Browse files Browse the repository at this point in the history
  • Loading branch information
Miodec committed Sep 11, 2023
1 parent ecbc20f commit 9f05da9
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions frontend/src/ts/test/lazy-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,35 @@ export function replaceAccents(
word: string,
accentsOverride?: MonkeyTypes.Accents
): string {
let newWord = word;
if (!accents && !accentsOverride) return newWord;
let regex;
const list = accentsOverride || accents;
for (let i = 0; i < list.length; i++) {
regex = new RegExp(`[${list[i][0]}]`, "gi");
newWord = newWord.replace(regex, list[i][1]);
if (!word) return word;

const accentMap = new Map(accentsOverride || accents);

const uppercased = word.toUpperCase();
const cases = Array(word.length);

for (let i = 0; i < word.length; i++) {
cases[i] = word[i] === uppercased[i] ? 1 : 0;
}
return newWord;

const newWordArray: string[] = [];

for (let i = 0; i < word.length; i++) {
const char = word[i];
if (accentMap.has(char)) {
newWordArray.push(accentMap.get(char) as string);
} else {
newWordArray.push(char);
}
}

if (cases.includes(1)) {
for (let i = 0; i < cases.length; i++) {
if (cases[i] === 1) {
newWordArray[i] = newWordArray[i].toUpperCase();
}
}
}

return newWordArray.join("");
}

0 comments on commit 9f05da9

Please sign in to comment.