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