nv-constexpr-simple-codify 1.0.6 → 1.0.7

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.
Files changed (2) hide show
  1. package/index.js +87 -86
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -36,85 +36,85 @@ const _codify_ab = (o,ClsName) => {
36
36
  }
37
37
 
38
38
  const _codify_leaf = (o)=>{
39
- if(o instanceof ArrayBuffer) {
39
+ if(o?.constructor?.name === 'ArrayBuffer') {
40
40
  return (_codify_ab(o,"ArrayBuffer"));
41
- } else if(o?.constructor?.name === 'SharedArrayBuffer') {
41
+ } else if(o?.constructor?.name === 'SharedArrayBuffer') {
42
42
  return (_codify_ab(o,"SharedArrayBuffer"));
43
- } else if(o instanceof DataView){
43
+ } else if(o?.constructor?.name === 'DataView'){
44
44
  return(`(new DataView((new Uint8Array(${_codify_typed_ary(o,Uint8Array)})).buffer))`);
45
- } else if(o instanceof Uint8Array){
45
+ } else if(o?.constructor?.name === 'Uint8Array'){
46
46
  return(`(new Uint8Array(${_codify_typed_ary(o,Uint8Array)}))`);
47
- } else if(o instanceof Uint8ClampedArray) {
47
+ } else if(o?.constructor?.name === 'Uint8ClampedArray') {
48
48
  return(`(new Uint8ClampedArray(${_codify_typed_ary(o,Uint8ClampedArray)}))`);
49
- } else if(o instanceof Int8Array) {
49
+ } else if(o?.constructor?.name === 'Int8Array') {
50
50
  return(`(new Int8Array(${_codify_typed_ary(o,Int8Array)}))`);
51
- } else if(o instanceof Uint16Array) {
51
+ } else if(o?.constructor?.name === 'Uint16Array') {
52
52
  return(`(new Uint16Array(${_codify_typed_ary(o,Uint16Array)}))`);
53
- } else if(o instanceof Int16Array) {
53
+ } else if(o?.constructor?.name === 'Int16Array') {
54
54
  return(`(new Int16Array(${_codify_typed_ary(o,Int16Array)}))`);
55
- } else if (o instanceof Uint32Array) {
55
+ } else if (o?.constructor?.name === 'Uint32Array') {
56
56
  return(`(new Uint32Array(${_codify_typed_ary(o,Uint32Array)}))`);
57
- } else if (o instanceof Int32Array) {
57
+ } else if (o?.constructor?.name === 'Int32Array') {
58
58
  return(`(new Int32Array(${_codify_typed_ary(o,Int32Array)}))`);
59
- } else if(o instanceof BigUint64Array) {
59
+ } else if(o?.constructor?.name === 'BigUint64Array') {
60
60
  return(`(new BigUint64Array(${_codify_typed_ary(o,BigUint64Array)}))`);
61
- } else if(o instanceof BigInt64Array) {
61
+ } else if(o?.constructor?.name === 'BigInt64Array') {
62
62
  return(`(new BigInt64Array(${_codify_typed_ary(o,BigInt64Array)}))`);
63
- } else if(o instanceof Float32Array) {
63
+ } else if(o?.constructor?.name === 'Float32Array') {
64
64
  return(`(new Float32Array(${_codify_typed_ary(o,Float32Array)}))`);
65
- } else if(o instanceof Float64Array) {
65
+ } else if(o?.constructor?.name === 'Float64Array') {
66
66
  return(`(new Float64Array(${_codify_typed_ary(o,Float64Array)}))`);
67
- } else if(o instanceof Date) {
67
+ } else if(o?.constructor?.name === 'Date') {
68
68
  var mts = o.getTime();
69
69
  return(`(new Date(${String(mts)}))`);
70
- } else if(o instanceof RegExp) {
70
+ } else if(o?.constructor?.name === 'RegExp') {
71
71
  var source = o.source;
72
72
  var flags = o.flags;
73
- return `/${source}/${flags}`
73
+ return `/${source}/${flags}`;
74
74
  } else if(o === undefined){
75
- return "undefined"
75
+ return "undefined";
76
76
  } else if(o === null) {
77
77
  return "null";
78
78
  } else {
79
79
  var tnm = typeof(o);
80
80
  if(tnm === "string") {
81
- return JSON.stringify(o)
81
+ return JSON.stringify(o);
82
82
  } else if(tnm === "number") {
83
- return String(o)
83
+ return String(o);
84
84
  } else if(tnm === "boolean") {
85
- return o?"true":"false"
85
+ return o ? "true" : "false";
86
86
  } else if(tnm === "bigint"){
87
87
  return String(o) + "n";
88
- } else if(tnm === "symbol") {
89
- var gkey = Symbol.keyFor(o);
90
- if(gkey !== undefined) {
91
- return `Symbol.for(${JSON.stringify(gkey)})`;
92
- } else {
93
- return undefined
94
- }
88
+ } else if(tnm === "symbol") {
89
+ var gkey = Symbol.keyFor(o);
90
+ if(gkey !== undefined) {
91
+ return `Symbol.for(${JSON.stringify(gkey)})`;
92
+ } else {
93
+ return undefined;
94
+ }
95
95
  } else {
96
- if(o instanceof String) {
97
- return o.valueOf(); //不加引号用作name
98
- } else if(o instanceof Error) {
99
- var Ctor = o.constructor;
100
- var msg = o.message; // only keep message and json-cause
101
- if(cause === undefined) {
102
- return `(new ${Ctor}(${msg}))`;
103
- } else {
104
- try {
105
- cause = JSON.stringify(cause);
106
- return `(new ${Ctor}(${msg},{cause:${cause}}))`;
107
- } catch(e) {
108
- return `(new ${Ctor}(${msg}))`;
109
- }
110
- }
111
- } else {
112
- return undefined
113
- }
114
- }
96
+ if(o?.constructor?.name === 'String') {
97
+ return o.valueOf(); //不加引号用作name
98
+ } else if(o?.constructor?.name?.includes?.('Error')) {
99
+ var Ctor = o.constructor;
100
+ var msg = o.message; // only keep message and json-cause
101
+ var cause = o.cause;
102
+ if(cause === undefined) {
103
+ return `(new ${Ctor}(${JSON.stringify(msg)}))`;
104
+ } else {
105
+ try {
106
+ cause = JSON.stringify(cause);
107
+ return `(new ${Ctor}(${JSON.stringify(msg)},{cause:${cause}}))`;
108
+ } catch(e) {
109
+ return `(new ${Ctor}(${JSON.stringify(msg)}))`;
110
+ }
111
+ }
112
+ } else {
113
+ return undefined;
114
+ }
115
+ }
115
116
  }
116
- }
117
-
117
+ };
118
118
 
