From 80861223d0851d0015a2e03f07e796fc3bc27425 Mon Sep 17 00:00:00 2001 From: zhoulixiang Date: Sun, 23 Feb 2025 18:39:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=20=C3=BC=20=E8=A6=81=E6=9B=BF=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/pinyin/index.ts | 3 ++- lib/core/pinyin/middlewares.ts | 2 +- test/v.test.js | 5 +++++ types/core/pinyin/index.d.ts | 3 ++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/core/pinyin/index.ts b/lib/core/pinyin/index.ts index 2b52b52..276605d 100644 --- a/lib/core/pinyin/index.ts +++ b/lib/core/pinyin/index.ts @@ -85,8 +85,9 @@ export interface BasicOptions { * @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效) * @value false:返回值中保留 ü (默认值) * @value true:返回值中 ü 转换成 v + * @value string:返回值中 ü 转换成指定字符 */ - v?: boolean; + v?: boolean | string; /** * @description 是否开启「一」和 「不」字的变调。默认开启。参考:https://zh.wiktionary.org/wiki/Appendix:%E2%80%9C%E4%B8%80%E2%80%9D%E5%8F%8A%E2%80%9C%E4%B8%8D%E2%80%9D%E7%9A%84%E5%8F%98%E8%B0%83 * @value true:开启 diff --git a/lib/core/pinyin/middlewares.ts b/lib/core/pinyin/middlewares.ts index 97bbcd5..304caf7 100644 --- a/lib/core/pinyin/middlewares.ts +++ b/lib/core/pinyin/middlewares.ts @@ -157,7 +157,7 @@ export const middlewareV = ( if (options.v) { list.forEach((item) => { if (item.isZh) { - item.result = item.result.replace(/ü/g, "v"); + item.result = item.result.replace(/ü/g, typeof options.v === 'string' ? options.v : "v"); } }); } diff --git a/test/v.test.js b/test/v.test.js index 6eac6cb..6e6973b 100644 --- a/test/v.test.js +++ b/test/v.test.js @@ -26,4 +26,9 @@ describe('v', () => { const result4 = pinyin('吕布ü', { toneType: 'none', v: true }); expect(result4).to.be.equal('lv bu ü'); }); + + it('[v]string', () => { + const result4 = pinyin('吕和平', { toneType: 'none', v: 'yu' }); + expect(result4).to.be.equal('lyu he ping'); + }); }); diff --git a/types/core/pinyin/index.d.ts b/types/core/pinyin/index.d.ts index bd80711..f5b5a1b 100644 --- a/types/core/pinyin/index.d.ts +++ b/types/core/pinyin/index.d.ts @@ -60,8 +60,9 @@ export interface BasicOptions { * @description 对于 ü 的返回是否转换成 v(仅在 toneType: none 启用时生效) * @value false:返回值中保留 ü (默认值) * @value true:返回值中 ü 转换成 v + * @value string:返回值中 ü 转换成指定字符 */ - v?: boolean; + v?: boolean | string; /** * @description 是否开启「一」和 「不」字的变调。默认开启。参考:https://zh.wiktionary.org/wiki/Appendix:%E2%80%9C%E4%B8%80%E2%80%9D%E5%8F%8A%E2%80%9C%E4%B8%8D%E2%80%9D%E7%9A%84%E5%8F%98%E8%B0%83 * @value true:开启