Skip to content

Commit

Permalink
Merge pull request #185 from zh-lx/feat-inflection
Browse files Browse the repository at this point in the history
feat: 增加变调开关
  • Loading branch information
zh-lx authored Mar 18, 2024
2 parents de7d2bd + 1383ef0 commit 7f79f2f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/core/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -41,6 +47,7 @@ const DefaultHtmlOptions: HtmlOptions = {
wrapNonChinese: false,
toneType: 'symbol',
customClassMap: {},
inflection: true,
};

/**
Expand All @@ -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 = '';
Expand Down
11 changes: 11 additions & 0 deletions lib/core/pinyin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
middlewareV,
middlewareType,
middlewareDoubleUnicode,
middlewareInflection,
} from './middlewares';

interface BasicOptions {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -143,6 +150,7 @@ const DEFAULT_OPTIONS: CompleteOptions = {
nonZh: 'spaced',
v: false,
separator: ' ',
inflection: true,
};

/**
Expand Down Expand Up @@ -207,6 +215,9 @@ function pinyin(

list = getPinyin(word, list, options.mode || 'normal');

// 一和不变调处理
list = middlewareInflection(list, options.inflection as boolean);

// 双 unicode 编码字符处理
list = middlewareDoubleUnicode(list);

Expand Down
17 changes: 17 additions & 0 deletions lib/core/pinyin/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
7 changes: 6 additions & 1 deletion test/special.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('number', () => {

it('[number]数字发音 一行人', () => {
const result = pinyin('一行人');
expect(result).to.be.equal(' xíng rén');
expect(result).to.be.equal(' xíng rén');
});

it('[number]数字发音 二百零一行', () => {
Expand Down Expand Up @@ -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');
});
});
6 changes: 6 additions & 0 deletions types/core/html/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 字符串
Expand Down
6 changes: 6 additions & 0 deletions types/core/pinyin/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions types/core/pinyin/middlewares.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

0 comments on commit 7f79f2f

Please sign in to comment.