119
119
  function _codify_ary(ary,depth,space,recv,is_lst,key) {
120
120
  var indent_str = space.repeat(depth);
@@ -253,20 +253,19 @@ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
253
253
 
254
254
  // 2. 排除 Local Symbol
255
255
  if (typeof o === 'symbol') {
256
- var gkey = Symbol.keyFor(o);
257
- if(gkey !== undefined) {
258
- return rtrn_pair ? [true, null] : true;
259
- } else {
256
+ var gkey = Symbol.keyFor(o);
257
+ if (gkey !== undefined) {
258
+ return rtrn_pair ? [true, null] : true;
259
+ } else {
260
260
  return rtrn_pair ? [false, "Local Symbol is not supported"] : false;
261
- }
261
+ }
262
262
  }
263
263
 
264
264
  // 特殊情况 new String 用作name
265
- if(o instanceof String) {
265
+ if (o?.constructor?.name === "String") {
266
266
  return rtrn_pair ? [true, null] : true;
267
267
  }
268
268
 
269
-
270
269
  // 3. 排除 Function
271
270
  if (typeof o === 'function') {
272
271
  return rtrn_pair ? [false, "Function is not supported"] : false;
@@ -288,51 +287,52 @@ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
288
287
  }
289
288
 
290
289
  // 8. 支持的特殊对象类型
291
- if (o instanceof Date) {
290
+ if (o?.constructor?.name === "Date") {
292
291
  return rtrn_pair ? [true, null] : true;
293
292
  }
294
293
 
295
- if (o instanceof RegExp) {
294
+ if (o?.constructor?.name === "RegExp") {
296
295
  return rtrn_pair ? [true, null] : true;
297
296
  }
298
297
 
299
- if (o instanceof ArrayBuffer || o?.constructor?.name === 'SharedArrayBuffer') {
298
+ if (
299
+ o?.constructor?.name === "ArrayBuffer" ||
300
+ o?.constructor?.name === "SharedArrayBuffer"
301
+ ) {
300
302
  return rtrn_pair ? [true, null] : true;
301
303
  }
302
304
 
303
- if (o instanceof DataView) {
305
+ if (o?.constructor?.name === "DataView") {
304
306
  return rtrn_pair ? [true, null] : true;
305
307
  }
306
308
 
307
309
  // TypedArrays
308
- if (
309
- o instanceof Uint8Array ||
310
- o instanceof Uint8ClampedArray ||
311
- o instanceof Int8Array ||
312
- o instanceof Uint16Array ||
313
- o instanceof Int16Array ||
314
- o instanceof Uint32Array ||
315
- o instanceof Int32Array ||
316
- o instanceof Float32Array ||
317
- o instanceof Float64Array ||
318
- o instanceof BigUint64Array ||
319
- o instanceof BigInt64Array
320
- ) {
310
+ const typedArrayNames = [
311
+ "Uint8Array",
312
+ "Uint8ClampedArray",
313
+ "Int8Array",
314
+ "Uint16Array",
315
+ "Int16Array",
316
+ "Uint32Array",
317
+ "Int32Array",
318
+ "Float32Array",
319
+ "Float64Array",
320
+ "BigUint64Array",
321
+ "BigInt64Array"
322
+ ];
323
+ if (typedArrayNames.includes(o?.constructor?.name)) {
321
324
  return rtrn_pair ? [true, null] : true;
322
325
  }
323
326
 
324
327
  // Error
325
- if(o instanceof Error) {
326
- return rtrn_pair ? [true, null] : true;
327
- }
328
-
329
-
328
+ if (o?.constructor?.name?.includes?.("Error")) {
329
+ return rtrn_pair ? [true, null] : true;
330
+ }
330
331
 
331
332
  // 9. 容器只能是 Array 和 plain Object
332
333
  if (Array.isArray(o)) {
333
334
  for (const item of o) {
334
335
  const res = is_supported(item, rtrn_pair, seen);
335
- // 直接用 res[0] 判断,因为 res 是 [bool, string] 或 bool
336
336
  if (rtrn_pair) {
337
337
  if (!res[0]) return res;
338
338
  } else {
@@ -344,13 +344,14 @@ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
344
344
 
345
345
  if (o !== null && typeof o === 'object') {
346
346
  // 排除不支持的容器类型
347
- if (
348
- o instanceof Set ||
349
- o instanceof Map ||
350
- o instanceof WeakMap ||
351
- o instanceof WeakSet ||
352
- o instanceof WeakRef
353
- ) {
347
+ const unsupportedContainerNames = [
348
+ "Set",
349
+ "Map",
350
+ "WeakMap",
351
+ "WeakSet",
352
+ "WeakRef"
353
+ ];
354
+ if (unsupportedContainerNames.includes(o?.constructor?.name)) {
354
355
  return rtrn_pair ? [false, "Set/Map/WeakMap/WeakSet/WeakRef is not supported"] : false;
355
356
  }
356
357
  // 递归检查每个属性值
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nv-constexpr-simple-codify",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"