New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

pinyin

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pinyin - npm Package Compare versions

Comparing version 3.0.0-alpha.0 to 3.0.0-alpha.1

lib/pinyin-web.d.ts

13

lib/declare.d.ts
import { ENUM_PINYIN_STYLE, ENUM_PINYIN_MODE } from "./constant";
export interface IPinyin {
(han: string, options?: IPinyinOptions): string[][];
compare: (a: string, b: string) => number;
compact: (arr: string[][]) => string[][];
STYLE_TONE: ENUM_PINYIN_STYLE;
STYLE_TONE2: ENUM_PINYIN_STYLE;
STYLE_TO3NE: ENUM_PINYIN_STYLE;
STYLE_NORMAL: ENUM_PINYIN_STYLE;
STYLE_INITIALS: ENUM_PINYIN_STYLE;
STYLE_FIRST_LETTER: ENUM_PINYIN_STYLE;
MODE_NORMAL: ENUM_PINYIN_MODE;
MODE_SURNAME: ENUM_PINYIN_MODE;
}
export declare type IPinyinStyle = "normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | // 推荐使用小写,和输出的拼音一致

@@ -3,0 +16,0 @@ "NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | // 方便老版本迁移

20

lib/pinyin.d.ts

@@ -1,15 +0,9 @@

