1 | 'use strict';
|
2 |
|
3 | var identity = require('./identity.js');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function toJS(value, arg, ctx) {
|
16 |
|
17 | if (Array.isArray(value))
|
18 | return value.map((v, i) => toJS(v, String(i), ctx));
|
19 | if (value && typeof value.toJSON === 'function') {
|
20 |
|
21 | if (!ctx || !identity.hasAnchor(value))
|
22 | return value.toJSON(arg, ctx);
|
23 | const data = { aliasCount: 0, count: 1, res: undefined };
|
24 | ctx.anchors.set(value, data);
|
25 | ctx.onCreate = res => {
|
26 | data.res = res;
|
27 | delete ctx.onCreate;
|
28 | };
|
29 | const res = value.toJSON(arg, ctx);
|
30 | if (ctx.onCreate)
|
31 | ctx.onCreate(res);
|
32 | return res;
|
33 | }
|
34 | if (typeof value === 'bigint' && !ctx?.keep)
|
35 | return Number(value);
|
36 | return value;
|
37 | }
|
38 |
|
39 | exports.toJS = toJS;
|