Skip to content

Commit 8966ae4

Browse files
committed
chore: refactor translate() function and tests
1 parent cd78628 commit 8966ae4

File tree

2 files changed

+13
-53
lines changed

2 files changed

+13
-53
lines changed

src/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ export const translate = (
3030
text: string,
3131
script: Script = Script.BAYBAYIN
3232
): string => {
33-
const scriptConverter = scriptConverters[script];
34-
35-
if (scriptConverter) {
36-
return scriptConverter(text);
37-
}
38-
39-
// Default to Baybayin if the script is not recognized
40-
return toBaybayin(text);
33+
const scriptConverter = scriptConverters[script] || toBaybayin;
34+
return scriptConverter(text);
4135
};

test/index.spec.ts

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,15 @@
11
import { Script, translate } from '../src';
22

3-
describe('baybayin', () => {
4-
describe('translate', () => {
5-
it('should return a string containing the baybayin text', () => {
6-
const text = 'a';
7-
8-
const result = translate(text);
9-
10-
expect(result).toMatch('\u1700');
11-
});
12-
});
13-
});
14-
15-
describe('hanunoo', () => {
16-
describe('translate', () => {
17-
it('should return a string containing the hanunoo text', () => {
18-
const text = 'a';
19-
20-
const result = translate(text, Script.HANUNOO);
21-
22-
expect(result).toMatch('\u1720');
23-
});
24-
});
25-
});
26-
27-
describe('buhid', () => {
28-
describe('translate', () => {
29-
it('should return a string containing the buhid text', () => {
30-
const text = 'nga';
31-
32-
const result = translate(text, Script.BUHID);
33-
34-
expect(result).toMatch('\u1745');
35-
});
36-
});
37-
});
38-
39-
describe('tagbanwa', () => {
40-
describe('translate', () => {
41-
it('should return a string containing the tagbanwa text', () => {
42-
const text = 'nga';
43-
44-
const result = translate(text, Script.TAGBANWA);
45-
46-
expect(result).toMatch('\u1765');
47-
});
3+
const testCases = [
4+
{ script: Script.BAYBAYIN, text: 'a', expected: '\u1700' },
5+
{ script: Script.HANUNOO, text: 'a', expected: '\u1720' },
6+
{ script: Script.BUHID, text: 'nga', expected: '\u1745' },
7+
{ script: Script.TAGBANWA, text: 'nga', expected: '\u1765' },
8+
];
9+
10+
testCases.forEach(({ script, text, expected }) => {
11+
test(`translate using script '${script}' with text '${text}' should return '${expected}'`, () => {
12+
const result = translate(text, script);
13+
expect(result).toMatch(expected);
4814
});
4915
});

0 commit comments

Comments
 (0)