diff --git a/lib/core/pinyin/handle.ts b/lib/core/pinyin/handle.ts index f2c59e1..e3078a8 100644 --- a/lib/core/pinyin/handle.ts +++ b/lib/core/pinyin/handle.ts @@ -122,6 +122,34 @@ const getPinyinWithoutTone: GetPinyinWithoutTone = (pinyin) => { .replace(/ḿ|m̀/g, 'm'); }; +/** + * @description: 获取单字符的多音拼音 + * @param {string} word + * @return {WordResult[]} + */ +type GetAllPinyin = ( + word: string, + mode?: PinyinMode +) => string[]; +export const getAllPinyin: GetAllPinyin = (word, mode = "normal") => { + const wordCode = word.charCodeAt(0); + const customMultpileDict = getCustomMultpileDict(); + let pinyin = DICT1[wordCode] ? DICT1[wordCode].split(" ") : []; + if (customMultpileDict[wordCode]) { + pinyin = customMultpileDict[wordCode] + ? customMultpileDict[wordCode].split(" ") + : []; + } else if (mode === "surname") { + const surnamePinyin = Surnames[word]; + if (surnamePinyin) { + pinyin = [surnamePinyin].concat( + pinyin.filter((py) => py !== surnamePinyin) + ); + } + } + return pinyin; +}; + /** * @description: 获取单字符的多音拼音 * @param {string} word @@ -132,23 +160,10 @@ type GetMultiplePinyin = ( mode?: PinyinMode ) => SingleWordResult[]; const getMultiplePinyin: GetMultiplePinyin = (word, mode = 'normal') => { - const wordCode = word.charCodeAt(0); - const customMultpileDict = getCustomMultpileDict(); - let pinyin = DICT1[wordCode] || ''; - if (customMultpileDict[wordCode]) { - pinyin = customMultpileDict[wordCode]; - } else if (mode === 'surname') { - const surnamePinyin = Surnames[word]; - if (surnamePinyin) { - pinyin = [ - surnamePinyin, - pinyin.split(' ').filter(py => py !== surnamePinyin), - ].join(' '); - } - } + let pinyin = getAllPinyin(word, mode); if (pinyin) { - return pinyin.split(' ').map((value) => ({ + return pinyin.map((value) => ({ origin: word, result: value, isZh: true, diff --git a/lib/core/pinyin/index.ts b/lib/core/pinyin/index.ts index 2777adc..a423c82 100644 --- a/lib/core/pinyin/index.ts +++ b/lib/core/pinyin/index.ts @@ -90,6 +90,8 @@ interface AllData { finalBody: string; finalTail: string; isZh: boolean; + polyphonic: string[]; + inZhRange: boolean; } interface OptionsReturnString extends BasicOptions { diff --git a/lib/core/pinyin/middlewares.ts b/lib/core/pinyin/middlewares.ts index 35223dc..8c0f070 100644 --- a/lib/core/pinyin/middlewares.ts +++ b/lib/core/pinyin/middlewares.ts @@ -1,10 +1,10 @@ -import { getStringLength } from '@/common/utils'; +import { getStringLength, isZhChar } from '@/common/utils'; import type { SingleWordResult } from '../../common/type'; import { DoubleUnicodePrefixReg, DoubleUnicodeSuffixReg, } from '@/common/constant'; -import { getMultiplePinyin } from './handle'; +import { getAllPinyin, getMultiplePinyin } from './handle'; import { CompleteOptions } from './index'; import { getNumOfTone, @@ -182,6 +182,14 @@ export const middlewareType = ( const pinyin = item.isZh ? item.result : ''; const { initial, final } = getInitialAndFinal(pinyin); const { head, body, tail } = getFinalParts(pinyin); + let polyphonic: string[] = []; + if (pinyin) { + polyphonic = [pinyin].concat( + getAllPinyin(item.origin, options.mode).filter( + (item) => item !== pinyin + ) + ); + } return { origin: item.origin, pinyin, @@ -193,6 +201,8 @@ export const middlewareType = ( finalTail: tail, num: Number(getNumOfTone(item.originPinyin)), isZh: item.isZh, + polyphonic, + inZhRange: isZhChar(item.origin), }; }); } diff --git a/lib/core/polyphonic/index.ts b/lib/core/polyphonic/index.ts index 6db22e8..d417ea3 100644 --- a/lib/core/polyphonic/index.ts +++ b/lib/core/polyphonic/index.ts @@ -15,6 +15,7 @@ import { getFinalParts, } from '@/core/pinyin/handle'; import { getCustomPolyphonicDict } from '../custom'; +import { isZhChar } from '@/common/utils'; interface BasicOptions { /** @@ -70,6 +71,7 @@ interface AllData { finalBody: string; finalTail: string; isZh: boolean; + inZhRange: boolean; } interface OptionsReturnString extends BasicOptions { @@ -257,6 +259,7 @@ export const handleType = ( finalTail: tail, num: Number(getNumOfTone(item.originPinyin)), isZh: item.isZh, + inZhRange: isZhChar(item.origin), }; }); } diff --git a/test/all.test.js b/test/all.test.js index bdfbc32..eace740 100644 --- a/test/all.test.js +++ b/test/all.test.js @@ -18,6 +18,8 @@ describe('all', () => { finalTail: 'n', num: 4, isZh: true, + inZhRange: true, + polyphonic: ['hàn'], }, { origin: '语', @@ -30,6 +32,8 @@ describe('all', () => { finalTail: '', num: 3, isZh: true, + inZhRange: true, + polyphonic: ['yǔ', "yù"], }, { origin: '拼', @@ -42,6 +46,8 @@ describe('all', () => { finalTail: 'n', num: 1, isZh: true, + inZhRange: true, + polyphonic: ['pīn'], }, { origin: '音', @@ -54,6 +60,8 @@ describe('all', () => { finalTail: 'n', num: 1, isZh: true, + inZhRange: true, + polyphonic: ['yīn'], }, ]); }); @@ -74,6 +82,8 @@ describe('all', () => { finalTail: 'n', num: 4, isZh: true, + inZhRange: true, + polyphonic: ['hàn'], }, { origin: 'a', @@ -86,6 +96,8 @@ describe('all', () => { finalTail: '', num: 0, isZh: false, + inZhRange: false, + polyphonic: [], }, { origin: '𧒽', @@ -98,6 +110,8 @@ describe('all', () => { finalTail: '', num: 0, isZh: false, + inZhRange: false, + polyphonic: [], }, { origin: '音', @@ -110,6 +124,8 @@ describe('all', () => { finalTail: 'n', num: 1, isZh: true, + inZhRange: true, + polyphonic: ['yīn'], }, ]); }); @@ -131,6 +147,8 @@ describe('all', () => { finalTail: 'n', num: 4, isZh: true, + inZhRange: true, + polyphonic: ['hàn'], }, { origin: '音', @@ -143,6 +161,8 @@ describe('all', () => { finalTail: 'n', num: 1, isZh: true, + inZhRange: true, + polyphonic: ['yīn'], }, ]); }); @@ -164,6 +184,8 @@ describe('all', () => { finalTail: 'n', num: 4, isZh: true, + inZhRange: true, + polyphonic: ['hàn'], }, { origin: 'a𧒽', @@ -176,6 +198,8 @@ describe('all', () => { finalTail: '', num: 0, isZh: false, + inZhRange: false, + polyphonic: [], }, { origin: '音', @@ -188,6 +212,8 @@ describe('all', () => { finalTail: 'n', num: 1, isZh: true, + inZhRange: true, + polyphonic: ['yīn'], }, ]); }); diff --git a/test/multiple.test.js b/test/multiple.test.js index bd6c955..94d8505 100644 --- a/test/multiple.test.js +++ b/test/multiple.test.js @@ -29,7 +29,7 @@ describe('multiple', () => { it('[multiple]非字符串', () => { const result = pinyin('a', { multiple: true, type: 'array' }); - expect(result).to.deep.equal(['a']); + expect(result).to.deep.equal([]); }); it('[multiple]multiple+surname同时使用', () => { diff --git a/test/polyphonic.test.js b/test/polyphonic.test.js index ac2859d..403bb96 100644 --- a/test/polyphonic.test.js +++ b/test/polyphonic.test.js @@ -32,6 +32,7 @@ describe('polyphonic', () => { num: 3, origin: '好', pinyin: 'hǎo', + inZhRange: true, }, { final: 'ào', @@ -44,6 +45,7 @@ describe('polyphonic', () => { num: 4, origin: '好', pinyin: 'hào', + inZhRange: true, }, ], [ @@ -58,6 +60,7 @@ describe('polyphonic', () => { num: 3, origin: '好', pinyin: 'hǎo', + inZhRange: true, }, { final: 'ào', @@ -70,6 +73,7 @@ describe('polyphonic', () => { num: 4, origin: '好', pinyin: 'hào', + inZhRange: true, }, ], [ @@ -84,6 +88,7 @@ describe('polyphonic', () => { num: 2, origin: '学', pinyin: 'xué', + inZhRange: true, }, ], [ @@ -98,6 +103,7 @@ describe('polyphonic', () => { num: 2, origin: '习', pinyin: 'xí', + inZhRange: true, }, ], ]); @@ -133,6 +139,7 @@ describe('polyphonic', () => { num: 3, origin: '好', pinyin: 'hǎo', + inZhRange: true, }, { final: 'ào', @@ -145,6 +152,7 @@ describe('polyphonic', () => { num: 4, origin: '好', pinyin: 'hào', + inZhRange: true, }, ], [ @@ -159,6 +167,7 @@ describe('polyphonic', () => { num: 3, origin: '好', pinyin: 'hǎo', + inZhRange: true, }, { final: 'ào', @@ -171,6 +180,7 @@ describe('polyphonic', () => { num: 4, origin: '好', pinyin: 'hào', + inZhRange: true, }, ], [ @@ -185,6 +195,7 @@ describe('polyphonic', () => { num: 2, origin: '学', pinyin: 'xué', + inZhRange: true, }, ], [ @@ -199,6 +210,7 @@ describe('polyphonic', () => { num: 2, origin: '习', pinyin: 'xí', + inZhRange: true, }, ], [ @@ -213,6 +225,7 @@ describe('polyphonic', () => { num: 0, origin: 's', pinyin: '', + inZhRange: false, }, ], ]); diff --git a/types/core/pinyin/handle.d.ts b/types/core/pinyin/handle.d.ts index 337d287..37b4d0b 100644 --- a/types/core/pinyin/handle.d.ts +++ b/types/core/pinyin/handle.d.ts @@ -14,6 +14,13 @@ export declare const getPinyin: (word: string, list: SingleWordResult[], mode: ' */ type GetPinyinWithoutTone = (pinyin: string) => string; declare const getPinyinWithoutTone: GetPinyinWithoutTone; +/** + * @description: 获取单字符的多音拼音 + * @param {string} word + * @return {WordResult[]} + */ +type GetAllPinyin = (word: string, mode?: PinyinMode) => string[]; +export declare const getAllPinyin: GetAllPinyin; /** * @description: 获取单字符的多音拼音 * @param {string} word diff --git a/types/core/pinyin/index.d.ts b/types/core/pinyin/index.d.ts index 014df77..72bf2b5 100644 --- a/types/core/pinyin/index.d.ts +++ b/types/core/pinyin/index.d.ts @@ -67,6 +67,8 @@ interface AllData { finalBody: string; finalTail: string; isZh: boolean; + polyphonic: string[]; + inZhRange: boolean; } interface OptionsReturnString extends BasicOptions { /** diff --git a/types/core/pinyin/middlewares.d.ts b/types/core/pinyin/middlewares.d.ts index 9232ef4..fbdfd7b 100644 --- a/types/core/pinyin/middlewares.d.ts +++ b/types/core/pinyin/middlewares.d.ts @@ -17,6 +17,8 @@ export declare const middlewareType: (list: SingleWordResult[], options: Complet finalTail: string; num: number; isZh: boolean; + polyphonic: string[]; + inZhRange: boolean; }[]; export declare const middlewareDoubleUnicode: (list: SingleWordResult[]) => SingleWordResult[]; export declare const middlewareToneSandhi: (list: SingleWordResult[], toneSandhi: boolean) => SingleWordResult[]; diff --git a/types/core/polyphonic/index.d.ts b/types/core/polyphonic/index.d.ts index 477ac63..2e10743 100644 --- a/types/core/polyphonic/index.d.ts +++ b/types/core/polyphonic/index.d.ts @@ -44,6 +44,7 @@ interface AllData { finalBody: string; finalTail: string; isZh: boolean; + inZhRange: boolean; } interface OptionsReturnString extends BasicOptions { /** @@ -113,5 +114,6 @@ export declare const handleType: (list: SingleWordResult[], options: CompleteOpt finalTail: string; num: number; isZh: boolean; + inZhRange: boolean; }[]; export { polyphonic };