seroval 0.1.0
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/LICENSE +7 -0
- package/README.md +153 -0
- package/dist/cjs/development/index.cjs +481 -0
- package/dist/cjs/development/index.cjs.map +7 -0
- package/dist/cjs/production/index.cjs +2 -0
- package/dist/esm/development/index.mjs +458 -0
- package/dist/esm/development/index.mjs.map +7 -0
- package/dist/esm/production/index.mjs +2 -0
- package/dist/types/array.d.ts +2 -0
- package/dist/types/get-identifier.d.ts +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/quote.d.ts +1 -0
- package/package.json +68 -0
- package/src/array.ts +18 -0
- package/src/get-identifier.ts +16 -0
- package/src/index.ts +479 -0
- package/src/quote.ts +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
MIT License Copyright (c) 2023 <author>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# seroval
|
|
2
|
+
|
|
3
|
+
> Stringify JS values
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/seroval) [](https://github.com/airbnb/javascript)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install --save seroval
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add seroval
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add seroval
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import seroval from 'seroval';
|
|
25
|
+
|
|
26
|
+
const object = {
|
|
27
|
+
number: [Math.random(), -0, NaN, Infinity, -Infinity],
|
|
28
|
+
string: ['hello world', '<script>Hello World</script>'],
|
|
29
|
+
boolean: [true, false],
|
|
30
|
+
null: null,
|
|
31
|
+
undefined: undefined,
|
|
32
|
+
bigint: 9007199254740991n,
|
|
33
|
+
array: [,,,], // holes
|
|
34
|
+
regexp: /[a-z0-9]+/i,
|
|
35
|
+
date: new Date(),
|
|
36
|
+
map: new Map([['hello', 'world']]),
|
|
37
|
+
set: new Set(['hello', 'world']),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// self cyclic references
|
|
41
|
+
// recursive objects
|
|
42
|
+
object.self = object;
|
|
43
|
+
// recursive arrays
|
|
44
|
+
object.array.push(object.array);
|
|
45
|
+
// recursive maps
|
|
46
|
+
object.map.set('self', object.map);
|
|
47
|
+
// recursive sets
|
|
48
|
+
object.set.add(object.set);
|
|
49
|
+
|
|
50
|
+
// mutual cyclic references
|
|
51
|
+
object.array.push(object.map);
|
|
52
|
+
object.map.set('mutual', object.set);
|
|
53
|
+
object.set.add(object.array);
|
|
54
|
+
|
|
55
|
+
const result = seroval(object);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Output (as a string):
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
((h,j,k,m)=>(h={number:[0.5906513825027921,-0,NaN,Infinity,-Infinity],string:["hello world","\x3Cscript>Hello World\x3C/script>"],boolean:[!0,!1],null:null,undefined:void 0,bigint:BigInt("9007199254740991"),array:j=[,,,,k=new Map([["hello","world"],["mutual",m=new Set(["hello","world"])]])],regexp:/[a-z0-9]+/i,date:new Date("2023-02-06T06:16:00.384Z"),map:k,set:m},j[3]=j,h.self=h,k.set("self",k),m.add(m).add(j),h))()
|
|
62
|
+
|
|
63
|
+
// Formatted for readability
|
|
64
|
+
((h, j, k, m) => (h = {
|
|
65
|
+
number: [0.5906513825027921, -0, NaN, Infinity, -Infinity],
|
|
66
|
+
string: ["hello world", "\x3Cscript>Hello World\x3C/script>"],
|
|
67
|
+
boolean: [!0, !1],
|
|
68
|
+
null: null,
|
|
69
|
+
undefined: void 0,
|
|
70
|
+
bigint: BigInt("9007199254740991"),
|
|
71
|
+
array: j = [, , , , k = new Map([
|
|
72
|
+
["hello", "world"],
|
|
73
|
+
["mutual", m = new Set(["hello", "world"])]
|
|
74
|
+
])],
|
|
75
|
+
regexp: /[a-z0-9]+/i,
|
|
76
|
+
date: new Date("2023-02-06T06:16:00.384Z"),
|
|
77
|
+
map: k,
|
|
78
|
+
set: m
|
|
79
|
+
}, j[3] = j, h.self = h, k.set("self", k), m.add(m).add(j), h))()
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Mutual cyclic example
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
import seroval from 'seroval';
|
|
86
|
+
|
|
87
|
+
const a = new Map([['name', 'a']]);
|
|
88
|
+
const b = new Map([['name', 'b']]);
|
|
89
|
+
const c = new Map([['name', 'c']]);
|
|
90
|
+
const d = new Map([['name', 'd']]);
|
|
91
|
+
|
|
92
|
+
c.set('left', a);
|
|
93
|
+
d.set('left', a);
|
|
94
|
+
c.set('right', b);
|
|
95
|
+
d.set('right', b);
|
|
96
|
+
a.set('children', [c, d]);
|
|
97
|
+
b.set('children', [c, d]);
|
|
98
|
+
|
|
99
|
+
const result = seroval({ a, b, c, d });
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Output (as a string):
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
((h,j,k,m,o,q)=>(q={a:h=new Map([["name","a"],["children",[j=new Map([["name","c"],["right",k=new Map([["name","b"],["children",o=[,m=new Map([["name","d"]])]]])]]),m]]]),b:k,c:j,d:m},o[0]=j,j.set("left",h),m.set("left",h).set("right",k),q))()
|
|
106
|
+
|
|
107
|
+
// Formatted
|
|
108
|
+
((h, j, k, m, o, q) => (q = {
|
|
109
|
+
a: h = new Map([
|
|
110
|
+
["name", "a"],
|
|
111
|
+
["children", [j = new Map([
|
|
112
|
+
["name", "c"],
|
|
113
|
+
["right", k = new Map([
|
|
114
|
+
["name", "b"],
|
|
115
|
+
["children", o = [, m = new Map([
|
|
116
|
+
["name", "d"]
|
|
117
|
+
])]]
|
|
118
|
+
])]
|
|
119
|
+
]), m]]
|
|
120
|
+
]),
|
|
121
|
+
b: k,
|
|
122
|
+
c: j,
|
|
123
|
+
d: m
|
|
124
|
+
}, o[0] = j, j.set("left", h), m.set("left", h).set("right", k), q))()
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Supports
|
|
128
|
+
|
|
129
|
+
The following values are the only values accepted by `seroval`:
|
|
130
|
+
|
|
131
|
+
- Exact values
|
|
132
|
+
- `NaN`
|
|
133
|
+
- `Infinity`
|
|
134
|
+
- `-Infinity`
|
|
135
|
+
- `-0`
|
|
136
|
+
- Primitives
|
|
137
|
+
- `number`
|
|
138
|
+
- `string`
|
|
139
|
+
- `boolean`
|
|
140
|
+
- `null`
|
|
141
|
+
- `undefined`
|
|
142
|
+
- `bigint`
|
|
143
|
+
- `Array` + holes
|
|
144
|
+
- `Object`
|
|
145
|
+
- `RegExp`
|
|
146
|
+
- `Date`
|
|
147
|
+
- `Map`
|
|
148
|
+
- `Set`
|
|
149
|
+
- Cyclic references (both self and mutual)
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
MIT © [lxsmnsyc](https://github.com/lxsmnsyc)
|
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
default: () => serialize
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/array.ts
|
|
28
|
+
function forEach(arr, callback) {
|
|
29
|
+
for (let i = 0, len = arr.length; i < len; i++) {
|
|
30
|
+
if (callback(arr[i], i)) {
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function join(arr, joiner) {
|
|
36
|
+
let result = "";
|
|
37
|
+
forEach(arr, (value, index) => {
|
|
38
|
+
result += value;
|
|
39
|
+
if (index < arr.length - 1) {
|
|
40
|
+
result += joiner;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/get-identifier.ts
|
|
47
|
+
var REF_START_CHARS = "hjkmoquxzABCDEFGHIJKLNPQRTUVWXYZ$_";
|
|
48
|
+
var REF_START_CHARS_LEN = REF_START_CHARS.length;
|
|
49
|
+
var REF_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$_";
|
|
50
|
+
var REF_CHARS_LEN = REF_CHARS.length;
|
|
51
|
+
function getIdentifier(index) {
|
|
52
|
+
let mod = index % REF_START_CHARS_LEN;
|
|
53
|
+
let ref = REF_START_CHARS[mod];
|
|
54
|
+
index = (index - mod) / REF_START_CHARS_LEN;
|
|
55
|
+
while (index > 0) {
|
|
56
|
+
mod = index % REF_CHARS_LEN;
|
|
57
|
+
ref += REF_CHARS[mod];
|
|
58
|
+
index = (index - mod) / REF_CHARS_LEN;
|
|
59
|
+
}
|
|
60
|
+
return ref;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/quote.ts
|
|
64
|
+
function quote(str) {
|
|
65
|
+
let result = "";
|
|
66
|
+
let lastPos = 0;
|
|
67
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
|
68
|
+
let replacement;
|
|
69
|
+
switch (str[i]) {
|
|
70
|
+
case '"':
|
|
71
|
+
replacement = '\\"';
|
|
72
|
+
break;
|
|
73
|
+
case "\\":
|
|
74
|
+
replacement = "\\\\";
|
|
75
|
+
break;
|
|
76
|
+
case "<":
|
|
77
|
+
replacement = "\\x3C";
|
|
78
|
+
break;
|
|
79
|
+
case "\n":
|
|
80
|
+
replacement = "\\n";
|
|
81
|
+
break;
|
|
82
|
+
case "\r":
|
|
83
|
+
replacement = "\\r";
|
|
84
|
+
break;
|
|
85
|
+
case "\u2028":
|
|
86
|
+
replacement = "\\u2028";
|
|
87
|
+
break;
|
|
88
|
+
case "\u2029":
|
|
89
|
+
replacement = "\\u2029";
|
|
90
|
+
break;
|
|
91
|
+
default:
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
result += str.slice(lastPos, i) + replacement;
|
|
95
|
+
lastPos = i + 1;
|
|
96
|
+
}
|
|
97
|
+
if (lastPos === 0) {
|
|
98
|
+
result = str;
|
|
99
|
+
} else {
|
|
100
|
+
result += str.slice(lastPos);
|
|
101
|
+
}
|
|
102
|
+
return `"${result}"`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/index.ts
|
|
106
|
+
var EMPTY_SET = "new Set";
|
|
107
|
+
var EMPTY_MAP = "new Map";
|
|
108
|
+
var EMPTY_ARRAY = "[]";
|
|
109
|
+
var REF_COUNT = /* @__PURE__ */ new Map();
|
|
110
|
+
var STACK = /* @__PURE__ */ new Set();
|
|
111
|
+
var REFS = /* @__PURE__ */ new Map();
|
|
112
|
+
var ASSIGNED_REFS = /* @__PURE__ */ new Set();
|
|
113
|
+
var VARS = [];
|
|
114
|
+
var ASSIGNMENTS = [];
|
|
115
|
+
var MAP_SETS = [];
|
|
116
|
+
var SET_ADDS = [];
|
|
117
|
+
function reset() {
|
|
118
|
+
ASSIGNMENTS = [];
|
|
119
|
+
MAP_SETS = [];
|
|
120
|
+
SET_ADDS = [];
|
|
121
|
+
VARS = [];
|
|
122
|
+
REF_COUNT.clear();
|
|
123
|
+
REFS.clear();
|
|
124
|
+
ASSIGNED_REFS.clear();
|
|
125
|
+
}
|
|
126
|
+
function resolveAssignments() {
|
|
127
|
+
if (ASSIGNMENTS.length) {
|
|
128
|
+
const result = {};
|
|
129
|
+
forEach(ASSIGNMENTS, ([source, value]) => {
|
|
130
|
+
if (value in result) {
|
|
131
|
+
result[value] = `${source}=${result[value]}`;
|
|
132
|
+
} else {
|
|
133
|
+
result[value] = `${source}=${value}`;
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
return `${join(Object.values(result), ",")},`;
|
|
137
|
+
}
|
|
138
|
+
return "";
|
|
139
|
+
}
|
|
140
|
+
function resolveMapSets() {
|
|
141
|
+
if (MAP_SETS.length) {
|
|
142
|
+
const result = {};
|
|
143
|
+
forEach(MAP_SETS, ([source, key, value]) => {
|
|
144
|
+
if (source in result) {
|
|
145
|
+
result[source] = `${result[source]}.set(${key},${value})`;
|
|
146
|
+
} else {
|
|
147
|
+
result[source] = `${source}.set(${key},${value})`;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return `${join(Object.values(result), ",")},`;
|
|
151
|
+
}
|
|
152
|
+
return "";
|
|
153
|
+
}
|
|
154
|
+
function resolveSetAdds() {
|
|
155
|
+
if (SET_ADDS.length) {
|
|
156
|
+
const result = {};
|
|
157
|
+
forEach(SET_ADDS, ([source, value]) => {
|
|
158
|
+
if (source in result) {
|
|
159
|
+
result[source] = `${result[source]}.add(${value})`;
|
|
160
|
+
} else {
|
|
161
|
+
result[source] = `${source}.add(${value})`;
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
return `${join(Object.values(result), ",")},`;
|
|
165
|
+
}
|
|
166
|
+
return "";
|
|
167
|
+
}
|
|
168
|
+
function resolvePatches() {
|
|
169
|
+
return `${resolveAssignments()}${resolveMapSets()}${resolveSetAdds()}`;
|
|
170
|
+
}
|
|
171
|
+
function insertRef(current) {
|
|
172
|
+
const count = REF_COUNT.get(current) || 0;
|
|
173
|
+
REF_COUNT.set(current, count + 1);
|
|
174
|
+
return count === 0;
|
|
175
|
+
}
|
|
176
|
+
function constructorCheck(value, constructor) {
|
|
177
|
+
return value.constructor === constructor;
|
|
178
|
+
}
|
|
179
|
+
function isPrimitive(current) {
|
|
180
|
+
if (!current || current === true) {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
const type = typeof current;
|
|
184
|
+
const classConst = current.constructor;
|
|
185
|
+
return type === "number" || type === "string" || type === "bigint" || classConst === Date || classConst === RegExp;
|
|
186
|
+
}
|
|
187
|
+
function lookupRefs(current) {
|
|
188
|
+
if (isPrimitive(current)) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (Array.isArray(current)) {
|
|
192
|
+
if (insertRef(current)) {
|
|
193
|
+
forEach(current, lookupRefs);
|
|
194
|
+
}
|
|
195
|
+
} else if (constructorCheck(current, Set)) {
|
|
196
|
+
if (insertRef(current)) {
|
|
197
|
+
for (const item of current.keys()) {
|
|
198
|
+
lookupRefs(item);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
} else if (constructorCheck(current, Map)) {
|
|
202
|
+
if (insertRef(current)) {
|
|
203
|
+
for (const [key, value] of current.entries()) {
|
|
204
|
+
lookupRefs(key);
|
|
205
|
+
lookupRefs(value);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
} else if (current.constructor === Object && insertRef(current)) {
|
|
209
|
+
forEach(Object.values(current), lookupRefs);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function createRef(value) {
|
|
213
|
+
const current = REFS.get(value);
|
|
214
|
+
if (current != null) {
|
|
215
|
+
return current;
|
|
216
|
+
}
|
|
217
|
+
const id = REFS.size;
|
|
218
|
+
REFS.set(value, id);
|
|
219
|
+
return id;
|
|
220
|
+
}
|
|
221
|
+
function getRefParam(index) {
|
|
222
|
+
if (VARS[index]) {
|
|
223
|
+
return VARS[index];
|
|
224
|
+
}
|
|
225
|
+
const result = getIdentifier(index);
|
|
226
|
+
VARS[index] = result;
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
function assignRef(index, value) {
|
|
230
|
+
return `${getRefParam(index)}=${value}`;
|
|
231
|
+
}
|
|
232
|
+
function createAssignment(source, value) {
|
|
233
|
+
ASSIGNMENTS.push([source, value]);
|
|
234
|
+
}
|
|
235
|
+
function createSetAdd(ref, value) {
|
|
236
|
+
SET_ADDS.push([getRefParam(ref), value]);
|
|
237
|
+
}
|
|
238
|
+
function createMapSet(ref, key, value) {
|
|
239
|
+
MAP_SETS.push([getRefParam(ref), key, value]);
|
|
240
|
+
}
|
|
241
|
+
function createArrayAssign(ref, index, value) {
|
|
242
|
+
createAssignment(`${getRefParam(ref)}[${index}]`, value);
|
|
243
|
+
}
|
|
244
|
+
function createObjectIdentifierAssign(ref, key, value) {
|
|
245
|
+
createAssignment(`${getRefParam(ref)}.${key}`, value);
|
|
246
|
+
}
|
|
247
|
+
function createObjectStringAssign(ref, key, value) {
|
|
248
|
+
createAssignment(`${getRefParam(ref)}[${quote(key)}]`, value);
|
|
249
|
+
}
|
|
250
|
+
function serializePrimitive(value) {
|
|
251
|
+
if (value === true) {
|
|
252
|
+
return "!0";
|
|
253
|
+
}
|
|
254
|
+
if (value === false) {
|
|
255
|
+
return "!1";
|
|
256
|
+
}
|
|
257
|
+
if (value === void 0) {
|
|
258
|
+
return "void 0";
|
|
259
|
+
}
|
|
260
|
+
if (value === null) {
|
|
261
|
+
return "null";
|
|
262
|
+
}
|
|
263
|
+
if (Object.is(value, -0)) {
|
|
264
|
+
return "-0";
|
|
265
|
+
}
|
|
266
|
+
if (typeof value === "bigint") {
|
|
267
|
+
return `BigInt("${value}")`;
|
|
268
|
+
}
|
|
269
|
+
if (typeof value === "string") {
|
|
270
|
+
return quote(value);
|
|
271
|
+
}
|
|
272
|
+
if (value.constructor === Date) {
|
|
273
|
+
return `new Date("${value.toISOString()}")`;
|
|
274
|
+
}
|
|
275
|
+
return String(value);
|
|
276
|
+
}
|
|
277
|
+
function traverse(current) {
|
|
278
|
+
if (isPrimitive(current)) {
|
|
279
|
+
return serializePrimitive(current);
|
|
280
|
+
}
|
|
281
|
+
let refResult = REFS.get(current);
|
|
282
|
+
if (refResult != null) {
|
|
283
|
+
if (ASSIGNED_REFS.has(refResult)) {
|
|
284
|
+
return { referred: getRefParam(refResult) };
|
|
285
|
+
}
|
|
286
|
+
ASSIGNED_REFS.add(refResult);
|
|
287
|
+
}
|
|
288
|
+
if (constructorCheck(current, Set)) {
|
|
289
|
+
if (current.size) {
|
|
290
|
+
const values = [];
|
|
291
|
+
STACK.add(current);
|
|
292
|
+
for (const value2 of current.keys()) {
|
|
293
|
+
if (STACK.has(value2)) {
|
|
294
|
+
if (refResult == null) {
|
|
295
|
+
refResult = createRef(current);
|
|
296
|
+
}
|
|
297
|
+
createSetAdd(refResult, getRefParam(createRef(value2)));
|
|
298
|
+
} else {
|
|
299
|
+
const result = traverse(value2);
|
|
300
|
+
if (typeof result === "string") {
|
|
301
|
+
values.push(result);
|
|
302
|
+
} else {
|
|
303
|
+
values.push(result.referred);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
STACK.delete(current);
|
|
308
|
+
const value = values.length ? `new Set([${join(values, ",")}])` : EMPTY_SET;
|
|
309
|
+
if (refResult != null) {
|
|
310
|
+
return assignRef(refResult, value);
|
|
311
|
+
}
|
|
312
|
+
return value;
|
|
313
|
+
}
|
|
314
|
+
return EMPTY_SET;
|
|
315
|
+
}
|
|
316
|
+
if (constructorCheck(current, Map)) {
|
|
317
|
+
if (current.size) {
|
|
318
|
+
const values = [];
|
|
319
|
+
STACK.add(current);
|
|
320
|
+
for (const [key, value2] of current.entries()) {
|
|
321
|
+
if (STACK.has(key)) {
|
|
322
|
+
if (refResult == null) {
|
|
323
|
+
refResult = createRef(current);
|
|
324
|
+
}
|
|
325
|
+
const keyRef = getRefParam(createRef(key));
|
|
326
|
+
if (STACK.has(value2)) {
|
|
327
|
+
createMapSet(refResult, keyRef, getRefParam(createRef(value2)));
|
|
328
|
+
} else {
|
|
329
|
+
const valueResult = traverse(value2);
|
|
330
|
+
if (typeof valueResult === "string") {
|
|
331
|
+
createMapSet(refResult, keyRef, valueResult);
|
|
332
|
+
} else {
|
|
333
|
+
createMapSet(refResult, keyRef, valueResult.referred);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
} else if (STACK.has(value2)) {
|
|
337
|
+
if (refResult == null) {
|
|
338
|
+
refResult = createRef(current);
|
|
339
|
+
}
|
|
340
|
+
const valueRef = getRefParam(createRef(value2));
|
|
341
|
+
const keyResult = traverse(key);
|
|
342
|
+
if (typeof keyResult === "string") {
|
|
343
|
+
createMapSet(refResult, keyResult, valueRef);
|
|
344
|
+
} else {
|
|
345
|
+
createMapSet(refResult, keyResult.referred, valueRef);
|
|
346
|
+
}
|
|
347
|
+
} else {
|
|
348
|
+
const keyResult = traverse(key);
|
|
349
|
+
const valueResult = traverse(value2);
|
|
350
|
+
if (typeof keyResult === "string") {
|
|
351
|
+
if (typeof valueResult === "string") {
|
|
352
|
+
values.push(`[${keyResult},${valueResult}]`);
|
|
353
|
+
} else {
|
|
354
|
+
values.push(`[${keyResult},${valueResult.referred}]`);
|
|
355
|
+
}
|
|
356
|
+
} else {
|
|
357
|
+
if (typeof valueResult === "string") {
|
|
358
|
+
values.push(`[${keyResult.referred},${valueResult}]`);
|
|
359
|
+
} else {
|
|
360
|
+
values.push(`[${keyResult.referred},${valueResult.referred}]`);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
STACK.delete(current);
|
|
366
|
+
const value = values.length ? `new Map([${join(values, ",")}])` : EMPTY_MAP;
|
|
367
|
+
if (refResult != null) {
|
|
368
|
+
return assignRef(refResult, value);
|
|
369
|
+
}
|
|
370
|
+
return value;
|
|
371
|
+
}
|
|
372
|
+
return EMPTY_MAP;
|
|
373
|
+
}
|
|
374
|
+
if (Array.isArray(current)) {
|
|
375
|
+
if (current.length) {
|
|
376
|
+
let values = "";
|
|
377
|
+
STACK.add(current);
|
|
378
|
+
forEach(current, (item, i) => {
|
|
379
|
+
if (i in current) {
|
|
380
|
+
if (STACK.has(item)) {
|
|
381
|
+
if (refResult == null) {
|
|
382
|
+
refResult = createRef(current);
|
|
383
|
+
}
|
|
384
|
+
createArrayAssign(refResult, i, getRefParam(createRef(item)));
|
|
385
|
+
values += ",";
|
|
386
|
+
} else {
|
|
387
|
+
const result = traverse(item);
|
|
388
|
+
if (typeof result === "string") {
|
|
389
|
+
values += result;
|
|
390
|
+
} else {
|
|
391
|
+
values += result.referred;
|
|
392
|
+
}
|
|
393
|
+
if (i < current.length - 1) {
|
|
394
|
+
values += ",";
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
} else {
|
|
398
|
+
values += ",";
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
STACK.delete(current);
|
|
402
|
+
const value = `[${values}]`;
|
|
403
|
+
if (refResult != null) {
|
|
404
|
+
return assignRef(refResult, value);
|
|
405
|
+
}
|
|
406
|
+
return value;
|
|
407
|
+
}
|
|
408
|
+
return EMPTY_ARRAY;
|
|
409
|
+
}
|
|
410
|
+
if (current.constructor === Object) {
|
|
411
|
+
const values = [];
|
|
412
|
+
STACK.add(current);
|
|
413
|
+
forEach(Object.entries(current), ([key, value2]) => {
|
|
414
|
+
const check = Number(key);
|
|
415
|
+
if (Number.isNaN(check) || /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
|
|
416
|
+
if (STACK.has(value2)) {
|
|
417
|
+
if (refResult == null) {
|
|
418
|
+
refResult = createRef(current);
|
|
419
|
+
}
|
|
420
|
+
createObjectIdentifierAssign(refResult, key, getRefParam(createRef(value2)));
|
|
421
|
+
} else {
|
|
422
|
+
const result = traverse(value2);
|
|
423
|
+
if (typeof result === "string") {
|
|
424
|
+
values.push(`${key}:${result}`);
|
|
425
|
+
} else {
|
|
426
|
+
values.push(`${key}:${result.referred}`);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
} else if (STACK.has(value2)) {
|
|
430
|
+
if (refResult == null) {
|
|
431
|
+
refResult = createRef(current);
|
|
432
|
+
}
|
|
433
|
+
createObjectStringAssign(refResult, key, getRefParam(createRef(value2)));
|
|
434
|
+
} else {
|
|
435
|
+
const result = traverse(value2);
|
|
436
|
+
if (typeof result === "string") {
|
|
437
|
+
values.push(`${quote(key)}:${result}`);
|
|
438
|
+
} else {
|
|
439
|
+
values.push(`${quote(key)}:${result.referred}`);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
STACK.delete(current);
|
|
444
|
+
const value = `{${join(values, ",")}}`;
|
|
445
|
+
if (refResult != null) {
|
|
446
|
+
return assignRef(refResult, value);
|
|
447
|
+
}
|
|
448
|
+
return value;
|
|
449
|
+
}
|
|
450
|
+
throw new Error("Unserializable value");
|
|
451
|
+
}
|
|
452
|
+
function serialize(source) {
|
|
453
|
+
if (isPrimitive(source)) {
|
|
454
|
+
return serializePrimitive(source);
|
|
455
|
+
}
|
|
456
|
+
reset();
|
|
457
|
+
lookupRefs(source);
|
|
458
|
+
for (const [key, value] of REF_COUNT.entries()) {
|
|
459
|
+
if (value > 1) {
|
|
460
|
+
createRef(key);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const result = traverse(source);
|
|
464
|
+
if (typeof result !== "string") {
|
|
465
|
+
throw new Error("Unreachable error");
|
|
466
|
+
}
|
|
467
|
+
if (VARS.length) {
|
|
468
|
+
const index = getRefParam(createRef(source));
|
|
469
|
+
const patches = resolvePatches();
|
|
470
|
+
const params = VARS.length > 1 ? `(${join(VARS, ",")})` : VARS[0];
|
|
471
|
+
if (result.startsWith(index)) {
|
|
472
|
+
return `(${params}=>(${result},${patches}${index}))()`;
|
|
473
|
+
}
|
|
474
|
+
return `(${params}=>(${index}=${result},${patches}${index}))()`;
|
|
475
|
+
}
|
|
476
|
+
if (source.constructor === Object) {
|
|
477
|
+
return `(${result})`;
|
|
478
|
+
}
|
|
479
|
+
return result;
|
|
480
|
+
}
|
|
481
|
+
//# sourceMappingURL=index.cjs.map
|