fix: 兼容 customPinyin 文字和拼音数量不对齐的问题

This commit is contained in:
zhoulixiang
2025-02-23 17:45:00 +08:00
parent fa07de93bd
commit 169ac1a104
3 changed files with 12 additions and 3 deletions

View File

@@ -59,9 +59,9 @@ export const getPinyin = (
const zhChars = splitString(match.zh);
list[i + j] = {
origin: zhChars[j],
result: pinyins[pinyinIndex],
result: pinyins[pinyinIndex] || '',
isZh: true,
originPinyin: pinyins[pinyinIndex],
originPinyin: pinyins[pinyinIndex] || '',
};
pinyinIndex++;
}

View File

@@ -130,6 +130,15 @@ describe('customConfig', () => {
expect(result).to.be.equal('𧒽 shā fā lei ke 𧒽 suàn fǎ shì');
clearAllCustomDicts();
});
it('[custom] length not match', () => {
customPinyin({
你好: 'nihao',
});
const result = pinyin('你好', { toneType: 'none'});
expect(result).to.be.equal('nihao ');
clearAllCustomDicts();
});
});

View File

@@ -74,6 +74,6 @@ declare const getPinyinWithNum: GetPinyinWithNum;
* @param {string} pinyin
* @return {string}
*/
type GetFirstLetter = (pinyin: string) => string;
type GetFirstLetter = (pinyin: string, isZh: boolean) => string;
declare const getFirstLetter: GetFirstLetter;
export { getPinyinWithoutTone, getInitialAndFinal, getMultiplePinyin, getNumOfTone, getPinyinWithNum, getFirstLetter, getFinalParts, };