Skip to content

Commit a032e95

Browse files
authored
Merge pull request #248 from zh-lx/faeture-special-final
feat: 支持特殊拼音字符的转换
2 parents 150c5f0 + 4dbe9bd commit a032e95

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

lib/core/convert/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ const toneMap = {
3131
iu: ['iu', 'iū', 'iú', 'iǔ', 'iù'],
3232
i: ['i', 'ī', 'í', 'ǐ', 'ì'],
3333
u: ['u', 'ū', 'ú', 'ǔ', 'ù'],
34-
n: ['n', 'n', 'ń', 'ň', 'ǹ'],
35-
m: ['m', 'm', 'ḿ', 'm', 'm̀'],
34+
n: ['n', 'n̄', 'ń', 'ň', 'ǹ'],
35+
m: ['m', 'm̄', 'ḿ', 'm̌', 'm̀'],
36+
ê: ['ê', 'ê̄', 'ế', 'ê̌', 'ề'],
3637
};
3738

3839
/**

lib/core/pinyin/handle.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ const getPinyinWithoutTone: GetPinyinWithoutTone = (pinyin) => {
9999
.replace(/(ī|í|ǐ|ì)/g, "i")
100100
.replace(/(ū|ú|ǔ|ù)/g, "u")
101101
.replace(/(ǖ|ǘ|ǚ|ǜ)/g, "ü")
102-
.replace(/(ń|ň|ǹ)/g, "n")
103-
.replace(/ḿ|m̀/g, "m");
102+
.replace(/(n̄|ń|ň|ǹ)/g, "n")
103+
.replace(/(m̄|ḿ|m̌|m̀)/g, "m")
104+
.replace(/(ê̄|ế|ê̌|)/g, "ê");
104105
};
105106

106107
/**
@@ -224,11 +225,12 @@ const getFinalParts: GetFinalParts = (pinyin) => {
224225
*/
225226
type GetNumOfTone = (pinyin: string) => string;
226227
const getNumOfTone: GetNumOfTone = (pinyin) => {
227-
const reg_tone1 = /(ā|ō|ē|ī|ū|ǖ)/;
228-
const reg_tone2 = /(á|ó|é|í|ú|ǘ|ń|ḿ)/;
229-
const reg_tone3 = /(ǎ|ǒ|ě|ǐ|ǔ|ǚ|ň)/;
230-
const reg_tone4 = /(à|ò|è|ì|ù|ǜ|ǹ|m̀)/;
231-
const reg_tone0 = /(a|o|e|i|u|ü|n)/;
228+
const reg_tone1 = /(ā|ō|ē|ī|ū|ǖ|n̄|m̄|ê̄)/;
229+
const reg_tone2 = /(á|ó|é|í|ú|ǘ|ń|ḿ|ế)/;
230+
const reg_tone3 = /(ǎ|ǒ|ě|ǐ|ǔ|ǚ|ň|m̌|ê̌)/;
231+
const reg_tone4 = /(à|ò|è|ì|ù|ǜ|ǹ|m̀|)/;
232+
const reg_tone0 = /(a|o|e|i|u|ü|ê)/;
233+
const special_tone = /(n|m)$/;
232234
const tone_num_arr: string[] = [];
233235
const pinyin_arr = pinyin.split(" ");
234236
pinyin_arr.forEach((_pinyin) => {
@@ -242,6 +244,8 @@ const getNumOfTone: GetNumOfTone = (pinyin) => {
242244
tone_num_arr.push("4");
243245
} else if (reg_tone0.test(_pinyin)) {
244246
tone_num_arr.push("0");
247+
} else if (special_tone.test(_pinyin)) {
248+
tone_num_arr.push("0");
245249
} else {
246250
tone_num_arr.push("");
247251
}

test/convert.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ describe('convert', () => {
6262
const result = convert('liu2', { format: 'numToSymbol' });
6363
expect(result).to.be.equal('liú');
6464
});
65+
66+
it('[convert]special tone', () => {
67+
const result = convert('m̄ hm ê̄ ế ê̌ ề', { format: 'symbolToNum' });
68+
expect(result).to.be.equal('m1 hm0 ê1 ê2 ê3 ê4');
69+
});
6570
});

0 commit comments

Comments
 (0)