nv-constexpr-simple-codify 1.0.2 → 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.
- package/TEST/tst-is-supported.js +7 -5
- package/index.js +32 -35
- package/package.json +1 -1
package/TEST/tst-is-supported.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
//
|
|
215
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
252
|
+
return rtrn_pair ? [true, null] : true;
|
|
259
253
|
}
|
|
260
254
|
|
|
261
255
|
// 8. 支持的特殊对象类型
|
|
262
256
|
if (o instanceof Date) {
|
|
263
|
-
return
|
|
257
|
+
return rtrn_pair ? [true, null] : true;
|
|
264
258
|
}
|
|
265
259
|
|
|
266
260
|
if (o instanceof RegExp) {
|
|
267
|
-
return
|
|
261
|
+
return rtrn_pair ? [true, null] : true;
|
|
268
262
|
}
|
|
269
263
|
|
|
270
264
|
if (o instanceof ArrayBuffer || o?.constructor?.name === 'SharedArrayBuffer') {
|
|
271
|
-
return
|
|
265
|
+
return rtrn_pair ? [true, null] : true;
|
|
272
266
|
}
|
|
273
267
|
|
|
274
268
|
if (o instanceof DataView) {
|
|
275
|
-
return
|
|
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
|
|
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
|
-
|
|
300
|
-
|
|
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
|
|
300
|
+
return rtrn_pair ? [true, null] : true;
|
|
304
301
|
}
|
|
305
302
|
|
|
306
303
|
if (o !== null && typeof o === 'object') {
|
|
@@ -312,29 +309,29 @@ function is_supported(o, seen = new WeakSet()) {
|
|
|
312
309
|
o instanceof WeakSet ||
|
|
313
310
|
o instanceof WeakRef
|
|
314
311
|
) {
|
|
315
|
-
return
|
|
312
|
+
return rtrn_pair ? [false, "Set/Map/WeakMap/WeakSet/WeakRef is not supported"] : false;
|
|
316
313
|
}
|
|
317
314
|
|
|
318
|
-
|
|
319
315
|
// 检查是否是普通对象(plain object)
|
|
320
316
|
const proto = Object.getPrototypeOf(o);
|
|
321
317
|
if (proto !== Object.prototype && proto !== null) {
|
|
322
|
-
return
|
|
318
|
+
return rtrn_pair ? [false, "Custom class instance is not supported (only plain objects)"] : false;
|
|
323
319
|
}
|
|
324
|
-
|
|
325
320
|
|
|
326
321
|
// 递归检查每个属性值
|
|
327
322
|
for (const key in o) {
|
|
328
|
-
const res = is_supported(o[key], seen);
|
|
329
|
-
if (
|
|
330
|
-
return res;
|
|
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;
|
|
331
328
|
}
|
|
332
329
|
}
|
|
333
|
-
return
|
|
330
|
+
return rtrn_pair ? [true, null] : true;
|
|
334
331
|
}
|
|
335
332
|
|
|
336
333
|
// 其他未知类型不支持
|
|
337
|
-
return
|
|
334
|
+
return rtrn_pair ? [false, "Unknown type is not supported"] : false;
|
|
338
335
|
}
|
|
339
336
|
|
|
340
337
|
|