From 4dbe9bdab70e884c846254aab7080f103d475fe3 Mon Sep 17 00:00:00 2001 From: zhoulixiang Date: Mon, 10 Jun 2024 11:17:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E6=8B=BC=E9=9F=B3=E5=AD=97=E7=AC=A6=E7=9A=84=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/convert/index.ts | 5 +++-- lib/core/pinyin/handle.ts | 18 +++++++++++------- test/convert.test.js | 5 +++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/core/convert/index.ts b/lib/core/convert/index.ts index cc4374a..5292f10 100644 --- a/lib/core/convert/index.ts +++ b/lib/core/convert/index.ts @@ -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̀'], + ê: ['ê', 'ê̄', 'ế', 'ê̌', 'ề'], }; /** diff --git a/lib/core/pinyin/handle.ts b/lib/core/pinyin/handle.ts index 54d3597..65ccfdc 100644 --- a/lib/core/pinyin/handle.ts +++ b/lib/core/pinyin/handle.ts @@ -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, "ê"); }; /** @@ -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) => { @@ -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(""); } diff --git a/test/convert.test.js b/test/convert.test.js index 76c56ba..5d0ed7d 100644 --- a/test/convert.test.js +++ b/test/convert.test.js @@ -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'); + }); });