Skip to content

Commit

Permalink
Merge pull request #287 from zh-lx/feature-v-string
Browse files Browse the repository at this point in the history
feat: 支持自定义 ü 要替换为的字符串
  • Loading branch information
zh-lx authored Feb 25, 2025
2 parents 169ac1a + 8086122 commit 5108803
Showing 4 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/core/pinyin/index.ts
Original file line number Diff line number Diff line change
@@ -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:开启
2 changes: 1 addition & 1 deletion lib/core/pinyin/middlewares.ts
Original file line number Diff line number Diff line change
@@ -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");
}
});
}
5 changes: 5 additions & 0 deletions test/v.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
3 changes: 2 additions & 1 deletion types/core/pinyin/index.d.ts
Original file line number Diff line number Diff line change
@@ -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:开启

0 comments on commit 5108803

Please sign in to comment.