Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khashashin committed May 26, 2024
1 parent 825d10e commit c9a35c6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
10 changes: 10 additions & 0 deletions __tests__/translit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@ce/transliteration",
"version": "0.1.7",
"version": "0.1.8",
"exports": "./translit.ts"
}
10 changes: 6 additions & 4 deletions translit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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̇';
Expand All @@ -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 'е'
}
Expand Down

0 comments on commit c9a35c6

Please sign in to comment.