-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,188 additions
and
1,057 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { soften } from './soften'; | ||
|
||
describe('soften function', () => { | ||
test('softens the last consonant by default', () => { | ||
expect(soften('dnes')).toBe('dneś'); | ||
expect(soften('gaz')).toBe('gaź'); | ||
expect(soften('lad')).toBe('laď'); | ||
}); | ||
|
||
test('softens the specified consonant at given index', () => { | ||
expect(soften('lad', 0)).toBe('ľad'); | ||
expect(soften('selsky', 2)).toBe('seľsky'); | ||
expect(soften('měd', 2)).toBe('měď'); | ||
}); | ||
|
||
test('handles negative indices', () => { | ||
expect(soften('dnes', -1)).toBe('dneś'); | ||
expect(soften('test', -2)).toBe('teśt'); | ||
}); | ||
|
||
test('handles all softenable consonants', () => { | ||
expect(soften('D')).toBe('Ď'); | ||
expect(soften('L')).toBe('Ľ'); | ||
expect(soften('N')).toBe('Ń'); | ||
expect(soften('R')).toBe('Ŕ'); | ||
expect(soften('S')).toBe('Ś'); | ||
expect(soften('T')).toBe('Ť'); | ||
expect(soften('Z')).toBe('Ź'); | ||
expect(soften('d')).toBe('ď'); | ||
expect(soften('l')).toBe('ľ'); | ||
expect(soften('n')).toBe('ń'); | ||
expect(soften('r')).toBe('ŕ'); | ||
expect(soften('s')).toBe('ś'); | ||
expect(soften('t')).toBe('ť'); | ||
expect(soften('z')).toBe('ź'); | ||
}); | ||
|
||
test('does not change non-softenable consonants and vowels', () => { | ||
expect(soften('baba')).toBe('baba'); | ||
expect(soften('mama')).toBe('mama'); | ||
expect(soften('papa')).toBe('papa'); | ||
}); | ||
|
||
test('handles empty strings', () => { | ||
expect(soften('')).toBe(''); | ||
expect(soften('', 0)).toBe(''); | ||
}); | ||
|
||
test('handles out of range index', () => { | ||
expect(soften('test', 4)).toBe('test'); | ||
expect(soften('test', -5)).toBe('test'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const FULL_SOFTENABLE_CONSONANT_MAP: Record<string, string> = { | ||
D: 'Ď', | ||
L: 'Ľ', | ||
N: 'Ń', | ||
R: 'Ŕ', | ||
S: 'Ś', | ||
T: 'Ť', | ||
Z: 'Ź', | ||
d: 'ď', | ||
l: 'ľ', | ||
n: 'ń', | ||
r: 'ŕ', | ||
s: 'ś', | ||
t: 'ť', | ||
z: 'ź', | ||
}; | ||
|
||
export function soften(str: string, index = str.length - 1): string { | ||
const pos = index < 0 ? str.length + index : index; | ||
if (pos < 0) return str; | ||
|
||
const before = str.slice(0, pos) || ''; | ||
const softened = FULL_SOFTENABLE_CONSONANT_MAP[str[pos]] || str[pos] || ''; | ||
const after = str.slice(pos + 1) || ''; | ||
|
||
return before + softened + after; | ||
} |
Oops, something went wrong.