Skip to content

Commit

Permalink
Merge pull request #248 from zh-lx/faeture-special-final
Browse files Browse the repository at this point in the history
feat: 支持特殊拼音字符的转换
  • Loading branch information
zh-lx authored Jun 10, 2024
2 parents 150c5f0 + 4dbe9bd commit a032e95
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/core/convert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ const toneMap = {
iu: ['iu', 'iū', 'iú', 'iǔ', 'iù'],
i: ['i', 'ī', 'í', 'ǐ', 'ì'],
u: ['u', 'ū', 'ú', 'ǔ', 'ù'],
n: ['n', 'n', 'ń', 'ň', 'ǹ'],
m: ['m', 'm', 'ḿ', 'm', 'm̀'],
n: ['n', 'n̄', 'ń', 'ň', 'ǹ'],
m: ['m', 'm̄', 'ḿ', 'm̌', 'm̀'],
ê: ['ê', 'ê̄', 'ế', 'ê̌', 'ề'],
};

/**
Expand Down
18 changes: 11 additions & 7 deletions lib/core/pinyin/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ const getPinyinWithoutTone: GetPinyinWithoutTone = (pinyin) => {
.replace(/(ī|í|ǐ|ì)/g, "i")
.replace(/(ū|ú|ǔ|ù)/g, "u")
.replace(/(ǖ|ǘ|ǚ|ǜ)/g, "ü")
.replace(/(ń|ň|ǹ)/g, "n")
.replace(/ḿ|m̀/g, "m");
.replace(/(n̄|ń|ň|ǹ)/g, "n")
.replace(/(m̄|ḿ|m̌|m̀)/g, "m")
.replace(/(ê̄|ế|ê̌|)/g, "ê");
};

/**
Expand Down Expand Up @@ -224,11 +225,12 @@ const getFinalParts: GetFinalParts = (pinyin) => {
*/
type GetNumOfTone = (pinyin: string) => string;
const getNumOfTone: GetNumOfTone = (pinyin) => {
const reg_tone1 = /(ā|ō|ē|ī|ū|ǖ)/;
const reg_tone2 = /(á|ó|é|í|ú|ǘ|ń|ḿ)/;
const reg_tone3 = /(ǎ|ǒ|ě|ǐ|ǔ|ǚ|ň)/;
const reg_tone4 = /(à|ò|è|ì|ù|ǜ|ǹ|m̀)/;
const reg_tone0 = /(a|o|e|i|u|ü|n)/;
const reg_tone1 = /(ā|ō|ē|ī|ū|ǖ|n̄|m̄|ê̄)/;
const reg_tone2 = /(á|ó|é|í|ú|ǘ|ń|ḿ|ế)/;
const reg_tone3 = /(ǎ|ǒ|ě|ǐ|ǔ|ǚ|ň|m̌|ê̌)/;
const reg_tone4 = /(à|ò|è|ì|ù|ǜ|ǹ|m̀|)/;
const reg_tone0 = /(a|o|e|i|u|ü|ê)/;
const special_tone = /(n|m)$/;
const tone_num_arr: string[] = [];
const pinyin_arr = pinyin.split(" ");
pinyin_arr.forEach((_pinyin) => {
Expand All @@ -242,6 +244,8 @@ const getNumOfTone: GetNumOfTone = (pinyin) => {
tone_num_arr.push("4");
} else if (reg_tone0.test(_pinyin)) {
tone_num_arr.push("0");
} else if (special_tone.test(_pinyin)) {
tone_num_arr.push("0");
} else {
tone_num_arr.push("");
}
Expand Down
5 changes: 5 additions & 0 deletions test/convert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ describe('convert', () => {
const result = convert('liu2', { format: 'numToSymbol' });
expect(result).to.be.equal('liú');
});

it('[convert]special tone', () => {
const result = convert('m̄ hm ê̄ ế ê̌ ề', { format: 'symbolToNum' });
expect(result).to.be.equal('m1 hm0 ê1 ê2 ê3 ê4');
});
});

0 comments on commit a032e95

Please sign in to comment.