Skip to content

Commit

Permalink
Merge pull request #198 from zh-lx/fix-multiple-with-surname
Browse files Browse the repository at this point in the history
fix: 修复 multiple 和 surname 一同使用的情景
  • Loading branch information
zh-lx authored Mar 21, 2024
2 parents f09c368 + 57302c8 commit 2750c1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
18 changes: 13 additions & 5 deletions lib/core/pinyin/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,19 @@ type GetMultiplePinyin = (
const getMultiplePinyin: GetMultiplePinyin = (word, mode = 'normal') => {
const wordCode = word.charCodeAt(0);
const customMultpileDict = getCustomMultpileDict();
const pinyin =
customMultpileDict[wordCode] ||
(mode === 'surname' ? Surnames[word] : '') ||
DICT1[wordCode] ||
'';
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(' ');
}
}

if (pinyin) {
return pinyin.split(' ').map((value) => ({
origin: word,
Expand Down
12 changes: 11 additions & 1 deletion test/multiple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ describe('multiple', () => {

it('[multiple]multiple+surname同时使用', () => {
const result = pinyin('能', { mode: 'surname', multiple: true });
expect(result).to.be.equal('nài');
expect(result).to.be.equal('nài néng');
});

it('[multiple]multiple+surname同时使用,无surname', () => {
const result = pinyin('好', { mode: 'surname', multiple: true });
expect(result).to.be.equal('hǎo hào');
});

it('[multiple]base', () => {
const result = pinyin('好', { multiple: true });
expect(result).to.be.equal('hǎo hào');
});

it('[multiple]multiple+surname同时使用,多音字优先使用姓氏读音', () => {
const result = pinyin('数学家华罗庚', { mode: 'surname', multiple: true });
expect(result).to.be.equal('shù xué jiā huà luó gēng');
});
});

0 comments on commit 2750c1d

Please sign in to comment.