diff --git a/__tests__/translit.test.ts b/__tests__/translit.test.ts index 7c2a129..97a6a52 100644 --- a/__tests__/translit.test.ts +++ b/__tests__/translit.test.ts @@ -78,6 +78,16 @@ describe('apply', () => { const expected = 'Melxo ə, şuna cẋanna ə txayx bala ca barẋama, diynaẋ ə, busa ə, q̇a ə ẋöguş, bolx bora oxa.'; expect(apply(text)).toBe(expected); }); + it('should transliterate a text correctly', () => { + const text = 'чекхъели чекХъели чекхЪели чекХЪели чекхъЕли чекХъЕли чекхЪЕли чекХЪЕли ЧЕКХЪЕЛИ Чекхъели'; + const expected = 'çeqyeli çeqyeli çeqyeli çeqyeli çeqYeli çeqYeli çeqYeli çeqYeli ÇEQYELI Çeqyeli'; + expect(apply(text)).toBe(expected); + }); + it('should transliterate a text correctly', () => { + const text = "къегина Къегина кЪегина КЪегина къЕгина КъЕгина кЪЕгина КЪЕгина КЪЕГИНА Къегина"; + const expected = 'q̇egina Q̇egina q̇egina Q̇egina q̇Egina Q̇Egina q̇Egina Q̇Egina Q̇EGINA Q̇egina'; + expect(apply(text)).toBe(expected); + }); it('should replace standalone Cyrillic "а" with "ə"', () => { expect(apply('дӏахь а доьгӏна')).toBe('djaẋ ə döġna'); diff --git a/jsr.json b/jsr.json index 701df1f..3fd7cb2 100644 --- a/jsr.json +++ b/jsr.json @@ -1,5 +1,5 @@ { "name": "@ce/transliteration", - "version": "0.1.7", + "version": "0.1.8", "exports": "./translit.ts" } \ No newline at end of file diff --git a/translit.ts b/translit.ts index b257763..87112a9 100644 --- a/translit.ts +++ b/translit.ts @@ -51,8 +51,9 @@ export function applyToWord(word: string): string { ]) { if (translitMap.hasOwnProperty(key)) { const preLetter = i > 0 ? word[i - 1].toLowerCase() : ''; + const nextLetter = i + 1 < word.length ? word[i + 1] : ''; // if key is 'ъ' and next character is 'е', 'ё', 'ю', or 'я' - if ((key === 'ъ' || key === 'Ъ') && i + 1 < word.length && 'еёюяЕЁЮЯ'.includes(word[i + 1])) { + if ((key === 'ъ' || key === 'Ъ') && 'еёюяЕЁЮЯ'.includes(nextLetter)) { // if 'к' is before 'ъ', transliterate 'ъ' as 'q̇' if (i > 0 && (preLetter === 'к')) { match = word[i - 1] === 'к' ? 'q̇' : 'Q̇'; @@ -63,9 +64,10 @@ export function applyToWord(word: string): string { if (i === 0) { match = key === 'е' ? 'ye' : 'Ye'; // 'е' at the start of the word } else if (i > 0) { - const preConsonant = i > 1 ? word.substring(i - 2, i) : ''; - if ((preLetter === 'ъ') && (i < 2 || (!['къ','Къ','кЪ','КЪ'].includes(preConsonant)))) { - match = key === 'е' ? 'ye' : 'Ye'; // 'е' following 'ъ' that does not follow 'къ' + const preConsonant = i > 1 ? word.substring(i - 2, i).toLowerCase() : ''; + if ((preLetter === 'ъ') && (i < 2 || preConsonant !== 'къ')) { + // 'е' following 'ъ' that does not follow 'къ' + match = key === 'е' ? 'ye' : (nextLetter.toUpperCase() === nextLetter ? 'YE' : 'Ye'); } else { match = translitMap[key]; // Regular transliteration for 'е' }