nv-constexpr-simple-codify 1.0.5 → 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.
package/README.md CHANGED
@@ -55,6 +55,10 @@ Every supported leaf type has a direct 1:1 counterpart in C++. This makes the ou
55
55
  `RegExp` use a third-party library that supports compile-time regex (do NOT use std::regex)
56
56
 
57
57
 
58
+ ## String
59
+ `new String` (typeof !== string BUT instanceof String) ; this is for special use: to generate var-name/identifier ; will NOT be quoted.
60
+ `string` will be JSON.stringify(quoted)
61
+
58
62
 
59
63
  ## Container Support
60
64
 
@@ -4,21 +4,27 @@ const {is_supported} = require("../index");
4
4
  // 正常情况
5
5
  console.log(is_supported({ a: 1n, b: [1, 2, 3] }));
6
6
 
7
+ console.log(is_supported(Symbol.for("aaa")));
8
+
9
+ console.log(is_supported(new String("aaa")));
10
+
7
11
  // 循环引用
8
12
  const circ = { a: 1 };
9
13
  circ.self = circ;
10
14
  console.log(is_supported(circ));
11
15
 
12
16
  // 不支持的类型
17
+ console.log(is_supported(Symbol("aaa")));
13
18
  console.log(is_supported(new Set()));
14
19
 
15
20
  console.log(is_supported(Promise.resolve()));
16
21
 
17
22
  console.log(is_supported(function() {}));
18
23
 
19
-
24
+ /*
20
25
  [ true, null ]
21
26
  [ false, 'Circular reference detected' ]
22
27
  [ false, 'Set/Map/WeakMap/WeakSet/WeakRef is not supported' ]
23
28
  [ false, 'Promise is not supported' ]
24
29
  [ false, 'Function is not supported' ]
30
+ */
package/TEST/tst.js CHANGED
@@ -8,6 +8,12 @@ var code0 = codify({
8
8
  str: "hello\"world\n\t\\test",
9
9
  num: 12345.6789,
10
10
  bigint: 9007199254740993n,
11
+ //global sym
12
+ gsym: Symbol.for("gloabl-symbol"),
13
+ //Name
14
+ name: new String("varname"), // varname
15
+ str2: "varname", // "varname"
16
+
11
17
  // TypedArrays
12
18
  uint8: new Uint8Array([1, 2, 255]),
13
19
  uint8c: new Uint8ClampedArray([0, 128, 255]),
package/index.js CHANGED
@@ -36,61 +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
95
  } else {
89
- return undefined
90
- }
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
+ }
91
116
  }
92
- }
93
-
117
+ };
94
118
 
95
119
  function _codify_ary(ary,depth,space,recv,is_lst,key) {
96
120
  var indent_str = space.repeat(depth);
@@ -227,9 +251,19 @@ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
227
251
  return rtrn_pair ? [true, null] : true;
228
252
  }
229
253
 
230
- // 2. 排除 Symbol
254
+ // 2. 排除 Local Symbol
231
255
  if (typeof o === 'symbol') {
232
- return rtrn_pair ? [false, "Symbol is not supported"] : false;
256
+ var gkey = Symbol.keyFor(o);
257
+ if (gkey !== undefined) {
258
+ return rtrn_pair ? [true, null] : true;
259
+ } else {
260
+ return rtrn_pair ? [false, "Local Symbol is not supported"] : false;
261
+ }
262
+ }
263
+
264
+ // 特殊情况 new String 用作name
265
+ if (o?.constructor?.name === "String") {
266
+ return rtrn_pair ? [true, null] : true;
233
267
  }
234
268
 
235
269
  // 3. 排除 Function
@@ -253,36 +287,45 @@ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
253
287
  }
254
288
 
255
289
  // 8. 支持的特殊对象类型
256
- if (o instanceof Date) {
290
+ if (o?.constructor?.name === "Date") {
257
291
  return rtrn_pair ? [true, null] : true;
258
292
  }
259
293
 
260
- if (o instanceof RegExp) {
294
+ if (o?.constructor?.name === "RegExp") {
261
295
  return rtrn_pair ? [true, null] : true;
262
296
  }
263
297
 
264
- if (o instanceof ArrayBuffer || o?.constructor?.name === 'SharedArrayBuffer') {
298
+ if (
299
+ o?.constructor?.name === "ArrayBuffer" ||
300
+ o?.constructor?.name === "SharedArrayBuffer"
301
+ ) {
265
302
  return rtrn_pair ? [true, null] : true;
266
303
  }
267
304
 
268
- if (o instanceof DataView) {
305
+ if (o?.constructor?.name === "DataView") {
269
306
  return rtrn_pair ? [true, null] : true;
270
307
  }
271
308
 
272
309
  // 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
- ) {
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)) {
324
+ return rtrn_pair ? [true, null] : true;
325
+ }
326
+
327
+ // Error
328
+ if (o?.constructor?.name?.includes?.("Error")) {
286
329
  return rtrn_pair ? [true, null] : true;
287
330
  }
288
331
 
@@ -290,7 +333,6 @@ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
290
333
  if (Array.isArray(o)) {
291
334
  for (const item of o) {
292
335
  const res = is_supported(item, rtrn_pair, seen);
293
- // 直接用 res[0] 判断,因为 res 是 [bool, string] 或 bool
294
336
  if (rtrn_pair) {
295
337
  if (!res[0]) return res;
296
338
  } else {
@@ -302,16 +344,16 @@ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
302
344
 
303
345
  if (o !== null && typeof o === 'object') {
304
346
  // 排除不支持的容器类型
305
- if (
306
- o instanceof Set ||
307
- o instanceof Map ||
308
- o instanceof WeakMap ||
309
- o instanceof WeakSet ||
310
- o instanceof WeakRef
311
- ) {
347
+ const unsupportedContainerNames = [
348
+ "Set",
349
+ "Map",
350
+ "WeakMap",
351
+ "WeakSet",
352
+ "WeakRef"
353
+ ];
354
+ if (unsupportedContainerNames.includes(o?.constructor?.name)) {
312
355
  return rtrn_pair ? [false, "Set/Map/WeakMap/WeakSet/WeakRef is not supported"] : false;
313
356
  }
314
-
315
357
  // 递归检查每个属性值
316
358
  for (const key in o) {
317
359
  const res = is_supported(o[key], rtrn_pair, seen);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nv-constexpr-simple-codify",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"