nv-constexpr-simple-codify 1.0.5 → 1.0.6

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
@@ -85,8 +85,32 @@ const _codify_leaf = (o)=>{
85
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
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
+ }
90
114
  }
91
115
  }
92
116
  }
@@ -227,11 +251,22 @@ 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 instanceof String) {
266
+ return rtrn_pair ? [true, null] : true;
233
267
  }
234
268
 
269
+
235
270
  // 3. 排除 Function
236
271
  if (typeof o === 'function') {
237
272
  return rtrn_pair ? [false, "Function is not supported"] : false;
@@ -286,6 +321,13 @@ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
286
321
  return rtrn_pair ? [true, null] : true;
287
322
  }
288
323
 
324
+ // Error
325
+ if(o instanceof Error) {
326
+ return rtrn_pair ? [true, null] : true;
327
+ }
328
+
329
+
330
+
289
331
  // 9. 容器只能是 Array 和 plain Object
290
332
  if (Array.isArray(o)) {
291
333
  for (const item of o) {
@@ -311,7 +353,6 @@ function is_supported(o, rtrn_pair = true, seen = new WeakSet()) {
311
353
  ) {
312
354
  return rtrn_pair ? [false, "Set/Map/WeakMap/WeakSet/WeakRef is not supported"] : false;
313
355
  }
314
-
315
356
  // 递归检查每个属性值
316
357
  for (const key in o) {
317
358
  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.6",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"