fix: additional accents not applied correctly

This commit is contained in:
Miodec 2025-10-12 16:20:06 +02:00
parent a15d84e0ce
commit 8699351be9
2 changed files with 29 additions and 42 deletions

View file

@ -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);
});
});
});

View file

@ -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(