diff --git a/src/index.spec.ts b/src/index.spec.ts index cb8a344..67a2370 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -45,6 +45,13 @@ describe('script tests', () => { expect(getRomanization('+அம்!மாஜ்--!', { grantha: false })).toEqual('+am!maaஜ்--!'); }); + it('keeps the grantha option apart between calls', () => { + expect(getIso15919('ஜன்னல்')).toEqual('jaṉṉal'); + expect(getIso15919('ஜன்னல்', { grantha: false })).toEqual('ஜṉṉal'); + expect(getIso15919('ஜன்னல்')).toEqual('jaṉṉal'); + expect(getIPA('ஜன்னல்', { grantha: false })).toEqual('ஜnnal'); + }); + it('transliterates grantha letters with a vowel', () => { expect(getIso15919('ஜன்னல்')).toEqual('jaṉṉal'); expect(getIPA('ஜன்னல்')).toEqual('d͡ʑannal'); diff --git a/src/index.ts b/src/index.ts index e8363a8..8d2bd06 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,13 +17,24 @@ export type Options = { export const script = data as Script[][]; export const granthaScript = grantha as Script[][]; -const transliterate = (phrase: string, notation: 'romanization' | 'iso15919' | 'ipa', options?: Options) => { - const notationMap = [...script, ...(options?.grantha === false ? [] : grantha)] - .flat() - .reduce( - (scripts, newScript) => ({ ...scripts, [newScript.script]: newScript[notation] }), - {} as Record, +type Notation = 'romanization' | 'iso15919' | 'ipa'; + +const notationMaps = new Map>(); + +const getNotationMap = (notation: Notation, withGrantha: boolean) => { + const key = `${notation}:${withGrantha}`; + let notationMap = notationMaps.get(key); + if (!notationMap) { + notationMap = Object.fromEntries( + [...script, ...(withGrantha ? grantha : [])].flat().map((letter) => [letter.script, letter[notation]]), ); + notationMaps.set(key, notationMap); + } + return notationMap; +}; + +const transliterate = (phrase: string, notation: Notation, options?: Options) => { + const notationMap = getNotationMap(notation, options?.grantha !== false); return phrase .split(' ')