nv-constexpr-simple-codify 1.0.2 → 1.0.4

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.
@@ -3,20 +3,22 @@ const {is_supported} = require("../index");
3
3
 
4
4
  // 正常情况
5
5
  console.log(is_supported({ a: 1n, b: [1, 2, 3] }));
6
- // Reason { reason: null, __proto__: Boolean, [[PrimitiveValue]]: true }
7
6
 
8
7
  // 循环引用
9
8
  const circ = { a: 1 };
10
9
  circ.self = circ;
11
10
  console.log(is_supported(circ));
12
- // Reason { reason: "Circular reference detected", __proto__: Boolean, [[PrimitiveValue]]: false }
13
11
 
14
12
  // 不支持的类型
15
13
  console.log(is_supported(new Set()));
16
- // Reason { reason: "Set/Map/WeakMap/WeakSet/WeakRef is not supported", ... }
17
14
 
18
15
  console.log(is_supported(Promise.resolve()));
19
- // Reason { reason: "Promise is not supported", ... }
20
16
 
21
17
  console.log(is_supported(function() {}));
22
- // Reason { reason: "Function is not supported", ... }
18
+
19
+
20
+ [ true, null ]
21
+ [ false, 'Circular reference detected' ]
22
+ [ false, 'Set/Map/WeakMap/WeakSet/WeakRef is not supported' ]
23
+ [ false, 'Promise is not supported' ]
24
+ [ false, 'Function is not supported' ]
package/index.js CHANGED
@@ -197,53 +197,47 @@ function codify (obj,space=0){
197
197
  return _codify(obj,space);
198
198
  }
199
199
 
200
-
201
200
  const is_prms = (o) => o[Symbol.toStringTag] === "Promise";
202
201
  const is_sg = (o) => o[Symbol.toStringTag] === 'Generator';
203
202
  const is_ag = (o) => o[Symbol.toStringTag] === 'AsyncGenerator';
204
203
  const is_g = (o) => is_sg(o) || is_ag(o);
205
204
  const is_iter = (o) => o[Symbol.toStringTag] ? (o[Symbol.toStringTag].includes("Iterator")) : false;
206
- class Reason extends Boolean {
207
- reason = null;
208
- constructor(reason) {
209
- super((reason === null || reason === undefined) ? true : false);
210
- this.reason = reason;
211
- }
212
- }
213
205
 
