Skip to content

Commit ced4b6e

Browse files
committed
impr(lazy mode): support replacing 2 characters with one
also adds lazy mode to yiddish closes monkeytypegame#6321
1 parent 5ca47e1 commit ced4b6e

File tree

3 files changed

+67
-14
lines changed

3 files changed

+67
-14
lines changed

frontend/__tests__/test/lazy-mode.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ let germanAccents = [
66
["ü", "ue"],
77
] as [string, string][];
88

9+
let multicharAccents = [
10+
["a", "bc"],
11+
["de", "f"],
12+
["gh", "ij"],
13+
] as [string, string][];
14+
915
describe("lazy-mode", () => {
1016
describe("replaceAccents", () => {
1117
it("should replace common accents", () => {
@@ -38,5 +44,23 @@ describe("lazy-mode", () => {
3844
expect(result).toBe("aesse");
3945
});
4046
});
47+
describe("multicharacter accents", () => {
48+
it("should correctly replace multicharacter accents", () => {
49+
const tests = [
50+
{ input: "a", expected: "bc" },
51+
{ input: "aa", expected: "bcbc" },
52+
{ input: "de", expected: "f" },
53+
{ input: "dede", expected: "ff" },
54+
{ input: "gh", expected: "ij" },
55+
{ input: "ghgh", expected: "ijij" },
56+
{ input: "abcdefgh", expected: "bcbcffij" },
57+
];
58+
59+
tests.forEach(({ input, expected }) => {
60+
const result = replaceAccents(input, multicharAccents);
61+
expect(result).toBe(expected);
62+
});
63+
});
64+
});
4165
});
4266
});

frontend/src/ts/test/lazy-mode.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,21 @@ const accentsMap = new Map<string, string>(
4949
export type Accents = [string, string][];
5050

5151
function findAccent(
52-
char: string,
52+
wordSlice: string,
5353
additionalAccents?: Accents
54-
): string | undefined {
55-
const lookup = char.toLowerCase();
54+
): [string, string] | undefined {
55+
const lookup = wordSlice.toLowerCase();
5656

57-
const found = additionalAccents?.find((rule) => rule[0].includes(lookup));
57+
const found = additionalAccents?.find((rule) => lookup.startsWith(rule[0]));
5858

59-
return found !== undefined ? found[1] : accentsMap.get(lookup);
59+
const common = accentsMap.get(lookup[0] as string);
60+
61+
const commonFound =
62+
common !== undefined
63+
? ([lookup[0], common] as [string, string])
64+
: undefined;
65+
66+
return found !== undefined ? found : commonFound;
6067
}
6168

6269
export function replaceAccents(
@@ -68,19 +75,24 @@ export function replaceAccents(
6875
const cases = [...word].map((it, i) => it === uppercased[i]);
6976
const newWordArray: string[] = [];
7077

78+
let offset = 0;
7179
for (let i = 0; i < word.length; i++) {
72-
const char = word[i] as string;
73-
const isUpperCase = cases[i];
74-
const accent = findAccent(char, additionalAccents);
80+
const index = i + offset;
81+
if (index >= word.length) break;
82+
const wordSlice = word.slice(index);
83+
const caseSlice = cases.slice(index);
84+
const accent = findAccent(wordSlice, additionalAccents);
7585

7686
if (accent !== undefined) {
77-
if (isUpperCase) {
78-
newWordArray.push(accent.substring(0, 1).toUpperCase());
79-
newWordArray.push(accent.substring(1));
80-
} else {
81-
newWordArray.push(accent);
87+
for (let j = 0; j < accent[1].length; j++) {
88+
const char = accent[1][j] as string;
89+
const isUpperCase = caseSlice[j] ?? false;
90+
newWordArray.push(isUpperCase ? char.toUpperCase() : char);
8291
}
92+
offset += accent[0].length - 1;
8393
} else {
94+
const char = word[index] as string;
95+
const isUpperCase = cases[index];
8496
newWordArray.push(isUpperCase ? char.toUpperCase() : char);
8597
}
8698
}

frontend/static/languages/yiddish.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,25 @@
22
"name": "yiddish",
33
"rightToLeft": true,
44
"ligatures": true,
5-
"noLazyMode": true,
65
"bcp47": "yi",
6+
"additionalAccents": [
7+
["אַ", "א"],
8+
["אָ", "א"],
9+
["בּ", "ב"],
10+
["בֿ", "ב"],
11+
["וּ", "ו"],
12+
["וֹ", "ו"],
13+
["יִ", "י"],
14+
["כּ", "כ"],
15+
["פּ", "פ"],
16+
["פֿ", "פ"],
17+
["שׂ", "ש"],
18+
["תּ", "ת"],
19+
["ײַ", "יי"],
20+
["ײ", "יי"],
21+
["ױ", "וי"],
22+
["װ", "וו"]
23+
],
724
"words": [
825
"קאַווע",
926
"אויפּס",

0 commit comments

Comments
 (0)