pinyin-pro
Advanced tools
Comparing version 3.8.1 to 3.8.2
@@ -0,1 +1,10 @@ | ||
## 3.8.2 | ||
当前版本: 3.8.1 -> 3.8.2 | ||
- 修复问题 | ||
- 修复双字节 unicode 编码的文字返回默认值不正确的问题 | ||
点击查看 [版本更新文档](./CHANGELOG.md) | ||
## 3.8.1 | ||
@@ -2,0 +11,0 @@ |
@@ -9,3 +9,2 @@ import INITIAL_LIST from '../data/initial'; | ||
import { getCustomDict } from './custom'; | ||
import { getStringLength } from './utils'; | ||
const dictArr = [{}, {}, DICT2, DICT3, DICT4, DICT5]; | ||
@@ -64,3 +63,3 @@ | ||
if (length === 1) { | ||
for (let i = 0; i < getStringLength(word); i++) { | ||
for (let i = 0; i < word.length; i++) { | ||
const result = getSingleWordPinyin(word[i]); | ||
@@ -85,8 +84,8 @@ const curIsChinese = result !== word[i]; | ||
const left_pinyin = left_word | ||
? `${getPinyin(left_word, getStringLength(left_word), { nonZh })} ` | ||
? `${getPinyin(left_word, left_word.length, { nonZh })} ` | ||
: ''; | ||
// 取出该词后右边拼音 | ||
const right_word = word.slice(index + getStringLength(key)); | ||
const right_word = word.slice(index + key.length); | ||
const right_pinyin = right_word | ||
? ` ${getPinyin(right_word, getStringLength(right_word), { nonZh })}` | ||
? ` ${getPinyin(right_word, right_word.length, { nonZh })}` | ||
: ''; | ||
@@ -124,3 +123,3 @@ // 取出的词的拼音 | ||
const left_pinyin = left_word | ||
? `${getPinyin(left_word, getStringLength(left_word), { | ||
? `${getPinyin(left_word, left_word.length, { | ||
mode: 'surname', | ||
@@ -131,5 +130,5 @@ nonZh, | ||
// 取出该词后右边拼音 | ||
const right_word = word.slice(index + getStringLength(key)); | ||
const right_word = word.slice(index + key.length); | ||
const right_pinyin = right_word | ||
? ` ${getPinyin(right_word, getStringLength(right_word), { | ||
? ` ${getPinyin(right_word, right_word.length, { | ||
mode: 'surname', | ||
@@ -145,3 +144,3 @@ nonZh, | ||
// 若姓氏表中的词均为匹配成功,则使用常规匹配 | ||
return getPinyin(word, getStringLength(word), { nonZh }); | ||
return getPinyin(word, word.length, { nonZh }); | ||
}; | ||
@@ -174,3 +173,3 @@ | ||
const left_pinyin = left_word | ||
? `${getPinyin(left_word, getStringLength(left_word), { | ||
? `${getPinyin(left_word, left_word.length, { | ||
mode, | ||
@@ -182,5 +181,5 @@ useCustomConfig: true, | ||
// 取出该词后右边拼音 | ||
const right_word = word.slice(index + getStringLength(key)); | ||
const right_word = word.slice(index + key.length); | ||
const right_pinyin = right_word | ||
? ` ${getPinyin(right_word, getStringLength(right_word), { | ||
? ` ${getPinyin(right_word, right_word.length, { | ||
mode, | ||
@@ -196,3 +195,3 @@ useCustomConfig: true, | ||
} | ||
return getPinyin(word, getStringLength(word), { mode, nonZh }); | ||
return getPinyin(word, word.length, { mode, nonZh }); | ||
}; | ||
@@ -245,3 +244,3 @@ | ||
if (_pinyin.startsWith(_initial)) { | ||
const _final = _pinyin.slice(getStringLength(_initial)); | ||
const _final = _pinyin.slice(_initial.length); | ||
initial_arr.push(_initial); | ||
@@ -248,0 +247,0 @@ final_arr.push(_final); |
import { pinyin as _pinyin } from './pinyin'; | ||
import { getStringLength } from './utils'; | ||
@@ -13,3 +12,3 @@ /** | ||
let currentPinyin = pinyin; | ||
for (let i = 0; i < getStringLength(words); i++) { | ||
for (let i = 0; i < words.length; i++) { | ||
// 当前字的多音字拼音 | ||
@@ -42,3 +41,3 @@ const ps = _pinyin(words[i], { | ||
let length = 0; | ||
for (let i = 0; i < getStringLength(pinyin1); i++) { | ||
for (let i = 0; i < pinyin1.length; i++) { | ||
if (pinyin1[i] === pinyin2[length]) { | ||
@@ -45,0 +44,0 @@ length++; |
@@ -10,3 +10,3 @@ import { | ||
} from './handle'; | ||
import { getStringLength } from './utils'; | ||
import { getStringLength, isZhChar } from './utils'; | ||
import { hasCustomConfig } from './custom'; | ||
@@ -47,3 +47,3 @@ | ||
* @param {string} word 要转换的汉语字符串 | ||
* @param {OptionsReturnString} options 配置项 | ||
* @param {OptionsReturnString=} options 配置项 | ||
* @return {string | string[]} options 配置项中的 type 为 string 时,返回字符串,中间用空格隔开;为 array 时,返回拼音字符串数组 | ||
@@ -56,3 +56,3 @@ */ | ||
* @param {string} word 要转换的汉语字符串 | ||
* @param {OptionsReturnArray} options 配置项 | ||
* @param {OptionsReturnArray=} options 配置项 | ||
* @return {string | string[]} options 配置项中的 type 为 string 时,返回字符串,中间用空格隔开;为 array 时,返回拼音字符串数组 | ||
@@ -65,3 +65,3 @@ */ | ||
* @param {string} word 要转换的汉语字符串 | ||
* @param {CompleteOptions} options 配置项 | ||
* @param {CompleteOptions=} options 配置项 | ||
* @return {string | string[]} options 配置项中的 type 为 string 时,返回字符串,中间用空格隔开;为 array 时,返回拼音字符串数组 | ||
@@ -77,6 +77,5 @@ */ | ||
let str = ''; | ||
for (let i = 0; i < getStringLength(word); i++) { | ||
for (let i = 0; i < word.length; i++) { | ||
const char = word[i]; | ||
let code = char.charCodeAt(0); | ||
if (code >= 19968 && code <= 40869) { | ||
if (isZhChar(char)) { | ||
str += char; | ||
@@ -92,9 +91,81 @@ } | ||
// 获取原始拼音 | ||
let pinyin = getPinyin(word, getStringLength(word), { | ||
mode: options.mode || 'normal', | ||
nonZh: options.nonZh || 'spaced', | ||
useCustomConfig: hasCustomConfig(), | ||
}); | ||
let pinyin = ''; | ||
if (options.removeNonZh || options.nonZh === 'removed') { | ||
pinyin = getPinyin(word, getStringLength(word), { | ||
mode: options.mode || 'normal', | ||
nonZh: options.nonZh, | ||
useCustomConfig: hasCustomConfig(), | ||
}); | ||
} else { | ||
// 针对双精度unicode编码字符处理 | ||
let i = 0; | ||
let lastIndex = 0; | ||
let items: { | ||
val: string; | ||
isDouble: boolean; | ||
firstNonZh?: boolean; | ||
lastNonZh?: boolean; | ||
}[] = []; | ||
while (i < word.length) { | ||
const currentWord = word.substring(i, i + 2); | ||
if (getStringLength(currentWord) !== currentWord.length) { | ||
// 双精度unicode编码字符 | ||
if (lastIndex !== i) { | ||
const leftWord = word.substring(lastIndex, i); | ||
const leftPinyin = getPinyin(leftWord, getStringLength(leftWord), { | ||
mode: options.mode || 'normal', | ||
nonZh: options.nonZh || 'spaced', | ||
useCustomConfig: hasCustomConfig(), | ||
}); | ||
items.push({ | ||
val: leftPinyin, | ||
isDouble: false, | ||
firstNonZh: !isZhChar(leftWord[0]), | ||
lastNonZh: !isZhChar(leftWord[leftWord.length - 1]), | ||
}); | ||
} | ||
items.push({ val: currentWord, isDouble: true }); | ||
lastIndex = i + 2; | ||
i = i + 2; | ||
} else { | ||
i++; | ||
} | ||
} | ||
if (lastIndex !== i) { | ||
const remainedWord = word.substring(lastIndex, i); | ||
const remainedPinyin = getPinyin( | ||
remainedWord, | ||
getStringLength(remainedWord), | ||
{ | ||
mode: options.mode || 'normal', | ||
nonZh: options.nonZh || 'spaced', | ||
useCustomConfig: hasCustomConfig(), | ||
} | ||
); | ||
items.push({ | ||
val: remainedPinyin, | ||
isDouble: false, | ||
firstNonZh: !isZhChar(remainedWord[0]), | ||
lastNonZh: !isZhChar(remainedWord[remainedWord.length - 1]), | ||
}); | ||
} | ||
if (options.nonZh === 'consecutive') { | ||
for (let i = 0; i < items.length; i++) { | ||
if (i === 0) { | ||
pinyin += items[0].val; | ||
} else { | ||
// 当前字符起始是否为非中文 | ||
const currentNonZh = items[i].isDouble || items[i].firstNonZh; | ||
// 上一个字符结束是否为非中文 | ||
const preNonZh = items[i - 1].isDouble || items[i - 1].lastNonZh; | ||
pinyin = | ||
pinyin + (currentNonZh && preNonZh ? '' : ' ') + items[i].val; | ||
} | ||
} | ||
} else { | ||
pinyin = items.map((item) => item.val).join(' '); | ||
} | ||
} | ||
// 对multiple进行处理 | ||
@@ -101,0 +172,0 @@ if (getStringLength(word) === 1 && options.multiple) { |
@@ -6,1 +6,6 @@ var regex = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; | ||
} | ||
export function isZhChar(char: string) { | ||
let code = char.charCodeAt(0); | ||
return code >= 19968 && code <= 40869; | ||
} |
{ | ||
"name": "pinyin-pro", | ||
"version": "3.8.1", | ||
"version": "3.8.2", | ||
"description": "汉字转拼音库。获取中文拼音、韵母、声母、声调、首字母,支持拼音匹配", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.cjs.js", |
@@ -26,3 +26,3 @@ [![pinyin-pro Logo](https://i.ibb.co/26fJ5vF/pinyin-logo.png)](https://github.com/zh-lx/pinyin-pro) | ||
当前版本: 3.8.0 -> 3.8.1 | ||
当前版本: 3.8.1 -> 3.8.2 | ||
@@ -29,0 +29,0 @@ - 修复问题 |
@@ -415,2 +415,7 @@ const { pinyin, customPinyin, match } = require('../dist/index.cjs.js'); | ||
}); | ||
it('match9', () => { | ||
const result = match('𧒽测试', 'cs'); | ||
expect(result).to.deep.equal([2, 3]); | ||
}); | ||
}); | ||
@@ -513,2 +518,21 @@ | ||
}); | ||
it('double unicode', () => { | ||
const result1 = pinyin('𧒽'); | ||
expect(result1).to.be.equal('𧒽'); | ||
const result2 = pinyin('𧒽测试'); | ||
expect(result2).to.be.equal('𧒽 cè shì'); | ||
const result3 = pinyin('𧒽测试𧒽测试', {}); | ||
expect(result3).to.be.equal('𧒽 cè shì 𧒽 cè shì'); | ||
const result4 = pinyin('𧒽测试', { nonZh: 'consecutive' }); | ||
expect(result4).to.be.equal('𧒽 cè shì'); | ||
const result6 = pinyin('测试a𧒽𧒽a测试a𧒽𧒽a测试', { | ||
nonZh: 'consecutive', | ||
}); | ||
expect(result6).to.be.equal('cè shì a𧒽𧒽a cè shì a𧒽𧒽a cè shì'); | ||
}); | ||
}); |
@@ -18,3 +18,3 @@ interface BasicOptions { | ||
* @param {string} word 要转换的汉语字符串 | ||
* @param {OptionsReturnString} options 配置项 | ||
* @param {OptionsReturnString=} options 配置项 | ||
* @return {string | string[]} options 配置项中的 type 为 string 时,返回字符串,中间用空格隔开;为 array 时,返回拼音字符串数组 | ||
@@ -26,3 +26,3 @@ */ | ||
* @param {string} word 要转换的汉语字符串 | ||
* @param {OptionsReturnArray} options 配置项 | ||
* @param {OptionsReturnArray=} options 配置项 | ||
* @return {string | string[]} options 配置项中的 type 为 string 时,返回字符串,中间用空格隔开;为 array 时,返回拼音字符串数组 | ||
@@ -29,0 +29,0 @@ */ |
export declare function getStringLength(string: string): number; | ||
export declare function isZhChar(char: string): boolean; |
Sorry, the diff of this file is too big to display
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
2276324
54818