Skip to content

Commit

Permalink
feat: 更好的传入参数优化,方便用户使用
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Mar 2, 2022
1 parent f0bca72 commit f141794
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 402 deletions.
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,40 @@ pīnyīn, 汉字拼音转换工具。

via npm:

```bash
```shell
npm install pinyin --save
```

## 用法

开发者:

```js
var pinyin = require("pinyin");
```typescript
import pinyin from "pinyin";

console.log(pinyin("中心")); // [ [ 'zhōng' ], [ 'xīn' ] ]

console.log(pinyin("中心", {
heteronym: true // 启用多音字模式
heteronym: true, // 启用多音字模式
})); // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]

console.log(pinyin("中心", {
heteronym: true, // 启用多音字模式
segment: true // 启用分词,以解决多音字问题。
segment: true, // 启用分词,以解决多音字问题。默认不开启,使用 true 开启使用 nodejieba 分词库
})); // [ [ 'zhōng' ], [ 'xīn' ] ]

console.log(pinyin("中心", {
segment: "@node-rs/jieba", // 指定分词库,可以是 "nodejieba"、"segmentit"、"@node-rs/jieba"。
})); // [ [ 'zhōng' ], [ 'xīn' ] ]

console.log(pinyin("我喜欢你", {
segment: true, // 启用分词
group: true // 启用词组
segment: "segmentit", // 启用分词
group: true, // 启用词组
})); // [ [ 'wǒ' ], [ 'xǐhuān' ], [ 'nǐ' ] ]

console.log(pinyin("中心", {
style: pinyin.STYLE_INITIALS, // 设置拼音风格
heteronym: true
style: "initials", // 设置拼音风格
heteronym: true, // 即使有多音字,因为拼音风格选择,重复的也会合并。
})); // [ [ 'zh' ], [ 'x' ] ]
```

Expand All @@ -82,10 +90,13 @@ zhōng xīn
$ pinyin -h
```

## 类型

### IPinyinOptions

## API

### 方法 `<Array> pinyin(words[, options])`
### 方法 `<Array> pinyin(words: string[, options: IPinyinOptions])`

将传入的中文字符串 (words) 转换成拼音符号串。

Expand Down
2 changes: 1 addition & 1 deletion bin/pinyin
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (commander.args.length === 0) {
commander.help();
}

var pinyin = require("../");
var pinyin = require("../").default;
var options = {
style: pinyin["STYLE_" + (commander.style || "TONE").toUpperCase()],
heteronym: commander.heteronym || false,
Expand Down
13 changes: 7 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// export type { IPinyinOptions, IPinyinSegment, PINYIN_STYLE } from "./src/declare";
export * from "./src/pinyin";
export { pinyin as default } from "./src/pinyin";

// export default from "./src/pinyin";

export default function pinyin(hans: string) {
return hans;
}
export type {
IPinyinOptions,
IPinyinSegment,
IPinyinStyle,
} from "./src/declare";
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"doc:deploy": "npm run doc:build && cp CNAME docs-dist/dist && gh-pages -d docs-dist/dist",
"lint": "eslint ./lib/ ./bin/ ./tests/",
"test": "nyc --reporter=lcov mocha --timeout=5000 --inline-diffs ./tests/",
"test-local": "jest && npx ts-node test/index.benchmark.ts",
"test-local": "jest",
"debug": "npx ts-node tools/debug.ts"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export enum PINYIN_STYLE {
export enum ENUM_PINYIN_STYLE {
NORMAL = 0, // 普通风格,不带声调。
TONE = 1, // 标准风格,声调在韵母的第一个字母上。
TONE2 = 2, // 声调以数字形式在拼音之后,使用数字 0~4 标识。
Expand Down
17 changes: 12 additions & 5 deletions src/declare.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { PINYIN_STYLE } from "./constant";
import { ENUM_PINYIN_STYLE } from "./constant";

export type IPinyinStyle =
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | // 推荐使用小写,和输出的拼音一致
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | // 方便老版本迁移
0 | 1 | 2 | 5 | 3 | 4; // 兼容老版本

export type IPinyinSegment = "nodejieba" | "segmentit" | "@node-rs/jieba";

export interface IPinyinOptions {
style: PINYIN_STYLE; // 拼音模式
// 强类型,所有字段都有值。
export interface IPinyinAllOptions {
style: ENUM_PINYIN_STYLE; // 拼音模式
// 指定分词库。
// 为了兼容老版本,可以使用 boolean 类型指定是否开启分词,默认开启。
segment: IPinyinSegment | boolean;
Expand All @@ -13,8 +19,9 @@ export interface IPinyinOptions {
group: boolean;
}

export interface IPinyinOptionsUser {
style?: PINYIN_STYLE; // 拼音模式
// 给到用户的类型,大都可选。
export interface IPinyinOptions {
style?: IPinyinStyle; // 拼音模式
// 指定分词库。
// 为了兼容老版本,可以使用 boolean 类型指定是否开启分词,默认开启。
segment?: IPinyinSegment | boolean;
Expand Down
24 changes: 12 additions & 12 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PINYIN_STYLE } from "./constant";
import { ENUM_PINYIN_STYLE } from "./constant";
import PHONETIC_SYMBOL from "./phonetic-symbol"; // 带声调字符。

// 声母表。
Expand Down Expand Up @@ -26,35 +26,35 @@ function initials(py: string): string {
* 格式化拼音风格。
*
* @param {string} pinyin TONE 风格的拼音。
* @param {PINYIN_STYLE} style 目标转换的拼音风格。
* @param {ENUM_PINYIN_STYLE} style 目标转换的拼音风格。
* @return {string} 转换后的拼音。
*/
export function toFixed(pinyin: string, style: PINYIN_STYLE): string {
export function toFixed(pinyin: string, style: ENUM_PINYIN_STYLE): string {
let tone = ""; // 声调。
let first_letter: string;
let py: string;
switch(style){
case PINYIN_STYLE.INITIALS:
switch(style) {
case ENUM_PINYIN_STYLE.INITIALS:
return initials(pinyin);

case PINYIN_STYLE.FIRST_LETTER:
case ENUM_PINYIN_STYLE.FIRST_LETTER:
first_letter = pinyin.charAt(0);
if (PHONETIC_SYMBOL.hasOwnProperty(first_letter)) {
first_letter = PHONETIC_SYMBOL[first_letter].charAt(0);
}
return first_letter;

case PINYIN_STYLE.NORMAL:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1_phonetic: string){
case ENUM_PINYIN_STYLE.NORMAL:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1_phonetic: string) {
return PHONETIC_SYMBOL[$1_phonetic].replace(RE_TONE2, "$1");
});

case PINYIN_STYLE.TO3NE:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1_phonetic: string){
case ENUM_PINYIN_STYLE.TO3NE:
return pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1_phonetic: string) {
return PHONETIC_SYMBOL[$1_phonetic];
});

case PINYIN_STYLE.TONE2:
case ENUM_PINYIN_STYLE.TONE2:
py = pinyin.replace(RE_PHONETIC_SYMBOL, function($0: string, $1: string){
// 声调数值。
tone = PHONETIC_SYMBOL[$1].replace(RE_TONE2, "$2");
Expand All @@ -63,7 +63,7 @@ export function toFixed(pinyin: string, style: PINYIN_STYLE): string {
});
return py + tone;

case PINYIN_STYLE.TONE:
case ENUM_PINYIN_STYLE.TONE:
default:
return pinyin;
}
Expand Down
138 changes: 0 additions & 138 deletions src/index.ts

This file was deleted.

Loading

0 comments on commit f141794

Please sign in to comment.