diff --git a/lib/core/html/index.ts b/lib/core/html/index.ts index ab17264..138e755 100644 --- a/lib/core/html/index.ts +++ b/lib/core/html/index.ts @@ -31,6 +31,12 @@ interface HtmlOptions { customClassMap?: { [classname: string]: 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:开启 + * @value false:不开启 + */ + inflection?: boolean; } const DefaultHtmlOptions: HtmlOptions = { @@ -41,6 +47,7 @@ const DefaultHtmlOptions: HtmlOptions = { wrapNonChinese: false, toneType: 'symbol', customClassMap: {}, + inflection: true, }; /** @@ -57,6 +64,7 @@ export const html = (text: string, options?: HtmlOptions) => { const pinyinArray = pinyin(text, { type: 'all', toneType: completeOptions.toneType, + inflection: options?.inflection, }); const result = pinyinArray.map((item) => { let additionalClass = ''; diff --git a/lib/core/pinyin/index.ts b/lib/core/pinyin/index.ts index c4795aa..a973e39 100644 --- a/lib/core/pinyin/index.ts +++ b/lib/core/pinyin/index.ts @@ -9,6 +9,7 @@ import { middlewareV, middlewareType, middlewareDoubleUnicode, + middlewareInflection, } from './middlewares'; interface BasicOptions { @@ -70,6 +71,12 @@ interface BasicOptions { * @value true:返回值中 ü 转换成 v */ v?: boolean; + /** + * @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:开启 + * @value false:不开启 + */ + inflection?: boolean; } interface AllData { @@ -143,6 +150,7 @@ const DEFAULT_OPTIONS: CompleteOptions = { nonZh: 'spaced', v: false, separator: ' ', + inflection: true, }; /** @@ -207,6 +215,9 @@ function pinyin( list = getPinyin(word, list, options.mode || 'normal'); + // 一和不变调处理 + list = middlewareInflection(list, options.inflection as boolean); + // 双 unicode 编码字符处理 list = middlewareDoubleUnicode(list); diff --git a/lib/core/pinyin/middlewares.ts b/lib/core/pinyin/middlewares.ts index 887df5d..b1a4f63 100644 --- a/lib/core/pinyin/middlewares.ts +++ b/lib/core/pinyin/middlewares.ts @@ -222,3 +222,20 @@ export const middlewareDoubleUnicode = ( }); return list; }; + +// 是否开启变调 +export const middlewareInflection = ( + list: SingleWordResult[], + inflection: boolean, +): SingleWordResult[] => { + if (inflection === false) { + list.forEach(item => { + if (item.origin === '一') { + item.result = item.originPinyin = 'yī'; + } else if (item.origin === '不') { + item.result = item.originPinyin = 'bù'; + } + }) + } + return list; +}; diff --git a/test/special.test.js b/test/special.test.js index eb75616..bcdd8f9 100644 --- a/test/special.test.js +++ b/test/special.test.js @@ -19,7 +19,7 @@ describe('number', () => { it('[number]数字发音 一行人', () => { const result = pinyin('一行人'); - expect(result).to.be.equal('yī xíng rén'); + expect(result).to.be.equal('yì xíng rén'); }); it('[number]数字发音 二百零一行', () => { @@ -64,4 +64,9 @@ describe('special change tone', () => { const result = pinyin('一会儿'); expect(result).to.be.equal('yí huì er'); }); + + it('[special change tone]一会儿', () => { + const result = pinyin('一会儿', { inflection: false }); + expect(result).to.be.equal('yī huì er'); + }); }); diff --git a/types/core/html/index.d.ts b/types/core/html/index.d.ts index fa68df6..5419f71 100644 --- a/types/core/html/index.d.ts +++ b/types/core/html/index.d.ts @@ -29,6 +29,12 @@ interface HtmlOptions { customClassMap?: { [classname: string]: 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:开启 + * @value false:不开启 + */ + inflection?: boolean; } /** * @description: 获取带拼音汉字的 html 字符串 diff --git a/types/core/pinyin/index.d.ts b/types/core/pinyin/index.d.ts index 4e0235f..8be6496 100644 --- a/types/core/pinyin/index.d.ts +++ b/types/core/pinyin/index.d.ts @@ -49,6 +49,12 @@ interface BasicOptions { * @value true:返回值中 ü 转换成 v */ v?: boolean; + /** + * @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:开启 + * @value false:不开启 + */ + inflection?: boolean; } interface AllData { origin: string; diff --git a/types/core/pinyin/middlewares.d.ts b/types/core/pinyin/middlewares.d.ts index 74285ca..5004ae1 100644 --- a/types/core/pinyin/middlewares.d.ts +++ b/types/core/pinyin/middlewares.d.ts @@ -19,3 +19,4 @@ export declare const middlewareType: (list: SingleWordResult[], options: Complet isZh: boolean; }[]; export declare const middlewareDoubleUnicode: (list: SingleWordResult[]) => SingleWordResult[]; +export declare const middlewareInflection: (list: SingleWordResult[], inflection: boolean) => SingleWordResult[];