diff --git a/src/conversion/katakana.ts b/src/conversion/katakana.ts index aaf9398..739a3bb 100644 --- a/src/conversion/katakana.ts +++ b/src/conversion/katakana.ts @@ -153,7 +153,7 @@ export interface KanaConversionOptions { } /** Gemination normalization, always applied (not user-configurable). */ -const GEMINATION_TABLE = { +export const GEMINATION_TABLE = { ㇷ゚パ: "ッパ", ㇷ゚ピ: "ッピ", ㇷ゚ペ: "ッペ", diff --git a/test/index.test.ts b/test/index.test.ts index e4a1124..4625241 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,4 +1,5 @@ import { expect, test } from "bun:test"; +import { GEMINATION_TABLE } from "../src/conversion/katakana"; import { convertCyrlToHang, convertCyrlToKana, @@ -27,6 +28,14 @@ function removeYiWu(str: string) { return str.replace(/yi/, "i").replace(/wu/, "u"); } +function normalizeGemination(kana: string) { + let normalized = kana; + for (const [variant, replacement] of Object.entries(GEMINATION_TABLE)) { + normalized = normalized.replaceAll(variant, replacement); + } + return normalized; +} + test("Script Detection", () => { expect(detect("aynu")).toBe("Latn"); expect(detect("アイヌ")).toBe("Kana"); @@ -47,7 +56,7 @@ test("Script Conversion (Latn -> Kana)", () => { for (const testCase of TEST_CASES) { const { latn, kana } = testCase; const converted = convertLatnToKana(latn); - const expected = kana.replace("ㇰカ", "ッカ"); + const expected = normalizeGemination(kana); if (converted !== expected) { console.log(`"${latn}" -> "${converted}" (expecting "${expected}")`); } @@ -61,7 +70,7 @@ test("Script Conversion (Latn -> Kana) with options", () => { testCase as { variants?: { options: object; kana: string }[] } ).variants ?? []) { const converted = convertLatnToKana(testCase.latn, variant.options); - const expected = variant.kana.replace("ㇰカ", "ッカ"); + const expected = normalizeGemination(variant.kana); if (converted !== expected) { console.log( `"${testCase.latn}" ${JSON.stringify(variant.options)} -> "${converted}" (expecting "${expected}")`, @@ -141,7 +150,7 @@ test("Script Conversion (Cyrl -> Kana)", () => { for (const testCase of TEST_CASES) { const { kana, cyrl } = testCase; const converted = convertCyrlToKana(cyrl); - const expected = kana.replace("ㇰカ", "ッカ"); + const expected = normalizeGemination(kana); if (converted !== expected) { console.log(`"${cyrl}" -> "${converted}" (expecting "${kana}")`); } @@ -182,7 +191,7 @@ test("Script Conversion (Hang -> Kana)", () => { for (const testCase of TEST_CASES) { const { kana, hang } = testCase; const converted = convertHangToKana(hang); - const expected = kana.replace("ㇰカ", "ッカ"); + const expected = normalizeGemination(kana); if (converted !== expected) { console.log(`"${hang}" -> "${converted}" (expecting "${expected}")`); } @@ -191,7 +200,7 @@ test("Script Conversion (Hang -> Kana)", () => { for (const testCase of ROBUSTNESS.filter((t) => t.from === "Hang")) { const { Hang, Kana } = testCase; - expect(convertHangToKana(Hang)).toBe(Kana.replace("ㇰカ", "ッカ")); + expect(convertHangToKana(Hang)).toBe(normalizeGemination(Kana)); } });