Skip to content

Commit

Permalink
[Fix] Update the way we split the string to get pinyin
Browse files Browse the repository at this point in the history
  • Loading branch information
Tykok committed Aug 17, 2024
1 parent 9c3a4c8 commit b1b65cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/format-cedict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const pathOfJSON = `${pathOfData}/cedict.json`;
/**
* Used to parse the cedict file given in the zip
*/
const parseLine = (line: string): ChineseWord | null => {
export const parseLine = (line: string): ChineseWord | null => {
if (!line || line === '' || line.startsWith('#')) return null;
const splitedLine = line.split(/\/(.*)/s);
if (splitedLine.length <= 0) return null;
Expand All @@ -23,9 +23,7 @@ const parseLine = (line: string): ChineseWord | null => {
const characters = charAndPinyin[0].split(' ');
const traditional = characters[0];
const simplified = characters[1];
let pinyin = charAndPinyin[1];
pinyin = pinyin.split(' ')[0] as unknown as string;
pinyin = pinyin.split(']')[0] as unknown as string;
const pinyin = charAndPinyin[1].split(']')[0] as string;

return { traditional, simplified, pinyin, english };
} catch (e: unknown) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/format-cedict.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { parseLine } from '../format-cedict';

describe('Check property return of dictionary function', () => {
it('parseLine should return the right pinyin', () => {
const line = '傍晚 傍晚 [bang4 wan3] /in the evening/when night falls/towards evening/at night fall/at dusk/';
const actual = parseLine(line);

expect(actual).toHaveProperty('pinyin');
expect(actual?.pinyin).toEqual('bang4 wan3');
});
});

0 comments on commit b1b65cd

Please sign in to comment.