import type { IPinyinOptions } from "./declare";
/**
* 拼音转换入口。
*/
export declare function pinyin(hans: string, options?: IPinyinOptions): string[][];
import PinyinBase from "./PinyinBase";
import type { IPinyinSegment } from "./declare";
export declare class Pinyin extends PinyinBase {
segment(hans: string, segmentType?: IPinyinSegment): string[];
}
export declare const pinyin: import("./declare").IPinyin;
export default pinyin;
/**
* 比较两个汉字转成拼音后的排序顺序,可以用作默认的拼音排序算法。
*
* @param {String} hanA 汉字字符串 A。
* @return {String} hanB 汉字字符串 B。
* @return {Number} 返回 -1,0,或 1。
*/
export declare function compare(hanA: string, hanB: string): number;
export declare const compare: (a: string, b: string) => number;
export { compact } from "./util";
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.compact = exports.compare = exports.pinyin = void 0;
var dict_zi_1 = __importDefault(require("./data/dict-zi")); // 单个汉字拼音数据。
var phrases_dict_1 = __importDefault(require("./data/phrases-dict")); // 词组拼音数据。
exports.compact = exports.compare = exports.pinyin = exports.Pinyin = void 0;
var PinyinBase_1 = __importStar(require("./PinyinBase"));
var segment_1 = require("./segment");
var format_1 = require("./format");
var surname_1 = __importDefault(require("./data/surname"));
var compound_surname_1 = __importDefault(require("./data/compound_surname"));
var Pinyin = /** @class */ (function (_super) {
__extends(Pinyin, _super);
function Pinyin() {
return _super !== null && _super.apply(this, arguments) || this;
}
Pinyin.prototype.segment = function (hans, segmentType) {
return (0, segment_1.segment)(hans, segmentType);
};
return Pinyin;
}(PinyinBase_1.default));
exports.Pinyin = Pinyin;
exports.pinyin = (0, PinyinBase_1.getPinyinInstance)(new Pinyin());
exports.default = exports.pinyin;
exports.compare = exports.pinyin.compare;
var util_1 = require("./util");
var constant_1 = require("./constant");
/**
* 拼音转换入口。
*/
function pinyin(hans, options) {
if (typeof hans !== "string") {
return [];
}
var opt = (0, util_1.convertUserOptions)(options);
var pys;
if (opt.mode === constant_1.ENUM_PINYIN_MODE.SURNAME) {
pys = surname_pinyin(hans, opt);
}
else {
// 因为分词结果有词性信息,结构不同,处理也不相同,所以需要分别处理。
if (opt.segment) {
// 分词加词性标注转换。
pys = segment_pinyin(hans, opt);
}
else {
// 单字拆分转换。连续的非中文字符作为一个词(原样输出,不转换成拼音)。
pys = normal_pinyin(hans, opt);
}
}
if (options === null || options === void 0 ? void 0 : options.compact) {
pys = (0, util_1.compact)(pys);
}
return pys;
}
exports.pinyin = pinyin;
exports.default = pinyin;
/**
* 不使用分词算法的拼音转换。
*/
function normal_pinyin(hans, options) {
var pys = [];
var nohans = "";
for (var i = 0, l = hans.length; i < l; i++) {
var words = hans[i];
var firstCharCode = words.charCodeAt(0);
if (dict_zi_1.default[firstCharCode]) {
// 处理前面的“非中文”部分。
if (nohans.length > 0) {
pys.push([nohans]);
nohans = ""; // 重置“非中文”缓存。
}
pys.push(single_pinyin(words, options));
}
else {
nohans += words;
}
}
// 清理最后的非中文字符串。
if (nohans.length > 0) {
pys.push([nohans]);
nohans = ""; // reset non-chinese words.
}
return pys;
}
/**
* 单字拼音转换。
* @param {String} han, 单个汉字
* @return {Array} 返回拼音列表,多音字会有多个拼音项。
*/
function single_pinyin(han, options) {
if (typeof han !== "string") {
return [];
}
if (han.length !== 1) {
return single_pinyin(han.charAt(0), options);
}
var hanCode = han.charCodeAt(0);
if (!dict_zi_1.default[hanCode]) {
return [han];
}
var pys = dict_zi_1.default[hanCode].split(",");
if (!options.heteronym) {
return [(0, format_1.toFixed)(pys[0], options.style)];
}
// 临时存储已存在的拼音,避免多音字拼音转换为非注音风格出现重复。
var py_cached = {};
var pinyins = [];
for (var i = 0, l = pys.length; i < l; i++) {
var py = (0, format_1.toFixed)(pys[i], options.style);
if ((0, util_1.hasKey)(py_cached, py)) {
continue;
}
py_cached[py] = py;
pinyins.push(py);
}
return pinyins;
}
/**
* 将文本分词,并转换成拼音。
*/
function segment_pinyin(hans, options) {
var phrases = (0, segment_1.segment)(hans, options.segment);
var pys = [];
var nohans = "";
for (var i = 0, l = phrases.length; i < l; i++) {
var words = phrases[i];
var firstCharCode = words.charCodeAt(0);
if (dict_zi_1.default[firstCharCode]) {
// ends of non-chinese words.
if (nohans.length > 0) {
pys.push([nohans]);
nohans = ""; // reset non-chinese words.
}
var newPys = words.length === 1
? normal_pinyin(words, options)
: phrases_pinyin(words, options);
if (options.group) {
pys.push(groupPhrases(newPys));
}
else {
pys = pys.concat(newPys);
}
}
else {
nohans += words;
}
}
// 清理最后的非中文字符串。
if (nohans.length > 0) {
pys.push([nohans]);
nohans = ""; // reset non-chinese words.
}
return pys;
}
/**
* 词语注音
* @param {String} phrases, 指定的词组。
* @param {Object} options, 选项。
* @return {Array}
*/
function phrases_pinyin(phrases, options) {
var py = [];
if ((0, util_1.hasKey)(phrases_dict_1.default, phrases)) {
//! copy pinyin result.
phrases_dict_1.default[phrases].forEach(function (item, idx) {
py[idx] = [];
if (options.heteronym) {
item.forEach(function (py_item, py_index) {
py[idx][py_index] = (0, format_1.toFixed)(py_item, options.style);
});
}
else {
py[idx][0] = (0, format_1.toFixed)(item[0], options.style);
}
});
}
else {
for (var i = 0, l = phrases.length; i < l; i++) {
py.push(single_pinyin(phrases[i], options));
}
}
return py;
}
function groupPhrases(phrases) {
if (phrases.length === 1) {
return phrases[0];
}
var grouped = (0, util_1.combo)(phrases);
return grouped;
}
// 姓名转成拼音
function surname_pinyin(hans, options) {
return compound_surname(hans, options);
}
// 复姓处理
function compound_surname(hans, options) {
var len = hans.length;
var prefixIndex = 0;
var result = [];
for (var i = 0; i < len; i++) {
var twowords = hans.substring(i, i + 2);
if ((0, util_1.hasKey)(compound_surname_1.default, twowords)) {
if (prefixIndex <= i - 1) {
result = result.concat(single_surname(hans.substring(prefixIndex, i), options));
}
result = result.concat(compound_surname_1.default[twowords]);
i = i + 2;
prefixIndex = i;
}
}
// 处理复姓后面剩余的部分。
result = result.concat(single_surname(hans.substring(prefixIndex, len), options));
return result;
}
// 单姓处理
function single_surname(hans, options) {
var result = [];
for (var i = 0, l = hans.length; i < l; i++) {
var word = hans.charAt(i);
if ((0, util_1.hasKey)(surname_1.default, word)) {
result = result.concat(surname_1.default[word]);
}
else {
result.push(single_pinyin(word, options));
}
}
return result;
}
/**
* 比较两个汉字转成拼音后的排序顺序,可以用作默认的拼音排序算法。
*
* @param {String} hanA 汉字字符串 A。
* @return {String} hanB 汉字字符串 B。
* @return {Number} 返回 -1,0,或 1。
*/
function compare(hanA, hanB) {
var pinyinA = pinyin(hanA);
var pinyinB = pinyin(hanB);
return String(pinyinA).localeCompare(String(pinyinB));
}
exports.compare = compare;
var util_2 = require("./util");
Object.defineProperty(exports, "compact", { enumerable: true, get: function () { return util_2.compact; } });
Object.defineProperty(exports, "compact", { enumerable: true, get: function () { return util_1.compact; } });
//# sourceMappingURL=pinyin.js.map

