Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 15 additions & 40 deletions frontend/__tests__/test/lazy-mode.spec.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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);
});
});
});
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/ts/test/lazy-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>(
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(
Expand Down
Loading