Skip to content

Commit

Permalink
feat: 分词组件作为可选对等依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
hotoo committed Dec 3, 2023
1 parent 8ea8eb3 commit 7c887be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

----

## 4.0.0-alpha.0

- optionalPeerDependencies nodejieba, @node-rs/jieba, and segmentit.
- 打包为 esm 和 umd 格式。

## 3.1.0

- release 3.1.0, with output amd format.

## 3.0.0-alpha.7 (2023-11-20)

- feat: dynamic import optional dependencies.
Expand Down
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pinyin",
"version": "3.1.0",
"version": "4.0.0-alpha.0",
"description": "汉语拼音转换工具。",
"main": "./dist/pinyin.js",
"module": "./dist/pinyin.mjs",
Expand Down Expand Up @@ -35,11 +35,22 @@
"dependencies": {
"commander": "~1.1.1"
},
"optionalDependencies": {
"peerDependencies": {
"@node-rs/jieba": "^1.6.0",
"nodejieba": "2.5.2",
"segmentit": "^2.0.3"
},
"peerDependenciesMeta": {
"@node-rs/jieba": {
"optional": true
},
"nodejieba": {
"optional": true
},
"segmentit": {
"optional": true
}
},
"devDependencies": {
"@babel/preset-env": "^7.15.6",
"@babel/preset-typescript": "^7.15.0",
Expand Down
12 changes: 12 additions & 0 deletions src/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function segment(hans: string, segment?: IPinyinSegment): string[] {
if (segment === "@node-rs/jieba") {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { load, cut /*, tag */ } = require("@node-rs/jieba");
if (!load || !cut) {
console.error("pinyin v4: \"@node-rs/jieba\" is peerDependencies");
return [hans];
}
if (!nodeRsJiebaLoaded) {
nodeRsJiebaLoaded = true;
load();
Expand All @@ -26,6 +30,10 @@ export function segment(hans: string, segment?: IPinyinSegment): string[] {
if (segment === "segmentit") {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { Segment, useDefault } = require("segmentit");
if (!Segment) {
console.error("pinyin v4: \"segmentit\" is peerDependencies");
return [hans];
}
if (!segmentit) {
segmentit = useDefault(new Segment());
}
Expand All @@ -48,6 +56,10 @@ export function segment(hans: string, segment?: IPinyinSegment): string[] {

// eslint-disable-next-line @typescript-eslint/no-var-requires
const nodejieba = require("nodejieba");
if (!nodejieba) {
console.error("pinyin v4: \"nodejieba\" is peerDependencies");
return [hans];
}
// 默认使用 nodejieba (C++)
// return nodejieba.tag(hans);
// nodejieba 定义的类型返回值错误,先忽略。
Expand Down

0 comments on commit 7c887be

Please sign in to comment.