pinyin-pro
Advanced tools
Comparing version 3.6.1 to 3.6.2
@@ -0,1 +1,9 @@ | ||
## 3.6.2 | ||
当前版本: 3.6.1 -> 3.6.2 | ||
- 问题修复 | ||
- 修复 `customPinyin` 和 `surname` 同时使用时没有优先匹配自定义拼音的问题 | ||
- 修复 `surname` 模式下部分情况姓氏拼音匹配不正确的问题 | ||
## 3.6.1 | ||
@@ -2,0 +10,0 @@ |
@@ -1,7 +0,2 @@ | ||
import _DICT1 from '../data/dict1'; | ||
import _DICT2 from '../data/dict2'; | ||
import _DICT3 from '../data/dict3'; | ||
import _DICT4 from '../data/dict4'; | ||
import _DICT5 from '../data/dict5'; | ||
const _dictArr = [{}, {}, _DICT2, _DICT3, _DICT4, _DICT5]; | ||
let customDict: { [key: string]: string } = {}; | ||
@@ -13,20 +8,22 @@ /** | ||
export function customPinyin(config: { [key: string]: string } = {}) { | ||
for (let key in config) { | ||
let pinyin = config[key]; | ||
if (key.length === 1) { | ||
const wordCode = key.charCodeAt(0); | ||
_DICT1[wordCode] = pinyin; | ||
} else if (key.length === 2) { | ||
_dictArr[2][key] = pinyin; | ||
} else if (key.length === 3) { | ||
_dictArr[3][key] = pinyin; | ||
} else if (key.length === 4) { | ||
_dictArr[4][key] = pinyin; | ||
} else { | ||
_dictArr[5][key] = pinyin; | ||
customDict = {}; | ||
const keys = Object.keys(config).sort( | ||
(key1, key2) => key2.length - key1.length | ||
); | ||
keys.forEach((key) => { | ||
customDict[key] = config[key]; | ||
}); | ||
} | ||
export const getCustomDict = () => { | ||
return customDict; | ||
}; | ||
export function hasCustomConfig() { | ||
for (let key in customDict) { | ||
if (key) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
export const DICT1 = _DICT1; | ||
export const dictArr = _dictArr; |
import INITIAL_LIST from '../data/initial'; | ||
import Surnames from '../data/surname'; | ||
import { DICT1, dictArr } from './custom'; | ||
import { getCustomDict } from './custom'; | ||
import DICT1 from '../data/dict1'; | ||
import DICT2 from '../data/dict2'; | ||
import DICT3 from '../data/dict3'; | ||
import DICT4 from '../data/dict4'; | ||
import DICT5 from '../data/dict5'; | ||
const dictArr = [{}, {}, DICT2, DICT3, DICT4, DICT5]; | ||
@@ -28,7 +34,19 @@ /** | ||
length: number, | ||
mode?: 'normal' | 'surname' | ||
params?: { | ||
mode?: 'normal' | 'surname'; | ||
useCustomConfig?: boolean; | ||
} | ||
) => string; | ||
const getPinyin: GetPinYin = (word, length, mode = 'normal') => { | ||
const getPinyin: GetPinYin = ( | ||
word, | ||
length, | ||
params = { mode: 'normal', useCustomConfig: false } | ||
) => { | ||
// 如果有用户自定拼音,则优先使用自定义拼音 | ||
if (params?.useCustomConfig) { | ||
return getCustomPinyin(word, params?.mode); | ||
} | ||
// 如果是姓氏模式,则优先替换姓氏拼音 | ||
if (mode === 'surname') { | ||
if (params?.mode === 'surname') { | ||
return getSurnamePinyin(word); | ||
@@ -90,5 +108,5 @@ } | ||
const left_word = word.slice(0, index); | ||
// left_word 存在时,说明左侧不存在姓氏词 | ||
// 取出该词后左边拼音 | ||
const left_pinyin = left_word | ||
? `${getPinyin(left_word, left_word.length)} ` | ||
? `${getPinyin(left_word, left_word.length, { mode: 'surname' })} ` | ||
: ''; | ||
@@ -98,3 +116,3 @@ // 取出该词后右边拼音 | ||
const right_pinyin = right_word | ||
? ` ${getPinyin(right_word, right_word.length, 'surname')}` | ||
? ` ${getPinyin(right_word, right_word.length, { mode: 'surname' })}` | ||
: ''; | ||
@@ -106,2 +124,3 @@ // 取出的词的拼音 | ||
} | ||
// 若姓氏表中的词均为匹配成功,则使用常规匹配 | ||
return getPinyin(word, word.length); | ||
@@ -111,2 +130,39 @@ }; | ||
/** | ||
* @description: 有自定义拼音优先匹配自定义拼音 | ||
* @param {string} word | ||
* @param {string} mode | ||
* @return {string} | ||
*/ | ||
type GetCustomPinyin = (word: string, mode?: 'normal' | 'surname') => string; | ||
const getCustomPinyin: GetCustomPinyin = (word, mode = 'normal') => { | ||
const customDict = getCustomDict(); | ||
let _word = word; | ||
for (let key in customDict) { | ||
let index = _word.indexOf(key); | ||
if (index > -1) { | ||
const left_word = word.slice(0, index); | ||
// 取出该词后左边拼音 | ||
const left_pinyin = left_word | ||
? `${getPinyin(left_word, left_word.length, { | ||
mode, | ||
useCustomConfig: true, | ||
})} ` | ||
: ''; | ||
// 取出该词后右边拼音 | ||
const right_word = word.slice(index + key.length); | ||
const right_pinyin = right_word | ||
? ` ${getPinyin(right_word, right_word.length, { | ||
mode, | ||
useCustomConfig: true, | ||
})}` | ||
: ''; | ||
// 取出的词的拼音 | ||
const word_pinyin = customDict[key]; | ||
return `${left_pinyin}${word_pinyin}${right_pinyin}`; | ||
} | ||
} | ||
return getPinyin(word, word.length, { mode }); | ||
}; | ||
/** | ||
* @description: 将带音调符号拼音转换为不带音调拼音 | ||
@@ -113,0 +169,0 @@ * @param {string} pinyin |
@@ -10,2 +10,3 @@ import { | ||
} from './handle'; | ||
import { hasCustomConfig } from './custom'; | ||
@@ -85,3 +86,6 @@ interface BasicOptions { | ||
// 获取原始拼音 | ||
let pinyin = getPinyin(word, word.length, options.mode); | ||
let pinyin = getPinyin(word, word.length, { | ||
mode: options.mode, | ||
useCustomConfig: hasCustomConfig(), | ||
}); | ||
@@ -88,0 +92,0 @@ // 对multiple进行处理 |
{ | ||
"name": "pinyin-pro", | ||
"version": "3.6.1", | ||
"version": "3.6.2", | ||
"description": "汉字转拼音库。获取中文拼音、韵母、声母、声调、首字母,支持拼音匹配", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -26,6 +26,7 @@ [![pinyin-pro Logo](https://i.ibb.co/26fJ5vF/pinyin-logo.png)](https://github.com/zh-lx/pinyin-pro) | ||
当前版本: 3.6.0 -> 3.6.1 | ||
当前版本: 3.6.1 -> 3.6.2 | ||
- 问题修复 | ||
- 修复 `removeNonZh` 参数为 `true` 且输入字符串全部为非汉字时的堆栈溢出问题 | ||
- 修复 `customPinyin` 和 `surname` 同时使用时没有优先匹配自定义拼音的问题 | ||
- 修复 `surname` 模式下部分情况姓氏拼音匹配不正确的问题 | ||
@@ -32,0 +33,0 @@ 点击查看 [版本更新文档](./CHANGELOG.md) |
@@ -272,50 +272,7 @@ const { pinyin, customPinyin, match } = require('../dist/index'); | ||
}); | ||
}); | ||
describe('customConfig', () => { | ||
it('custom none', () => { | ||
customPinyin(); | ||
const result = pinyin('干一行行一行'); | ||
expect(result).to.be.equal('gān yī xíng xíng yī xíng'); | ||
it('multiple surname2', () => { | ||
const result = pinyin('曾令狐冲', { mode: 'surname' }); | ||
expect(result).to.be.equal('zēng líng hú chōng'); | ||
}); | ||
it('custom1', () => { | ||
customPinyin({ | ||
能: 'nài', | ||
}); | ||
const result = pinyin('我姓能'); | ||
expect(result).to.be.equal('wǒ xìng nài'); | ||
}); | ||
it('custom2', () => { | ||
customPinyin({ | ||
好好: 'hào hǎo', | ||
}); | ||
const result = pinyin('爱好好多'); | ||
expect(result).to.be.equal('ài hào hǎo duō'); | ||
}); | ||
it('custom3', () => { | ||
customPinyin({ | ||
哈什玛: 'hà shén mǎ', | ||
}); | ||
const result = pinyin('哈什玛'); | ||
expect(result).to.be.equal('hà shén mǎ'); | ||
}); | ||
it('custom4', () => { | ||
customPinyin({ | ||
暴虎冯河: 'bào hǔ píng hé', | ||
}); | ||
const result = pinyin('暴虎冯河'); | ||
expect(result).to.be.equal('bào hǔ píng hé'); | ||
}); | ||
it('custom>5', () => { | ||
customPinyin({ | ||
干一行行一行: 'gàn yī háng xíng yī háng', | ||
}); | ||
const result = pinyin('干一行行一行'); | ||
expect(result).to.be.equal('gàn yī háng xíng yī háng'); | ||
}); | ||
}); | ||
@@ -467,1 +424,66 @@ | ||
}); | ||
describe('customConfig', () => { | ||
it('custom none', () => { | ||
customPinyin(); | ||
const result = pinyin('干一行行一行'); | ||
expect(result).to.be.equal('gān yī xíng xíng yī xíng'); | ||
}); | ||
it('custom1', () => { | ||
customPinyin({ | ||
能: 'nài', | ||
}); | ||
const result = pinyin('我姓能'); | ||
expect(result).to.be.equal('wǒ xìng nài'); | ||
}); | ||
it('custom2', () => { | ||
customPinyin({ | ||
好好: 'hào hǎo', | ||
}); | ||
const result = pinyin('爱好好多'); | ||
expect(result).to.be.equal('ài hào hǎo duō'); | ||
}); | ||
it('custom3', () => { | ||
customPinyin({ | ||
哈什玛: 'hà shén mǎ', | ||
}); | ||
const result = pinyin('哈什玛'); | ||
expect(result).to.be.equal('hà shén mǎ'); | ||
}); | ||
it('custom4', () => { | ||
customPinyin({ | ||
暴虎冯河: 'bào hǔ píng hé', | ||
}); | ||
const result = pinyin('暴虎冯河'); | ||
expect(result).to.be.equal('bào hǔ píng hé'); | ||
}); | ||
it('custom>5', () => { | ||
customPinyin({ | ||
干一行行一行: 'gàn yī háng xíng yī háng', | ||
}); | ||
const result = pinyin('干一行行一行'); | ||
expect(result).to.be.equal('gàn yī háng xíng yī háng'); | ||
}); | ||
it('custom with surname', () => { | ||
customPinyin({ | ||
乐嘉: 'lè jiā', | ||
}); | ||
const result = pinyin('乐嘉', { mode: 'surname' }); | ||
expect(result).to.be.equal('lè jiā'); | ||
}); | ||
it('customs', () => { | ||
customPinyin({ | ||
好: 'hào', | ||
好好: 'hào hǎo', | ||
}); | ||
const result = pinyin('好好'); | ||
expect(result).to.be.equal('hào hǎo'); | ||
}); | ||
}); |
@@ -8,5 +8,5 @@ /** | ||
}): void; | ||
export declare const DICT1: string[]; | ||
export declare const dictArr: { | ||
[prop: string]: string; | ||
}[]; | ||
export declare const getCustomDict: () => { | ||
[key: string]: string; | ||
}; | ||
export declare function hasCustomConfig(): boolean; |
@@ -8,3 +8,6 @@ /** | ||
*/ | ||
declare type GetPinYin = (word: string, length: number, mode?: 'normal' | 'surname') => string; | ||
declare type GetPinYin = (word: string, length: number, params?: { | ||
mode?: 'normal' | 'surname'; | ||
useCustomConfig?: boolean; | ||
}) => string; | ||
declare const getPinyin: GetPinYin; | ||
@@ -11,0 +14,0 @@ /** |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2513948
94282
249