214
- // 用于循环引用检测的 WeakSet
215
- function is_supported(o, seen = new WeakSet()) {
206
+ // 如果 rtrn_pair 是 true,我们返回 [true, null] | [false, reason字符串]
207
+ // 否则只返回 true|false
208
+ // 这样不容易误用
209
+ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
216
210
  // 1. 循环引用检测
217
211
  if (o !== null && typeof o === 'object') {
218
212
  if (seen.has(o)) {
219
- return new Reason("Circular reference detected");
213
+ return rtrn_pair ? [false, "Circular reference detected"] : false;
220
214
  }
221
215
  seen.add(o);
222
216
  }
223
217
 
224
218
  // 2. 排除 Symbol
225
219
  if (typeof o === 'symbol') {
226
- return new Reason("Symbol is not supported");
220
+ return rtrn_pair ? [false, "Symbol is not supported"] : false;
227
221
  }
228
222
 
229
223
  // 3. 排除 Function
230
224
  if (typeof o === 'function') {
231
- return new Reason("Function is not supported");
225
+ return rtrn_pair ? [false, "Function is not supported"] : false;
232
226
  }
233
227
 
234
228
  // 4. 排除 Promise
235
229
  if (is_prms(o)) {
236
- return new Reason("Promise is not supported");
230
+ return rtrn_pair ? [false, "Promise is not supported"] : false;
237
231
  }
238
232
 
239
233
  // 5. 排除 Generator / AsyncGenerator
240
234
  if (is_g(o)) {
241
- return new Reason("Generator/AsyncGenerator is not supported");
235
+ return rtrn_pair ? [false, "Generator/AsyncGenerator is not supported"] : false;
242
236
  }
243
237
 
244
238
  // 6. 排除 Iterator
245
239
  if (is_iter(o)) {
246
- return new Reason("Iterator is not supported");
240
+ return rtrn_pair ? [false, "Iterator is not supported"] : false;
247
241
  }
248
242
 
249
243
  // 7. 支持的基础类型
@@ -255,24 +249,24 @@ function is_supported(o, seen = new WeakSet()) {
255
249
  typeof o === 'number' ||
256
250
  typeof o === 'bigint'
257
251
  ) {
258
- return new Reason(null); // 无理由,就是支持
252
+ return rtrn_pair ? [true, null] : true;
259
253
  }
260
254
 
261
255
  // 8. 支持的特殊对象类型
262
256
  if (o instanceof Date) {
263
- return new Reason(null);
257
+ return rtrn_pair ? [true, null] : true;
264
258
  }
265
259
 
266
260
  if (o instanceof RegExp) {
267
- return new Reason(null);
261
+ return rtrn_pair ? [true, null] : true;
268
262
  }
269
263
 
270
264
  if (o instanceof ArrayBuffer || o?.constructor?.name === 'SharedArrayBuffer') {
271
- return new Reason(null);
265
+ return rtrn_pair ? [true, null] : true;
272
266
  }
273
267
 
274
268
  if (o instanceof DataView) {
275
- return new Reason(null);
269
+ return rtrn_pair ? [true, null] : true;
276
270
  }
277
271
 
278
272
  // TypedArrays
@@ -289,18 +283,21 @@ function is_supported(o, seen = new WeakSet()) {
289
283
  o instanceof BigUint64Array ||
290
284
  o instanceof BigInt64Array
291
285
  ) {
292
- return new Reason(null);
286
+ return rtrn_pair ? [true, null] : true;
293
287
  }
294
288
 
295
289
  // 9. 容器只能是 Array 和 plain Object
296
290
  if (Array.isArray(o)) {
297
291
  for (const item of o) {
298
- const res = is_supported(item, seen);
299
- if (!res) {
300
- return res; // 已经是 Reason(false)
292
+ const res = is_supported(item, rtrn_pair, seen);
293
+ // 直接用 res[0] 判断,因为 res 是 [bool, string] 或 bool
294
+ if (rtrn_pair) {
295
+ if (!res[0]) return res;
296
+ } else {
297
+ if (!res) return res;
301
298
  }
302
299
  }
303
- return new Reason(null);
300
+ return rtrn_pair ? [true, null] : true;
304
301
  }
305
302
 
306
303
  if (o !== null && typeof o === 'object') {
@@ -312,29 +309,23 @@ function is_supported(o, seen = new WeakSet()) {
312
309
  o instanceof WeakSet ||
313
310
  o instanceof WeakRef
314
311
  ) {
315
- return new Reason("Set/Map/WeakMap/WeakSet/WeakRef is not supported");
312
+ return rtrn_pair ? [false, "Set/Map/WeakMap/WeakSet/WeakRef is not supported"] : false;
316
313
  }
317
314
 
318
-
319
- // 检查是否是普通对象(plain object)
320
- const proto = Object.getPrototypeOf(o);
321
- if (proto !== Object.prototype && proto !== null) {
322
- return new Reason("Custom class instance is not supported (only plain objects)");
323
- }
324
-
325
-
326
315
  // 递归检查每个属性值
327
316
  for (const key in o) {
328
- const res = is_supported(o[key], seen);
329
- if (!res) {
330
- return res;
317
+ const res = is_supported(o[key], rtrn_pair, seen);
318
+ if (rtrn_pair) {
319
+ if (!res[0]) return res;
320
+ } else {
321
+ if (!res) return res;
331
322
  }
332
323
  }
333
- return new Reason(null);
324
+ return rtrn_pair ? [true, null] : true;
334
325
  }
335
326
 
336
327
  // 其他未知类型不支持
337
- return new Reason("Unknown type is not supported");
328
+ return rtrn_pair ? [false, "Unknown type is not supported"] : false;
338
329
  }
339
330
 
340
331
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nv-constexpr-simple-codify",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"