Skip to content

Commit 8699351

Browse files
committed
fix: additional accents not applied correctly
1 parent a15d84e commit 8699351

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

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

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import { describe, it, expect } from "vitest";
22
import { replaceAccents } from "../../src/ts/test/lazy-mode";
33

4-
let germanAccents = [
5-
["ö", "oe"],
6-
["ä", "ae"],
7-
["ü", "ue"],
8-
] as [string, string][];
9-
10-
let multicharAccents = [
11-
["a", "bc"],
12-
["de", "f"],
13-
["gh", "ij"],
4+
let additionalAccents = [
5+
["abc", "1"],
6+
["def", "22"],
7+
["gh", "333"],
148
] as [string, string][];
159

1610
describe("lazy-mode", () => {
@@ -31,36 +25,17 @@ describe("lazy-mode", () => {
3125
const result = replaceAccents("");
3226
expect(result).toBe("");
3327
});
34-
describe("german accents", () => {
35-
it("should replace additional accents", () => {
36-
const result = replaceAccents("Tränenüberströmt", germanAccents);
37-
expect(result).toBe("Traenenueberstroemt");
38-
});
39-
it("should replace starting with uppercase accent", () => {
40-
const result = replaceAccents("Äpfel", germanAccents);
41-
expect(result).toBe("Aepfel");
42-
});
43-
it("should replace common accents", () => {
44-
const result = replaceAccents("äße", germanAccents);
45-
expect(result).toBe("aesse");
46-
});
47-
});
48-
describe("multicharacter accents", () => {
49-
it("should correctly replace multicharacter accents", () => {
50-
const tests = [
51-
{ input: "a", expected: "bc" },
52-
{ input: "aa", expected: "bcbc" },
53-
{ input: "de", expected: "f" },
54-
{ input: "dede", expected: "ff" },
55-
{ input: "gh", expected: "ij" },
56-
{ input: "ghgh", expected: "ijij" },
57-
{ input: "abcdefgh", expected: "bcbcffij" },
58-
];
59-
60-
tests.forEach(({ input, expected }) => {
61-
const result = replaceAccents(input, multicharAccents);
62-
expect(result).toBe(expected);
63-
});
28+
it("should correctly use additional accents", () => {
29+
const tests = [
30+
{ input: "abc", expected: "111" },
31+
{ input: "abcdef", expected: "111222222" },
32+
{ input: "gh", expected: "333333" },
33+
{ input: "abcdefgh", expected: "111222222333333" },
34+
{ input: "zzdzz", expected: "zz22zz" },
35+
];
36+
tests.forEach(({ input, expected }) => {
37+
const result = replaceAccents(input, additionalAccents);
38+
expect(result).toBe(expected);
6439
});
6540
});
6641
});

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,28 @@ function findAccent(
5757
): [string, string] | undefined {
5858
const lookup = wordSlice.toLowerCase();
5959

60-
const found = additionalAccents?.find((rule) => lookup.startsWith(rule[0]));
60+
const additionalAccentsMap = new Map<string, string>(
61+
additionalAccents?.flatMap((rule) =>
62+
// ignoring for now but this might need a different approach
63+
// eslint-disable-next-line @typescript-eslint/no-misused-spread
64+
[...rule[0]].map((accent) => [accent, rule[1]])
65+
) ?? []
66+
);
6167

6268
const common = accentsMap.get(lookup[0] as string);
69+
const additional = additionalAccentsMap.get(lookup[0] as string);
6370

6471
const commonFound =
6572
common !== undefined
6673
? ([lookup[0], common] as [string, string])
6774
: undefined;
6875

69-
return found !== undefined ? found : commonFound;
76+
const additionalFound =
77+
additional !== undefined
78+
? ([lookup[0], additional] as [string, string])
79+
: undefined;
80+
81+
return additionalFound ?? commonFound;
7082
}
7183

7284
export function replaceAccents(

0 commit comments

Comments
 (0)