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
7 changes: 7 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
23 changes: 17 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>,
type Notation = 'romanization' | 'iso15919' | 'ipa';

const notationMaps = new Map<string, Record<string, string>>();

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(' ')
Expand Down
Loading