From 8699351be98897073bea5d38cd8cffcef244bf69 Mon Sep 17 00:00:00 2001 From: Miodec Date: Sun, 12 Oct 2025 16:20:06 +0200 Subject: [PATCH] fix: additional accents not applied correctly --- frontend/__tests__/test/lazy-mode.spec.ts | 55 +++++++---------------- frontend/src/ts/test/lazy-mode.ts | 16 ++++++- 2 files changed, 29 insertions(+), 42 deletions(-) diff --git a/frontend/__tests__/test/lazy-mode.spec.ts b/frontend/__tests__/test/lazy-mode.spec.ts index 6b1660cb4..84fedf2c0 100644 --- a/frontend/__tests__/test/lazy-mode.spec.ts +++ b/frontend/__tests__/test/lazy-mode.spec.ts @@ -1,16 +1,10 @@ import { describe, it, expect } from "vitest"; import { replaceAccents } from "../../src/ts/test/lazy-mode"; -let germanAccents = [ - ["ö", "oe"], - ["ä", "ae"], - ["ü", "ue"], -] as [string, string][]; - -let multicharAccents = [ - ["a", "bc"], - ["de", "f"], - ["gh", "ij"], +let additionalAccents = [ + ["abc", "1"], + ["def", "22"], + ["gh", "333"], ] as [string, string][]; describe("lazy-mode", () => { @@ -31,36 +25,17 @@ describe("lazy-mode", () => { const result = replaceAccents(""); expect(result).toBe(""); }); - describe("german accents", () => { - it("should replace additional accents", () => { - const result = replaceAccents("Tränenüberströmt", germanAccents); - expect(result).toBe("Traenenueberstroemt"); - }); - it("should replace starting with uppercase accent", () => { - const result = replaceAccents("Äpfel", germanAccents); - expect(result).toBe("Aepfel"); - }); - it("should replace common accents", () => { - const result = replaceAccents("äße", germanAccents); - expect(result).toBe("aesse"); - }); - }); - describe("multicharacter accents", () => { - it("should correctly replace multicharacter accents", () => { - const tests = [ - { input: "a", expected: "bc" }, - { input: "aa", expected: "bcbc" }, - { input: "de", expected: "f" }, - { input: "dede", expected: "ff" }, - { input: "gh", expected: "ij" }, - { input: "ghgh", expected: "ijij" }, - { input: "abcdefgh", expected: "bcbcffij" }, - ]; - - tests.forEach(({ input, expected }) => { - const result = replaceAccents(input, multicharAccents); - expect(result).toBe(expected); - }); + it("should correctly use additional accents", () => { + const tests = [ + { input: "abc", expected: "111" }, + { input: "abcdef", expected: "111222222" }, + { input: "gh", expected: "333333" }, + { input: "abcdefgh", expected: "111222222333333" }, + { input: "zzdzz", expected: "zz22zz" }, + ]; + tests.forEach(({ input, expected }) => { + const result = replaceAccents(input, additionalAccents); + expect(result).toBe(expected); }); }); }); diff --git a/frontend/src/ts/test/lazy-mode.ts b/frontend/src/ts/test/lazy-mode.ts index 0b15e3c1d..c703b7c1f 100644 --- a/frontend/src/ts/test/lazy-mode.ts +++ b/frontend/src/ts/test/lazy-mode.ts @@ -57,16 +57,28 @@ function findAccent( ): [string, string] | undefined { const lookup = wordSlice.toLowerCase(); - const found = additionalAccents?.find((rule) => lookup.startsWith(rule[0])); + const additionalAccentsMap = new Map( + additionalAccents?.flatMap((rule) => + // ignoring for now but this might need a different approach + // eslint-disable-next-line @typescript-eslint/no-misused-spread + [...rule[0]].map((accent) => [accent, rule[1]]) + ) ?? [] + ); const common = accentsMap.get(lookup[0] as string); + const additional = additionalAccentsMap.get(lookup[0] as string); const commonFound = common !== undefined ? ([lookup[0], common] as [string, string]) : undefined; - return found !== undefined ? found : commonFound; + const additionalFound = + additional !== undefined + ? ([lookup[0], additional] as [string, string]) + : undefined; + + return additionalFound ?? commonFound; } export function replaceAccents(