Skip to content

Commit

Permalink
Merge pull request #157 from zh-lx/fix-nonZh-toneType
Browse files Browse the repository at this point in the history
fix: 修复非中文 toneType: 'num' 时出现额外数字的问题
  • Loading branch information
zh-lx authored Feb 1, 2024
2 parents 00f7213 + 2b56b83 commit 6b5a1e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/core/pinyin/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,37 @@ export const middlewarePattern = (
break;
case 'num':
list.forEach((item) => {
item.result = getNumOfTone(item.result);
item.result = item.isZh ? getNumOfTone(item.result) : '';
});
break;
case 'initial':
list.forEach((item) => {
item.result = getInitialAndFinal(item.result).initial;
item.result = item.isZh ? getInitialAndFinal(item.result).initial : '';
});
break;
case 'final':
list.forEach((item) => {
item.result = getInitialAndFinal(item.result).final;
item.result = item.isZh ? getInitialAndFinal(item.result).final : '';
});
break;
case 'first':
list.forEach((item) => {
item.result = getFirstLetter(item.result);
item.result = item.isZh ? getFirstLetter(item.result) : '';
});
break;
case 'finalHead':
list.forEach((item) => {
item.result = getFinalParts(item.result).head;
item.result = item.isZh ? getFinalParts(item.result).head : '';
});
break;
case 'finalBody':
list.forEach((item) => {
item.result = getFinalParts(item.result).body;
item.result = item.isZh ? getFinalParts(item.result).body : '';
});
break;
case 'finalTail':
list.forEach((item) => {
item.result = getFinalParts(item.result).tail;
item.result = item.isZh ? getFinalParts(item.result).tail : '';
});
break;
default:
Expand All @@ -124,15 +124,19 @@ export const middlewareToneType = (
break;
case 'none':
list.forEach((item) => {
item.result = getPinyinWithoutTone(item.result);
if (item.isZh) {
item.result = getPinyinWithoutTone(item.result);
}
});
break;
case 'num': {
list.forEach((item) => {
item.result = getPinyinWithNum(
item.result,
item.originPinyin as string
);
if (item.isZh) {
item.result = getPinyinWithNum(
item.result,
item.originPinyin as string
);
}
});
break;
}
Expand All @@ -148,7 +152,9 @@ export const middlewareV = (
) => {
if (options.v) {
list.forEach((item) => {
item.result = item.result.replace(/ü/g, 'v');
if (item.isZh) {
item.result = item.result.replace(/ü/g, 'v');
}
});
}
};
Expand Down
10 changes: 10 additions & 0 deletions test/tone-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,14 @@ describe('pattern with toneType', () => {
const resultNum = pinyin('赵钱孙李吧', { toneType: 'num' });
expect(resultNum).to.be.equal('zhao4 qian2 sun1 li3 ba0');
});

it('[tone-type]nonZh', () => {
const resultNum = pinyin('ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz', { toneType: 'num', nonZh: 'consecutive' });
expect(resultNum).to.be.equal('ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz');
});

it('[tone-type]nonZh with consecutive', () => {
const resultNum = pinyin('How are you? ', { toneType: 'num', nonZh: 'consecutive' });
expect(resultNum).to.be.equal('How are you? ');
});
});

0 comments on commit 6b5a1e7

Please sign in to comment.