@@ -21,2 +21,3 @@ "use strict";

if (!nodeRsJiebaLoaded) {
nodeRsJiebaLoaded = true;
(0, jieba_1.load)();

@@ -23,0 +24,0 @@ }

{
"name": "pinyin",
"version": "3.0.0-alpha.0",
"version": "3.0.0-alpha.1",
"description": "汉语拼音转换工具。",
"main": "lib/pinyin.js",
"module": "esm/pinyin.js",
"browser": "lib/pinyin.js",
"main": "./lib/pinyin.js",
"module": "./esm/pinyin.js",
"browser": {
"./lib/pinyin.js": "./lib/pinyin-web.js",
"./esm/pinyin.js": "./esm/pinyin-web.js"
},
"bin": {

@@ -24,3 +27,3 @@ "pinyin": "./bin/pinyin"

"build": "tsc --downlevelIteration -p tsconfig-es5.json & tsc --downlevelIteration -p tsconfig.json",
"doc:build": "dumi build",
"doc:build": "rm -rf ./src/.umi && dumi build",
"doc:deploy": "npm run doc:build && cp CNAME docs-dist/dist && gh-pages -d docs-dist/dist",

@@ -27,0 +30,0 @@ "lint": "eslint ./src/ ./test/",

@@ -0,4 +1,3 @@

# pīnyīn for Han (汉字) (v3)
# pīnyīn for Han (汉字)
---

@@ -23,3 +22,3 @@

[中文文档](/)
[中文文档网站](/)

@@ -26,0 +25,0 @@ [中文 README](README.md)

@@ -1,2 +0,2 @@

# README
# README (v3)

@@ -25,3 +25,3 @@ pīnyīn, 汉字拼音转换工具。

[English Documention](/en-US/)
[English Documention Site](/en-US/)

@@ -50,3 +50,3 @@ [English README](README.en-US.md)

```bash
npm install pinyin --save
npm install pinyin@v3 --save
```

@@ -103,3 +103,54 @@

传入给 pinyin 方法的第二个参数的选项类型。
```typescript
export interface IPinyinOptions {
style?: IPinyinStyle; // 拼音输出形式
mode?: IPinyinMode, // 拼音模式
// 指定分词库。
// 为了兼容老版本,可以使用 boolean 类型指定是否开启分词,默认开启。
segment?: IPinyinSegment | boolean;
// 是否返回多音字
heteronym?: boolean;
// 是否分组词组拼音
group?: boolean;
compact?: boolean;
}
```
### IPinyinStyle
输出拼音格式。可以从直接使用以下字符串或数字,也兼容 v2 版本中 `pinyin.STYLE_TONE` 这样的形式。
```
export type IPinyinStyle =
"normal" | "tone" | "tone2" | "to3ne" | "initials" | "first_letter" | // 推荐使用小写,和输出的拼音一致
"NORMAL" | "TONE" | "TONE2" | "TO3NE" | "INITIALS" | "FIRST_LETTER" | // 方便老版本迁移
0 | 1 | 2 | 5 | 3 | 4; // 兼容老版本
```
### IPinyinMode
拼音模式,默认普通模式,可以指定人名模式。
```
// - NORMAL: 普通模式
// - SURNAME: 姓氏模式,优先使用姓氏的拼音。
export type IPinyinMode =
"normal" | "surname" |
"NORMAL" | "SURNAME";
```
### IPinyinSegment
分词方式。
- 默认关闭 `false`,
- 也可以设置为 `true` 开启,Web 版中使用 "segmentit" 分词,Node 版中使用 "nodejieba" 分词。
- 也可以声明以下字符串来指定分词算法。但目前 Web 版只支持 "segmentit" 分词。
```
export type IPinyinSegment = "nodejieba" | "segmentit" | "@node-rs/jieba";
```
## API

@@ -106,0 +157,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc