slimjson 1.1.1 → 1.1.3

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.
package/README.md CHANGED
@@ -503,7 +503,7 @@ mimo-v2.5-pro
503
503
  ## 开发
504
504
 
505
505
  ```bash
506
- # 运行测试(192 个用例,100% 覆盖率)
506
+ # 运行测试(209 个用例,100% 覆盖率)
507
507
  npm test
508
508
 
509
509
  # 运行压缩率基准测试(含 trim 对比)
package/README_EN.md CHANGED
@@ -492,7 +492,7 @@ mimo-v2.5-pro
492
492
  ## Development
493
493
 
494
494
  ```bash
495
- # Run tests (192 cases, 100% coverage)
495
+ # Run tests (209 cases, 100% coverage)
496
496
  npm test
497
497
 
498
498
  # Run compression ratio benchmarks (with trim comparison)
package/compress.js CHANGED
@@ -13,7 +13,6 @@
13
13
  */
14
14
  function mergeSchemas(s1, s2) {
15
15
  if (!Array.isArray(s1) || !Array.isArray(s2)) return s1;
16
-
17
16
  const first1 = s1[0];
18
17
  const first2 = s2[0];
19
18
 
@@ -40,12 +39,7 @@ function mergeSchemas(s1, s2) {
40
39
  }
41
40
 
42
41
  // 两者都是数组(不是对象 schema)→ 递归合并第一个元素
43
- if (Array.isArray(first1) && Array.isArray(first2)) {
44
- return [mergeSchemas(first1, first2)];
45
- }
46
-
47
- // 其他情况(原始值数组或类型不匹配)→ 取第一个
48
- return s1;
42
+ return [mergeSchemas(first1, first2)];
49
43
  }
50
44
 
51
45
  /**
@@ -76,12 +70,10 @@ function inferSchema(value) {
76
70
  return [inferObjectSchema(objects)];
77
71
  }
78
72
  // 原始值数组 - 不压缩,由父级处理
79
- return undefined;
80
- }
81
- if (typeof value === 'object' && value !== null) {
82
- return inferObjectSchema([value]);
73
+ return;
83
74
  }
84
- return undefined;
75
+ // value 是单个对象
76
+ return inferObjectSchema([value]);
85
77
  }
86
78
 
87
79
  /**
@@ -106,7 +98,7 @@ function inferObjectSchema(objects) {
106
98
  }
107
99
 
108
100
  return keyOrder.map(key => {
109
- const values = keyValues.get(key) || [];
101
+ const values = keyValues.get(key);
110
102
  if (values.length === 0) return key;
111
103
 
112
104
  const sample = values[0];
@@ -147,15 +139,12 @@ function inferObjectSchema(objects) {
147
139
  for (const v of values) {
148
140
  if (Array.isArray(v)) {
149
141
  const s = inferSchema(v);
150
- if (s) {
151
- // inferSchema 返回 [innerSchema],取 innerSchema 用于合并
152
- const inner = Array.isArray(s) && s.length === 1 ? s[0] : s;
153
- merged = merged ? mergeSchemas(merged, inner) : inner;
154
- }
142
+ const inner = s[0];
143
+ merged = merged ? mergeSchemas(merged, inner) : inner;
155
144
  }
156
145
  }
157
146
  // 再包一层 [] 表示"数组的数组"
158
- return { [key]: [merged || inferSchema(sample[0])] };
147
+ return { [key]: [merged] };
159
148
  }
160
149
 
161
150
  // 原始值数组(如 ["张三","李四"])→ 不压缩,直接用 key 名
@@ -180,31 +169,21 @@ function compressWithSchema(value, schema) {
180
169
  return value.map(item => compressWithSchema(item, inner));
181
170
  }
182
171
 
183
- // schema 包含 undefined → 原始值数组,不压缩
184
- if (Array.isArray(schema) && schema.some(s => s === undefined || s === null)) {
185
- return value;
186
- }
187
-
188
172
  // schema 是数组(对象 schema)→ 值是对象
189
- if (Array.isArray(schema)) {
190
- if (Array.isArray(value)) return value.length === 0 ? [] : null;
191
- if (!value || typeof value !== 'object') return value;
192
- return schema.map(fieldDef => {
193
- let key, valueSchema;
194
- if (typeof fieldDef === 'string') {
195
- key = fieldDef;
196
- valueSchema = undefined;
197
- } else {
198
- key = Object.keys(fieldDef)[0];
199
- valueSchema = fieldDef[key];
200
- }
201
- const val = value[key];
202
- if (val == null) return null;
203
- return compressWithSchema(val, valueSchema);
204
- });
205
- }
206
-
207
- return value;
173
+ if (!value || typeof value !== 'object') return value;
174
+ return schema.map(fieldDef => {
175
+ let key, valueSchema;
176
+ if (typeof fieldDef === 'string') {
177
+ key = fieldDef;
178
+ valueSchema = undefined;
179
+ } else {
180
+ key = Object.keys(fieldDef)[0];
181
+ valueSchema = fieldDef[key];
182
+ }
183
+ const val = value[key];
184
+ if (val == null) return null;
185
+ return compressWithSchema(val, valueSchema);
186
+ });
208
187
  }
209
188
 
210
189
  /**
@@ -257,41 +236,32 @@ function decompressWithSchema(data, schema) {
257
236
 
258
237
  // schema 是 [innerSchema] → 还原为数组
259
238
  if (Array.isArray(schema) && schema.length === 1 && Array.isArray(schema[0])) {
260
- if (!Array.isArray(data)) return data;
261
239
  const inner = schema[0];
262
240
  return data.map(item => decompressWithSchema(item, inner));
263
241
  }
264
242
 
265
- // schema 是数组
266
- if (Array.isArray(schema)) {
267
- // schema 包含 undefined 元素 → 原始值数组,不压缩
268
- if (schema.some(s => s === undefined || s === null)) return data;
269
-
270
- // 原始值(混合数组中的原始元素)→ 直接返回
271
- if (typeof data !== 'object' || data === null) return data;
272
-
273
- // 对象 schema 还原为对象
274
- const obj = {};
275
- for (let i = 0; i < schema.length; i++) {
276
- const fieldDef = schema[i];
277
- let key, valueSchema;
278
- if (typeof fieldDef === 'string') {
279
- key = fieldDef;
280
- valueSchema = undefined;
281
- } else if (typeof fieldDef === 'object' && fieldDef !== null) {
282
- key = Object.keys(fieldDef)[0];
283
- valueSchema = fieldDef[key];
284
- } else {
285
- continue;
286
- }
287
- const val = data[i];
288
- if (val === undefined) { obj[key] = null; continue; }
289
- obj[key] = decompressWithSchema(val, valueSchema);
243
+ // 原始值(混合数组中的原始元素)→ 直接返回
244
+ if (typeof data !== 'object') return data;
245
+
246
+ // 对象 schema 还原为对象
247
+ const obj = {};
248
+ for (let i = 0; i < schema.length; i++) {
249
+ const fieldDef = schema[i];
250
+ let key, valueSchema;
251
+ if (typeof fieldDef === 'string') {
252
+ key = fieldDef;
253
+ valueSchema = undefined;
254
+ } else if (typeof fieldDef === 'object' && fieldDef !== null) {
255
+ key = Object.keys(fieldDef)[0];
256
+ valueSchema = fieldDef[key];
257
+ } else {
258
+ continue;
290
259
  }
291
- return obj;
260
+ const val = data[i];
261
+ if (val === undefined) { obj[key] = null; continue; }
262
+ obj[key] = decompressWithSchema(val, valueSchema);
292
263
  }
293
-
294
- return data;
264
+ return obj;
295
265
  }
296
266
 
297
267
  /**
@@ -494,8 +464,7 @@ function parse(text) {
494
464
  skipWs();
495
465
  if (text[pos] !== ':') error('Expected :');
496
466
  pos++;
497
- const val = parseValue();
498
- obj[key] = val;
467
+ obj[key] = parseValue();
499
468
  skipWs();
500
469
  if (text[pos] === '}') { pos++; return obj; }
501
470
  if (text[pos] === ',') { pos++; continue; }