feat(3.4.0): 新增姓氏模式和自定义拼音

This commit is contained in:
zh-lx
2021-12-16 00:53:36 +08:00
parent f039e51008
commit c965b09afe
24 changed files with 879 additions and 67 deletions

View File

@@ -1,3 +1,20 @@
## 3.4.0
当前版本: 3.3.1 -> 3.4.0
- 新增功能
- 支持姓氏模式拼音识别,具体用法见[姓氏模式](#surname)
- 支持自定义拼音功能,具体用法见[自定义拼音](#custom)
- 问题修复
修复文本过长时堆栈溢出问题
- 字音校准
- 褚: 'zhǔ' -> 'chǔ zhǔ'
- 俞: 'yù' -> 'yú yù'
- 臧: 'zàng' -> 'zāng'
- 贲: 'bì' -> 'bēn bì'
- 莘: 'xīn' -> 'shēn xīn'
- 郦: 'zhí' -> 'lì zhí'
## 3.3.1
当前版本: 3.3.0 -> 3.3.1

View File

@@ -17,12 +17,17 @@
- 获取拼音首字母
- 获取音调
- 获取多音字的多种拼音
- 支持人名姓氏模式
- 支持自定义拼音
- 支持字符串和数组两种输出形式
## 版本更新
当前版本: 3.3.0 -> 3.3.1
当前版本: 3.3.1 -> 3.4.0
- 新增功能
- 支持姓氏模式拼音识别,具体用法见[姓氏模式](#surname)
- 支持自定义拼音功能,具体用法见[自定义拼音](#custom)
- 问题修复
修复文本过长时堆栈溢出问题
- 字音校准
@@ -91,6 +96,7 @@ pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'
| toneType | 音调输出形式(拼音符号 / 数字 / 不加音调) | string | symbol / num / none | symbol |
| type | 输出结果类型(字符串/数组) | string | string / array | string |
| multiple | 输出多音字全部拼音(仅在 word 为长度为 1 的汉字字符串时生效) | boolean | true / false | false |
| mode | 拼音查找的模式(常规模式 / 姓氏模式) | string | normal / surname | normal |
## 使用示例
@@ -182,6 +188,33 @@ pinyin('好', { multiple: true }); // 'hǎo hào'
pinyin('好', { multiple: true, type: 'array' }); // ["hǎo", "hào"]
```
### <a id="surname">姓氏模式</a>
通过设置 `mode: 'surname'` 开启姓氏模式后,匹配到百家姓中的姓氏优先输出姓氏拼音
```javascript
import { pinyin } from 'pinyin-pro';
// 不开启姓氏模式
pinyin('我叫曾小贤'); // 'wǒ jiào céng xiǎo xián'
// 开启姓氏模式
pinyin('我叫曾小贤', { mode: 'surname' }); // 'wǒ jiào zēng xiǎo xián'
```
### <a id="custom">自定义拼音</a>
包内部导出了 `customPinyin` 方法,支持用户自定义设置词句拼音,当中文中匹配用户自己定义的词句拼音时,优先使用用户自定义的词句拼音
```javascript
import { pinyin, customPinyin } from 'pinyin-pro';
customPinyin({
干一行行一行: 'gàn yī háng xíng yī háng',
});
pinyin('干一行行一行'); // 'gàn yī háng xíng yī háng'
```
## 贡献与反馈
参与开源贡献请参照 [pinyin-pro 贡献](./docs/contribute.md)

View File

@@ -1,32 +1,476 @@
const Surnames = {
: 'huà',
: 'yuè',
: 'miào',
: 'xiè',
: 'yīng',
: 'shàn',
: 'yū',
: 'kuí',
: 'nìng',
: 'qiú',
: 'tǒu',
: 'bó',
: 'nài',
: 'sāng',
: 'yān',
: 'quē',
: 'zǐ',
: 'nā',
: 'zēng',
: 'xiàng',
: 'zhā',
const Surnames: { [key: string]: string } = {
: 'nán gōng',
: 'dì wǔ',
: 'mò qí',
: 'sī mǎ',
: 'shàng guān',
: 'ōu yáng',
: 'xià hóu',
: 'zhū gě',
: 'wén rén',
: 'dōng fāng',
: 'hè lián',
: 'huáng pǔ',
: 'yù chí',
: 'gōng yáng',
: 'tán tái',
: 'gōng yě',
: 'zōng zhèng',
: 'pú yáng',
: 'chún yú',
: 'chán yú',
: 'tài shū',
: 'shēn tú',
: 'gōng sūn',
: 'zhòng sūn',
: 'xuān yuán',
: 'líng hú',
: 'zhōng lí',
: 'yǔ wén',
: 'zhǎng sūn',
: 'mù róng',
: 'xiān yú',
: 'lǘ qiū',
: 'sī tú',
: 'sī kōng',
: 'qí guān',
: 'sī kòu',
: 'zhǎng dū',
: 'zǐ jū',
: 'zhuān sūn',
: 'duān mù',
: 'wū mǎ',
西: 'gōng xī',
: 'qī diāo',
: 'yuè zhèng',
: 'rǎng sì',
: 'gōng liáng',
: 'tuò bá',
: 'jiá gǔ',
: 'zǎi fǔ',
: 'gǔ liáng',
: 'duàn gān',
: 'bǎi lǐ',
: 'dōng guō',
: 'nán mén',
: 'hū yán',
: 'yáng shé',
: 'liáng qiū',
: 'zuǒ qiū',
: 'dōng mén',
西: 'xī mén',
: 'zhào',
: 'qián',
: 'sūn',
: 'lǐ',
: 'zhōu',
: 'wú',
: 'zhèng',
: 'wáng',
: 'féng',
: 'chén',
: 'chǔ',
: 'wèi',
: 'jiǎng',
: 'shěn',
: 'hán',
: 'yáng',
: 'zhū',
: 'qín',
: 'yóu',
: 'xǔ',
: 'hé',
: 'lǚ',
: 'shī',
: 'zhāng',
: 'kǒng',
: 'cáo',
: 'yán',
: 'huà',
: 'jīn',
: 'wèi',
: 'táo',
: 'jiāng',
: 'qī',
: 'xiè',
: 'zōu',
: 'yù',
: 'bǎi',
: 'shuǐ',
: 'dòu',
: 'zhāng',
: 'yún',
: 'sū',
: 'pān',
: 'gě',
: 'xī',
: 'fàn',
: 'péng',
: 'láng',
: 'lǔ',
: 'wéi',
: 'chāng',
: 'mǎ',
: 'miáo',
: 'fèng',
: 'huā',
: 'fāng',
: 'yú',
: 'rèn',
: 'yuán',
: 'liǔ',
: 'fēng',
: 'bào',
: 'shǐ',
: 'táng',
: 'fèi',
: 'lián',
: 'cén',
: 'xuē',
: 'léi',
: 'hè',
: 'ní',
: 'tāng',
: 'téng',
: 'yīn',
: 'luó',
: 'bì',
: 'hǎo',
: 'wū',
: 'ān',
: 'cháng',
: 'yuè',
: 'yú',
: 'shí',
: 'fù',
: 'pí',
: 'biàn',
: 'qí',
: 'kāng',
: 'wǔ',
: 'yú',
: 'yuán',
: 'bǔ',
: 'gù',
: 'mèng',
: 'píng',
: 'huáng',
: 'hé',
: 'mù',
: 'xiāo',
: 'yǐn',
: 'yáo',
: 'shào',
: 'zhàn',
: 'wāng',
: 'qí',
: 'máo',
: 'yǔ',
: 'dí',
: 'mǐ',
: 'bèi',
: 'míng',
: 'zāng',
: 'jì',
: 'fú',
: 'chéng',
: 'dài',
: 'tán',
: 'sòng',
: 'máo',
: 'páng',
: 'xióng',
: 'jì',
: 'shū',
: 'qū',
: 'xiàng',
: 'zhù',
: 'dǒng',
: 'liáng',
: 'dù',
: 'ruǎn',
: 'lán',
: 'mǐn',
: 'xí',
: 'jì',
: 'má',
: 'qiáng',
: 'jiǎ',
: 'lù',
: 'lóu',
: 'wēi',
: 'jiāng',
: 'tóng',
: 'yán',
: 'guō',
: 'méi',
: 'shèng',
: 'lín',
: 'diāo',
: 'zhōng',
: 'xú',
: 'qiū',
: 'luò',
: 'gāo',
: 'xià',
: 'cài',
: 'tián',
: 'fán',
: 'hú',
: 'líng',
: 'huò',
: 'yú',
: 'wàn',
: 'zhī',
: 'kē',
: 'zǎn',
: 'guǎn',
: 'lú',
: 'mò',
: 'jīng',
: 'fáng',
: 'qiú',
: 'miào',
: 'gān',
: 'xiè',
: 'yīng',
: 'zōng',
: 'dīng',
: 'xuān',
: 'bēn',
: 'dèng',
: 'yù',
: 'shàn',
: 'háng',
: 'hóng',
: 'bāo',
: 'zhū',
: 'zuǒ',
: 'shí',
: 'cuī',
: 'jí',
: 'niǔ',
: 'gōng',
: 'chéng',
: 'jī',
: 'xíng',
: 'huá',
: 'péi',
: 'lù',
: 'róng',
: 'wēng',
: 'xún',
: 'yáng',
: 'yū',
: 'huì',
: 'zhēn',
: 'qū',
: 'jiā',
: 'fēng',
: 'ruì',
羿: 'yì',
: 'chǔ',
: 'jìn',
: 'jí',
: 'bǐng',
: 'mí',
: 'sōng',
: 'jǐng',
: 'duàn',
: 'fù',
: 'wū',
: 'wū',
: 'jiāo',
: 'bā',
: 'gōng',
: 'mù',
: 'kuí',
: 'shān',
: 'gǔ',
: 'chē',
: 'hóu',
: 'mì',
: 'péng',
: 'quán',
: 'xī',
: 'bān',
: 'yǎng',
: 'qiū',
: 'zhòng',
: 'yī',
: 'gōng',
: 'nìng',
: 'qiú',
: 'luán',
: 'bào',
: 'gān',
: 'tǒu',
: 'lì',
: 'róng',
: 'zǔ',
: 'wǔ',
: 'fú',
: 'liú',
: 'jǐng',
: 'zhān',
: 'shù',
: 'lóng',
: 'yè',
: 'xìng',
: 'sī',
: 'sháo',
: 'gào',
: 'lí',
: 'jì',
: 'bó',
: 'yìn',
宿: 'sù',
: 'bái',
怀: 'huái',
: 'pú',
: 'tái',
: 'cóng',
: 'è',
: 'suǒ',
: 'xián',
: 'jí',
: 'lài',
: 'zhuó',
: 'lìn',
: 'tú',
: 'méng',
: 'chí',
: 'qiáo',
: 'yīn',
: 'yù',
: 'xū',
: 'nài',
: 'cāng',
: 'shuāng',
: 'wén',
: 'shēn',
: 'dǎng',
: 'zhái',
: 'tán',
: 'gòng',
: 'láo',
: 'páng',
: 'jī',
: 'shēn',
: 'fú',
: 'dǔ',
: 'rǎn',
: 'zǎi',
: 'lì',
: 'yōng',
: 'xì',
: 'qú',
: 'sāng',
: 'guì',
: 'pú',
: 'niú',
寿: 'shòu',
: 'tōng',
: 'biān',
: 'hù',
: 'yān',
: 'jì',
: 'jiá',
: 'pǔ',
: 'shàng',
: 'nóng',
: 'wēn',
: 'bié',
: 'zhuāng',
: 'yàn',
: 'chái',
: 'qú',
: 'yán',
: 'chōng',
: 'mù',
: 'lián',
: 'rú',
: 'xí',
: 'huàn',
: 'ài',
: 'yú',
: 'róng',
: 'xiàng',
: 'gǔ',
: 'yì',
: 'shèn',
: 'gē',
: 'liào',
: 'yǔ',
: 'zhōng',
: 'jì',
: 'jū',
: 'héng',
: 'bù',
: 'dū',
: 'gěng',
: 'mǎn',
: 'hóng',
: 'kuāng',
: 'guó',
: 'wén',
: 'kòu',
广: 'guǎng',
: 'lù',
: 'quē',
: 'dōng',
: 'ōu',
: 'shū',
: 'wò',
: 'lì',
: 'wèi',
: 'yuè',
: 'kuí',
: 'lóng',
: 'shī',
: 'gǒng',
: 'shè',
: 'niè',
: 'cháo',
: 'gōu',
: 'áo',
: 'róng',
: 'lěng',
: 'zǐ',
: 'xīn',
: 'kàn',
: 'nā',
: 'jiǎn',
: 'ráo',
: 'kōng',
: 'zēng',
: 'mǔ',
: 'shā',
: 'niè',
: 'yǎng',
: 'jū',
: 'xū',
: 'fēng',
: 'cháo',
: 'guān',
: 'kuǎi',
: 'xiàng',
: 'zhā',
: 'hòu',
: 'jīng',
: 'hóng',
: 'yóu',
: 'zhú',
: 'quán',
: 'lù',
: 'gài',
: 'yì',
: 'huán',
: 'gōng',
: 'móu',
: 'hǎ',
: 'yán',
: 'fú',
};
export default Surnames;

2
dist/index.js vendored
View File

File diff suppressed because one or more lines are too long

2
dist/pinyin-pro.js vendored
View File

File diff suppressed because one or more lines are too long

226
docs/3.4.x.md Normal file
View File

@@ -0,0 +1,226 @@
[![pinyin-pro Logo](https://i.ibb.co/26fJ5vF/pinyin-logo.png)](https://github.com/zh-lx/pinyin-pro)
[![NPM version](https://img.shields.io/npm/v/pinyin-pro.svg)](https://www.npmjs.com/package/pinyin-pro)
[![GITHUB star](https://img.shields.io/github/stars/zh-lx/pinyin-pro.svg)](https://github.com/zh-lx/pinyin-pro)
[![travis-build](https://travis-ci.com/zh-lx/pinyin-pro.svg?branch=main)](https://travis-ci.com/github/zh-lx/pinyin-pro)
[![NPM Downloads](https://img.shields.io/npm/dm/pinyin-pro.svg)](https://npmcharts.com/compare/pinyin-pro?minimal=true)
[![Coverage Status](https://coveralls.io/repos/github/zh-lx/pinyin-pro/badge.svg?branch=main)](https://coveralls.io/github/zh-lx/pinyin-pro?branch=main)
[![MIT-license](https://img.shields.io/npm/l/pinyin-pro.svg)](https://opensource.org/licenses/MIT)
[![GITHUB-language](https://img.shields.io/github/languages/top/zh-lx/pinyin-pro.svg)](https://github.com/zh-lx/pinyin-pro)
## 特色功能
- 支持汉字、词语、句子多种格式输入获取
- 获取拼音
- 获取声母
- 获取韵母
- 获取拼音首字母
- 获取音调
- 获取多音字的多种拼音
- 支持人名姓氏模式
- 支持自定义拼音
- 支持字符串和数组两种输出形式
## 版本更新
当前版本: 3.3.1 -> 3.4.0
- 新增功能
- 支持姓氏模式拼音识别,具体用法见[姓氏模式](#surname)
- 支持自定义拼音功能,具体用法见[自定义拼音](#custom)
- 问题修复
修复文本过长时堆栈溢出问题
- 字音校准
- 褚: 'zhǔ' -> 'chǔ zhǔ'
- 俞: 'yù' -> 'yú yù'
- 臧: 'zàng' -> 'zāng'
- 贲: 'bì' -> 'bēn bì'
- 莘: 'xīn' -> 'shēn xīn'
- 郦: 'zhí' -> 'lì zhí'
点击查看 [版本更新文档](./CHANGELOG.md)
## 安装
npm 安装
```
npm install pinyin-pro
```
yarn 安装
```
yarn add pinyin-pro
```
## 引入
浏览器 script 引入:
```html
<!--引入某个版本如3.3.0版本-->
<!-- <script src="https://cdn.jsdelivr.net/gh/zh-lx/pinyin-pro@3.3.0/dist/pinyin-pro.js"></script> -->
<!--引入最新版本-->
<script src="https://cdn.jsdelivr.net/gh/zh-lx/pinyin-pro@latest/dist/pinyin-pro.js"></script>
<script>
var { pinyin } = pinyinPro;
pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'
</script>
```
ESModule 引入:
```javascript
import { pinyin } from 'pinyin-pro';
pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'
```
commonjs 引入:
```javascript
const { pinyin } = require('pinyin-pro');
pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'
```
## 参数
`pinyin(word, options)` 接收两个参数<br>
- <b>word</b>必填。String 类型,需要转化为拼音的中文
- <b>options</b>可选。Object 类型用于配置各种输出形式options 的键值配置如下:
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
| -------- | ------------------------------------------------------------- | ------- | -------------------------------------- | ------ |
| pattern | 输出的结果的信息(拼音 / 声母 / 韵母 / 音调 / 首字母) | string | pinyin / initial / final / num / first | pinyin |
| toneType | 音调输出形式(拼音符号 / 数字 / 不加音调) | string | symbol / num / none | symbol |
| type | 输出结果类型(字符串/数组) | string | string / array | string |
| multiple | 输出多音字全部拼音(仅在 word 为长度为 1 的汉字字符串时生效) | boolean | true / false | false |
| mode | 拼音查找的模式(常规模式 / 姓氏模式) | string | normal / surname | normal |
## 使用示例
### 获取拼音
```js
import { pinyin } from 'pinyin-pro';
// 获取带音调拼音
pinyin('汉语拼音'); // 'hàn yǔ pīn yīn'
// 获取不带声调的拼音
pinyin('汉语拼音', { toneType: 'none' }); // 'han yu pin yin'
// 获取声调转换为数字后缀的拼音
pinyin('汉语拼音', { toneType: 'num' }); // 'han4 yu3 pin1 yin1'
// 获取数组形式带音调拼音
pinyin('汉语拼音', { type: 'array' }); // ["hàn", "yǔ", "pīn", "yīn"]
// 获取数组形式不带声调的拼音
pinyin('汉语拼音', { toneType: 'none', type: 'array' }); // ["han", "yu", "pin", "yin"]
// 获取数组形式声调转换为数字后缀的拼音
pinyin('汉语拼音', { toneType: 'num', type: 'array' }); // ["han4", "yu3", "pin1", "yin1"]
```
### 获取声母
```js
import { pinyin } from 'pinyin-pro';
// 获取声母
pinyin('汉语拼音', { pattern: 'initial' }); // 'h y p y'
// 获取数组形式声母
pinyin('汉语拼音', { pattern: 'initial', type: 'array' }); // ["h", "y", "p", "y"]
```
### 获取韵母
```js
import { pinyin } from 'pinyin-pro';
// 获取带音调韵母
pinyin('汉语拼音', { pattern: 'final' }); // 'àn ǔ īn īn'
// 获取不带音调韵母
pinyin('汉语拼音', { pattern: 'final', toneType: 'none' }); // 'an u in in'
// 获取音调为数字的韵母
pinyin('汉语拼音', { pattern: 'final', toneType: 'num' }); // 'an4 u3 in1 in1'
// 获取数组形式带音调韵母
pinyin('汉语拼音', { pattern: 'final', type: 'array' }); // ["àn", "ǔ", "īn", "īn"]
// 获取数组形式不带音调韵母
pinyin('汉语拼音', { pattern: 'final', toneType: 'none', type: 'array' }); // ["an", "u", "in", "in"]
// 获取数组形式音调为数字的韵母
pinyin('汉语拼音', { pattern: 'final', toneType: 'num', type: 'array' }); // ['an4', 'u3', 'in1', 'in1']
```
### 获取音调
```js
import { pinyin } from 'pinyin-pro';
// 获取音调
pinyin('汉语拼音', { pattern: 'num' }); // '4 3 1 1'
// 获取数组形式音调
pinyin('汉语拼音', { pattern: 'num', type: 'array' }); // ["4", "3", "1", "1"]
```
### 获取拼音首字母
```js
import { pinyin } from 'pinyin-pro';
// 获取拼音首字母
pinyin('赵钱孙李额', { pattern: 'first' }); // 'z q s l é'
// 获取不带音调拼音首字母
pinyin('赵钱孙李额', { pattern: 'first', toneType: 'none' }); // 'z q s l e'
// 获取数组形式拼音首字母
pinyin('赵钱孙李额', { pattern: 'first', type: 'array' }); // ['z', 'q', 's', 'l', 'é']
// 获取数组形式不带音调拼音首字母
pinyin('赵钱孙李额', { pattern: 'first', toneType: 'none' type: 'array' }); // ['z', 'q', 's', 'l', 'e']
```
### 获取单个字的多音
只有单字可以获取到多音模式, 词语、句子无效。同样可以通过配置 options 选项获取数组形式、韵母等格式
```javascript
import { pinyin } from 'pinyin-pro';
// 获取多音
pinyin('好', { multiple: true }); // 'hǎo hào'
// 获取数组形式多音
pinyin('好', { multiple: true, type: 'array' }); // ["hǎo", "hào"]
```
### <a id="surname">姓氏模式</a>
通过设置 `mode: 'surname'` 开启姓氏模式后,匹配到百家姓中的姓氏优先输出姓氏拼音
```javascript
import { pinyin } from 'pinyin-pro';
// 不开启姓氏模式
pinyin('我叫曾小贤'); // 'wǒ jiào céng xiǎo xián'
// 开启姓氏模式
pinyin('我叫曾小贤', { mode: 'surname' }); // 'wǒ jiào zēng xiǎo xián'
```
### <a id="custom">自定义拼音</a>
包内部导出了 `customPinyin` 方法,支持用户自定义设置词句拼音,当中文中匹配用户自己定义的词句拼音时,优先使用用户自定义的词句拼音
```javascript
import { pinyin, customPinyin } from 'pinyin-pro';
customPinyin({
干一行行一行: 'gàn yī háng xíng yī háng',
});
pinyin('干一行行一行'); // 'gàn yī háng xíng yī háng'
```
## 贡献与反馈
参与开源贡献请参照 [pinyin-pro 贡献](./docs/contribute.md)
使用遇到问题或者需要功能支持欢迎提 issue。
交流及参与贡献欢迎加微信:
![wechat](https://i.ibb.co/VYXW19H/QQ-20210323221842.jpg)

View File

@@ -6,7 +6,7 @@ import _DICT5 from '../data/dict5';
import _INITIAL_LIST from '../data/initial';
const _dictArr = [{}, {}, _DICT2, _DICT3, _DICT4, _DICT5];
function customPinyin(config: { [key: string]: string } = {}) {
export function customPinyin(config: { [key: string]: string } = {}) {
for (let key in config) {
let pinyin = config[key];
if (key.length === 1) {

View File

@@ -1,10 +1,6 @@
// 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';
import INITIAL_LIST from '../data/initial';
// const dictArr = [{}, {}, DICT2, DICT3, DICT4, DICT5];
import Surnames from '../data/surname';
import { DICT1, dictArr } from './data';
/**
* @description: 获取单个字符的拼音
@@ -29,9 +25,14 @@ const getSingleWordPinyin: GetSingleWordPinyin = (word) => {
type GetPinYin = (
word: string,
length: number,
mode?: 'default' | 'surname'
mode?: 'normal' | 'surname'
) => string;
const getPinyin: GetPinYin = (word, length, mode = 'default') => {
const getPinyin: GetPinYin = (word, length, mode = 'normal') => {
// 如果是姓氏模式,则优先替换姓氏拼音
if (mode === 'surname') {
return getSurnamePinyin(word);
}
// 若length值大于5返回getPinyin(word, 5)
if (length > 5) {
return getPinyin(word, 5);
@@ -91,9 +92,27 @@ const getPinyin: GetPinYin = (word, length, mode = 'default') => {
* @param {string} pinyin
* @return {string}
*/
type GetSurnamePinyin = (pinyin: string) => string;
const getSurnamePinyin: GetSurnamePinyin = (pinyin) => {
return '';
type GetSurnamePinyin = (word: string) => string;
const getSurnamePinyin: GetSurnamePinyin = (word) => {
let _word = word;
for (let key in Surnames) {
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)} `
: '';
// 取出该词后右边拼音
const right_word = word.slice(index + key.length);
const right_pinyin = right_word
? ` ${getPinyin(right_word, right_word.length, 'surname')}`
: '';
// 取出的词的拼音
const word_pinyin = Surnames[key];
return `${left_pinyin}${word_pinyin}${right_pinyin}`;
}
}
return getPinyin(word, word.length);
};
/**

View File

@@ -1 +1,2 @@
export { pinyinFn as pinyin } from './pinyin';
export { pinyin } from './pinyin';
export { customPinyin } from './data';

View File

@@ -7,7 +7,6 @@ import {
getPinyinWithNum,
getFirstLetter,
} from './handle';
import Surnames from '../data/surname';
export { DICT1, dictArr } from './data';
const DEFAULT_OPTIONS: {
@@ -24,7 +23,7 @@ const DEFAULT_OPTIONS: {
mode: 'normal',
};
function pinyinFn(
function pinyin(
word: string,
options?: {
toneType?: 'symbol' | 'num' | 'none';
@@ -34,7 +33,7 @@ function pinyinFn(
mode?: 'normal' | 'surname';
}
): string;
function pinyinFn(
function pinyin(
word: string,
options?: {
toneType?: 'symbol' | 'num' | 'none';
@@ -45,7 +44,7 @@ function pinyinFn(
}
): string[];
function pinyinFn(word: string, options = DEFAULT_OPTIONS): string | string[] {
function pinyin(word: string, options = DEFAULT_OPTIONS): string | string[] {
// word传入类型错误时
if (typeof word !== 'string') {
return word;
@@ -56,7 +55,7 @@ function pinyinFn(word: string, options = DEFAULT_OPTIONS): string | string[] {
}
// 获取原始拼音
let pinyin = getPinyin(word, word.length);
let pinyin = getPinyin(word, word.length, options.mode);
// 对multiple进行处理
if (word.length === 1 && options.multiple) {
@@ -100,4 +99,4 @@ function pinyinFn(word: string, options = DEFAULT_OPTIONS): string | string[] {
return options.type === 'array' ? pinyin.split(' ') : pinyin;
}
export { pinyinFn };
export { pinyin };

View File

@@ -1,6 +1,6 @@
{
"name": "pinyin-pro",
"version": "3.3.1",
"version": "3.4.0",
"description": "汉字转拼音库。获取中文拼音、韵母、声母、声调、首字母等信息。",
"main": "./dist/index.js",
"typings": "./types/lib/index.d.ts",

View File

@@ -1,4 +1,4 @@
const { pinyin } = require('../dist/index');
const { pinyin, customPinyin } = require('../dist/index');
const expect = require('chai').expect;
describe('aggregate', () => {
@@ -244,6 +244,66 @@ describe('getNumOfTone', () => {
});
});
describe('surname', () => {
it('multiple surname1', () => {
const result = pinyin('万俟', { mode: 'surname' });
expect(result).to.be.equal('mò qí');
});
it('multiple surname2', () => {
const result = pinyin('我叫令狐冲', { mode: 'surname' });
expect(result).to.be.equal('wǒ jiào líng hú chōng');
});
});
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');
});
});
describe('pinyinFn', () => {
it('not string type', () => {
const result = pinyin(2222);

View File

@@ -1,7 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["ES2021"],
"module": "esnext",
"strict": true,
"jsx": "preserve",

View File

@@ -1 +1,2 @@
export declare const DICT1: string[];
declare const DICT1: string[];
export default DICT1;

View File

@@ -1,3 +1,4 @@
export declare const DICT2: {
declare const DICT2: {
[prop: string]: string;
};
export default DICT2;

View File

@@ -1,3 +1,4 @@
export declare const DICT3: {
declare const DICT3: {
[prop: string]: string;
};
export default DICT3;

View File

@@ -1,3 +1,4 @@
export declare const DICT4: {
declare const DICT4: {
[prop: string]: string;
};
export default DICT4;

View File

@@ -1,3 +1,4 @@
export declare const DICT5: {
declare const DICT5: {
[prop: string]: string;
};
export default DICT5;

View File

@@ -1 +1,2 @@
export declare const INITIAL_LIST: string[];
declare const INITIAL_LIST: string[];
export default INITIAL_LIST;

4
types/data/surname.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
declare const Surnames: {
[key: string]: string;
};
export default Surnames;

7
types/lib/data.d.ts vendored Normal file
View File

@@ -0,0 +1,7 @@
export declare function customPinyin(config?: {
[key: string]: string;
}): void;
export declare const DICT1: string[];
export declare const dictArr: {
[prop: string]: string;
}[];

13
types/lib/handle.d.ts vendored
View File

@@ -1,18 +1,11 @@
/**
* @description: 获取长文本字符串带符号音调的拼音
* @param {string} word
* @param {number} length
* @return {string}
*/
declare type GetTextPinyin = (word: string, length: number) => string;
declare const getTextPinyin: GetTextPinyin;
/**
* @description: 获取字符串带符号音调的拼音
* @param {string} word
* @param {number} length
* @param {string} mode
* @return {string}
*/
declare type GetPinYin = (word: string, length: number) => string;
declare type GetPinYin = (word: string, length: number, mode?: 'normal' | 'surname') => string;
declare const getPinyin: GetPinYin;
/**
* @description: 将带音调符号拼音转换为不带音调拼音
@@ -59,4 +52,4 @@ declare const getPinyinWithNum: GetPinyinWithNum;
*/
declare type GetFirstLetter = (pinyin: string) => string;
declare const getFirstLetter: GetFirstLetter;
export { getPinyin, getTextPinyin, getPinyinWithoutTone, getInitialAndFinal, getMultipleTone, getNumOfTone, getPinyinWithNum, getFirstLetter, };
export { getPinyin, getPinyinWithoutTone, getInitialAndFinal, getMultipleTone, getNumOfTone, getPinyinWithNum, getFirstLetter, };

View File

@@ -1 +1,2 @@
export { pinyinFn as pinyin } from './pinyin';
export { pinyin } from './pinyin';
export { customPinyin } from './data';

View File

@@ -1,13 +1,16 @@
declare function pinyinFn(word: string, options?: {
export { DICT1, dictArr } from './data';
declare function pinyin(word: string, options?: {
toneType?: 'symbol' | 'num' | 'none';
pattern?: 'pinyin' | 'initial' | 'final' | 'num' | 'first';
type?: 'string';
multiple?: boolean;
mode?: 'normal' | 'surname';
}): string;
declare function pinyinFn(word: string, options?: {
declare function pinyin(word: string, options?: {
toneType?: 'symbol' | 'num' | 'none';
pattern?: 'pinyin' | 'initial' | 'final' | 'num' | 'first';
type: 'array';
multiple?: boolean;
mode?: 'normal' | 'surname';
}): string[];
export { pinyinFn };
export { pinyin };