nodejieba-plus 3.5.12 → 3.5.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
package/lib/nodejieba.cpp CHANGED
@@ -236,7 +236,11 @@ Napi::Value NodeJieba::loadUserDict(const Napi::CallbackInfo& info) {
236
236
  for (size_t i = 0; i < arr.Length(); i++) {
237
237
  Napi::Value val = arr[i];
238
238
  if (val.IsString()) {
239
- buf.push_back(val.As<Napi::String>().Utf8Value());
239
+ std::string line = val.As<Napi::String>().Utf8Value();
240
+ // 过滤空字符串,避免断言失败
241
+ if (!line.empty()) {
242
+ buf.push_back(line);
243
+ }
240
244
  }
241
245
  }
242
246
  _jieba_handle->LoadUserDict(buf);
@@ -244,8 +248,11 @@ Napi::Value NodeJieba::loadUserDict(const Napi::CallbackInfo& info) {
244
248
  // 支持传入单个词典条目字符串
245
249
  std::string line = info[0].As<Napi::String>().Utf8Value();
246
250
  std::vector<std::string> buf;
247
- buf.push_back(line);
248
- _jieba_handle->LoadUserDict(buf);
251
+ // 过滤空字符串
252
+ if (!line.empty()) {
253
+ buf.push_back(line);
254
+ _jieba_handle->LoadUserDict(buf);
255
+ }
249
256
  } else if (info[0].IsBuffer()) {
250
257
  // 支持传入 Buffer,将其转换为字符串并按行分割
251
258
  Napi::Buffer<char> buffer = info[0].As<Napi::Buffer<char>>();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nodejieba-plus",
3
3
  "description": "chinese word segmentation for node",
4
- "version": "3.5.12",
4
+ "version": "3.5.13",
5
5
  "author": "Yanyi Wu <wuyanyi09@foxmail.com>",
6
6
  "maintainers": [
7
7
  "Yanyi Wu <wuyanyi09@foxmail.com>"
@@ -74,4 +74,17 @@ describe("nodejieba.loadUserDict", function() {
74
74
  var loadResult = nodejieba.loadUserDict(dictSet);
75
75
  loadResult.should.eql(true);
76
76
  });
77
+
78
+ it("nodejieba.loadUserDict should filter empty strings", function() {
79
+ // 测试空字符串被过滤,不会导致断言失败
80
+ var dictLines = [
81
+ "有效词1",
82
+ "", // 空字符串
83
+ "有效词2",
84
+ "", // 空字符串
85
+ " " // 只有空格的字符串(也会被保留,因为不是完全空)
86
+ ];
87
+ var loadResult = nodejieba.loadUserDict(dictLines);
88
+ loadResult.should.eql(true);
89
+ });
77
90
  });