seroval 0.4.0-alpha.3 → 0.4.0-alpha.4

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.
@@ -20,10 +20,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ compileJSON: () => compileJSON,
23
24
  default: () => src_default,
24
25
  deserialize: () => deserialize,
26
+ fromJSON: () => fromJSON,
25
27
  serialize: () => serialize,
26
- serializeAsync: () => serializeAsync
28
+ serializeAsync: () => serializeAsync,
29
+ toJSON: () => toJSON,
30
+ toJSONAsync: () => toJSONAsync
27
31
  });
28
32
  module.exports = __toCommonJS(src_exports);
29
33
 
@@ -33,7 +37,7 @@ function isPrimitive(current) {
33
37
  return true;
34
38
  }
35
39
  const type = typeof current;
36
- return type === "number" || type === "string" || type === "bigint";
40
+ return type === "number" || type === "string";
37
41
  }
38
42
  function isPromise(obj) {
39
43
  return !!obj && (typeof obj === "object" || typeof obj === "function") && "then" in obj && typeof obj.then === "function";
@@ -68,7 +72,7 @@ function compareVersion(left, right) {
68
72
  }
69
73
  var VERSION_TABLE = {
70
74
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError#browser_compatibility
71
- [0 /* AggregateError */]: {
75
+ [1 /* AggregateError */]: {
72
76
  es: [2021, 0, 0],
73
77
  chrome: [85, 0, 0],
74
78
  edge: [85, 0, 0],
@@ -81,7 +85,7 @@ var VERSION_TABLE = {
81
85
  node: [15, 0, 0]
82
86
  },
83
87
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values#browser_compatibility
84
- [1 /* ArrayPrototypeValues */]: {
88
+ [2 /* ArrayPrototypeValues */]: {
85
89
  es: [6, 0, 0],
86
90
  chrome: [66, 0, 0],
87
91
  edge: [14, 0, 0],
@@ -94,7 +98,7 @@ var VERSION_TABLE = {
94
98
  node: [10, 9, 0]
95
99
  },
96
100
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#browser_compatibility
97
- [2 /* ArrowFunction */]: {
101
+ [4 /* ArrowFunction */]: {
98
102
  es: [6, 0, 0],
99
103
  chrome: [45, 0, 0],
100
104
  edge: [12, 0, 0],
@@ -107,7 +111,7 @@ var VERSION_TABLE = {
107
111
  node: [4, 0, 0]
108
112
  },
109
113
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility
110
- [3 /* BigInt */]: {
114
+ [8 /* BigInt */]: {
111
115
  es: [2020, 0, 0],
112
116
  chrome: [67, 0, 0],
113
117
  edge: [79, 0, 0],
@@ -120,7 +124,7 @@ var VERSION_TABLE = {
120
124
  node: [10, 4, 0]
121
125
  },
122
126
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#browser_compatibility
123
- [4 /* Map */]: {
127
+ [16 /* Map */]: {
124
128
  es: [6, 0, 0],
125
129
  chrome: [38, 0, 0],
126
130
  edge: [12, 0, 0],
@@ -133,7 +137,7 @@ var VERSION_TABLE = {
133
137
  node: [0, 12, 0]
134
138
  },
135
139
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#browser_compatibility
136
- [5 /* MethodShorthand */]: {
140
+ [32 /* MethodShorthand */]: {
137
141
  es: [6, 0, 0],
138
142
  chrome: [39, 0, 0],
139
143
  edge: [12, 0, 0],
@@ -146,7 +150,7 @@ var VERSION_TABLE = {
146
150
  node: [4, 0, 0]
147
151
  },
148
152
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#browser_compatibility
149
- [6 /* ObjectAssign */]: {
153
+ [64 /* ObjectAssign */]: {
150
154
  es: [6, 0, 0],
151
155
  chrome: [45, 0, 0],
152
156
  edge: [12, 0, 0],
@@ -159,7 +163,7 @@ var VERSION_TABLE = {
159
163
  node: [4, 0, 0]
160
164
  },
161
165
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#browser_compatibility
162
- [7 /* Promise */]: {
166
+ [128 /* Promise */]: {
163
167
  es: [6, 0, 0],
164
168
  chrome: [32, 0, 0],
165
169
  edge: [12, 0, 0],
@@ -172,7 +176,7 @@ var VERSION_TABLE = {
172
176
  node: [0, 12, 0]
173
177
  },
174
178
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#browser_compatibility
175
- [8 /* Set */]: {
179
+ [256 /* Set */]: {
176
180
  es: [6, 0, 0],
177
181
  chrome: [38, 0, 0],
178
182
  edge: [12, 0, 0],
@@ -185,7 +189,7 @@ var VERSION_TABLE = {
185
189
  node: [0, 12, 0]
186
190
  },
187
191
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#browser_compatibility
188
- [9 /* SymbolIterator */]: {
192
+ [512 /* SymbolIterator */]: {
189
193
  es: [6, 0, 0],
190
194
  chrome: [43, 0, 0],
191
195
  edge: [12, 0, 0],
@@ -198,7 +202,7 @@ var VERSION_TABLE = {
198
202
  node: [0, 12, 0]
199
203
  },
200
204
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#browser_compatibility
201
- [10 /* TypedArray */]: {
205
+ [1024 /* TypedArray */]: {
202
206
  es: [6, 0, 0],
203
207
  chrome: [7, 0, 0],
204
208
  edge: [12, 0, 0],
@@ -209,6 +213,19 @@ var VERSION_TABLE = {
209
213
  samsung: [1, 0, 0],
210
214
  deno: [1, 0, 0],
211
215
  node: [0, 10, 0]
216
+ },
217
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array#browser_compatibility
218
+ [2048 /* BigIntTypedArray */]: {
219
+ es: [2020, 0, 0],
220
+ chrome: [67, 0, 0],
221
+ edge: [79, 0, 0],
222
+ firefox: [68, 0, 0],
223
+ opera: [54, 6, 0],
224
+ safari: [15, 1, 0],
225
+ ios: [15, 2, 0],
226
+ samsung: [9, 0, 0],
227
+ deno: [1, 0, 0],
228
+ node: [10, 4, 0]
212
229
  }
213
230
  };
214
231
  var TARGET_REGEX = /^(es|chrome|edge|safari|firefox|opera|ios|samsung|deno|node)([0-9]+(\.[0-9]+(\.[0-9]+)?)?)$/i;
@@ -231,7 +248,7 @@ function getTargetVersions(targets) {
231
248
  }
232
249
  function parseTargets(targets) {
233
250
  const parsed = getTargetVersions(targets);
234
- const flags = /* @__PURE__ */ new Set();
251
+ let flags = 0;
235
252
  for (const key in VERSION_TABLE) {
236
253
  const base = VERSION_TABLE[key];
237
254
  let flag = true;
@@ -240,7 +257,7 @@ function parseTargets(targets) {
240
257
  flag = flag && !!baseVersion && compareVersion(baseVersion, version) <= 0;
241
258
  }
242
259
  if (flag) {
243
- flags.add(Number(key));
260
+ flags |= Number(key);
244
261
  }
245
262
  }
246
263
  return flags;
@@ -309,16 +326,22 @@ function getIdentifier(index) {
309
326
  var DEFAULT_OPTIONS = {
310
327
  target: "es2023"
311
328
  };
312
- function createSerializationContext(options = {}) {
329
+ function createParserContext(options = {}) {
313
330
  const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
314
331
  return {
315
- markedRefs: [],
316
- stack: [],
332
+ markedRefs: /* @__PURE__ */ new Set(),
317
333
  refs: /* @__PURE__ */ new Map(),
334
+ features: parseTargets(result.target)
335
+ };
336
+ }
337
+ function createSerializationContext(options) {
338
+ return {
339
+ stack: [],
318
340
  vars: [],
319
341
  assignments: [],
320
342
  validRefs: /* @__PURE__ */ new Map(),
321
- features: parseTargets(result.target)
343
+ features: options.features,
344
+ markedRefs: new Set(options.markedRefs)
322
345
  };
323
346
  }
324
347
  function getAssignmentExpression(assignment) {
@@ -387,16 +410,7 @@ function resolvePatches(ctx) {
387
410
  return resolveAssignments(ctx.assignments);
388
411
  }
389
412
  function markRef(ctx, current) {
390
- ctx.markedRefs[current] = true;
391
- }
392
- function createRef(ctx, index) {
393
- const current = ctx.refs.get(index);
394
- if (current != null) {
395
- return current;
396
- }
397
- const id = ctx.refs.size;
398
- ctx.refs.set(index, id);
399
- return id;
413
+ ctx.markedRefs.add(current);
400
414
  }
401
415
  function createValidRef(ctx, index) {
402
416
  const current = ctx.validRefs.get(index);
@@ -417,11 +431,20 @@ function getRefParam(ctx, index) {
417
431
  return result;
418
432
  }
419
433
  function assignRef(ctx, index, value) {
420
- if (ctx.markedRefs[index]) {
434
+ if (ctx.markedRefs.has(index)) {
421
435
  return `${getRefParam(ctx, index)}=${value}`;
422
436
  }
423
437
  return value;
424
438
  }
439
+ function createRef(ctx, index) {
440
+ const current = ctx.refs.get(index);
441
+ if (current != null) {
442
+ return current;
443
+ }
444
+ const id = ctx.refs.size;
445
+ ctx.refs.set(index, id);
446
+ return id;
447
+ }
425
448
  function createAssignment(ctx, source, value) {
426
449
  ctx.assignments.push({
427
450
  type: "index",
@@ -450,13 +473,10 @@ function createArrayAssign(ctx, ref, index, value) {
450
473
  markRef(ctx, ref);
451
474
  createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
452
475
  }
453
- function createObjectIdentifierAssign(ctx, ref, key, value) {
476
+ function createObjectAssign(ctx, ref, key, value, computed) {
454
477
  markRef(ctx, ref);
455
- createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
456
- }
457
- function createObjectComputedAssign(ctx, ref, key, value) {
458
- markRef(ctx, ref);
459
- createAssignment(ctx, `${getRefParam(ctx, ref)}[${key}]`, value);
478
+ const member = computed ? `[${key}]` : `.${key}`;
479
+ createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
460
480
  }
461
481
  function getErrorConstructor(error) {
462
482
  if (error instanceof EvalError) {
@@ -508,75 +528,99 @@ function getIterableOptions(obj) {
508
528
  return void 0;
509
529
  }
510
530
  function isReferenceInStack(ctx, node) {
511
- return node[0] === 1 /* Reference */ && ctx.stack.includes(node[1]);
531
+ return node[0] === 2 /* Reference */ && ctx.stack.includes(node[1]);
512
532
  }
513
533
  function generateRef(ctx, current) {
514
534
  const ref = ctx.refs.has(current);
515
535
  if (ref) {
516
536
  const refID = ctx.refs.get(current) || 0;
517
537
  markRef(ctx, refID);
518
- return [1 /* Reference */, refID];
538
+ return [2 /* Reference */, refID];
519
539
  }
520
540
  const id = ctx.refs.size;
521
541
  ctx.refs.set(current, id);
522
542
  return id;
523
543
  }
524
544
  function generateSemiPrimitiveValue(ctx, current, id) {
545
+ if (typeof current === "bigint") {
546
+ assert(ctx.features & 8 /* BigInt */, 'Unsupported type "BigInt"');
547
+ return [1 /* BigInt */, `${current}n`];
548
+ }
525
549
  if (constructorCheck(current, Date)) {
526
- return [2 /* Date */, current, id];
550
+ return [3 /* Date */, current.toISOString(), id];
527
551
  }
528
552
  if (constructorCheck(current, RegExp)) {
529
- return [3 /* RegExp */, current, id];
553
+ return [4 /* RegExp */, String(current), id];
530
554
  }
531
555
  if (constructorCheck(current, Int8Array)) {
532
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int8Array"');
533
- return [13 /* TypedArray */, ["Int8Array", current], id];
556
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int8Array"');
557
+ return [14 /* TypedArray */, ["Int8Array", current.toString(), current.byteOffset], id];
534
558
  }
535
559
  if (constructorCheck(current, Int16Array)) {
536
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int16Array"');
537
- return [13 /* TypedArray */, ["Int16Array", current], id];
560
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int16Array"');
561
+ return [14 /* TypedArray */, ["Int16Array", current.toString(), current.byteOffset], id];
538
562
  }
539
563
  if (constructorCheck(current, Int32Array)) {
540
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Int32Array"');
541
- return [13 /* TypedArray */, ["Int32Array", current], id];
564
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Int32Array"');
565
+ return [14 /* TypedArray */, ["Int32Array", current.toString(), current.byteOffset], id];
542
566
  }
543
567
  if (constructorCheck(current, Uint8Array)) {
544
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8Array"');
545
- return [13 /* TypedArray */, ["Uint8Array", current], id];
568
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8Array"');
569
+ return [14 /* TypedArray */, ["Uint8Array", current.toString(), current.byteOffset], id];
546
570
  }
547
571
  if (constructorCheck(current, Uint16Array)) {
548
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint16Array"');
549
- return [13 /* TypedArray */, ["Uint16Array", current], id];
572
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint16Array"');
573
+ return [14 /* TypedArray */, ["Uint16Array", current.toString(), current.byteOffset], id];
550
574
  }
551
575
  if (constructorCheck(current, Uint32Array)) {
552
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint32Array"');
553
- return [13 /* TypedArray */, ["Uint32Array", current], id];
576
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint32Array"');
577
+ return [14 /* TypedArray */, ["Uint32Array", current.toString(), current.byteOffset], id];
554
578
  }
555
579
  if (constructorCheck(current, Uint8ClampedArray)) {
556
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Uint8ClampedArray"');
557
- return [13 /* TypedArray */, ["Uint8ClampedArray", current], id];
580
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Uint8ClampedArray"');
581
+ return [14 /* TypedArray */, ["Uint8ClampedArray", current.toString(), current.byteOffset], id];
558
582
  }
559
583
  if (constructorCheck(current, Float32Array)) {
560
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float32Array"');
561
- return [13 /* TypedArray */, ["Float32Array", current], id];
584
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float32Array"');
585
+ return [14 /* TypedArray */, ["Float32Array", current.toString(), current.byteOffset], id];
562
586
  }
563
587
  if (constructorCheck(current, Float64Array)) {
564
- assert(ctx.features.has(10 /* TypedArray */), 'Unsupported value type "Float64Array"');
565
- return [13 /* TypedArray */, ["Float64Array", current], id];
588
+ assert(ctx.features & 1024 /* TypedArray */, 'Unsupported value type "Float64Array"');
589
+ return [14 /* TypedArray */, ["Float64Array", current.toString(), current.byteOffset], id];
566
590
  }
567
591
  if (constructorCheck(current, BigInt64Array)) {
568
592
  assert(
569
- ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
593
+ ctx.features & 2048 /* BigIntTypedArray */,
570
594
  'Unsupported value type "BigInt64Array"'
571
595
  );
572
- return [13 /* TypedArray */, ["BigInt64Array", current], id];
596
+ let result = "";
597
+ const cap = current.length - 1;
598
+ for (let i = 0; i < cap; i++) {
599
+ result += `${current[i]}n,`;
600
+ }
601
+ result += `"${current[cap]}"`;
602
+ return [
603
+ 15 /* BigIntTypedArray */,
604
+ ["BigInt64Array", result, current.byteOffset],
605
+ id
606
+ ];
573
607
  }
574
608
  if (constructorCheck(current, BigUint64Array)) {
575
609
  assert(
576
- ctx.features.has(10 /* TypedArray */) && ctx.features.has(3 /* BigInt */),
610
+ ctx.features & 2048 /* BigIntTypedArray */,
577
611
  'Unsupported value type "BigUint64Array"'
578
612
  );
579
- return [13 /* TypedArray */, ["BigUint64Array", current], id];
613
+ let result = "";
614
+ const cap = current.length - 1;
615
+ for (let i = 0; i < cap; i++) {
616
+ result += `"${current[i]}n",`;
617
+ }
618
+ result += `"${current[cap]}"`;
619
+ return [
620
+ 15 /* BigIntTypedArray */,
621
+ ["BigInt64Array", result, current.byteOffset],
622
+ id
623
+ ];
580
624
  }
581
625
  return void 0;
582
626
  }
@@ -608,7 +652,7 @@ function serializePropertiesSync(ctx, properties) {
608
652
  }
609
653
  function generateTreeSync(ctx, current) {
610
654
  if (isPrimitive(current)) {
611
- return [0 /* Primitive */, current];
655
+ return [0 /* Primitive */, serializePrimitive(current)];
612
656
  }
613
657
  const id = generateRef(ctx, current);
614
658
  if (Array.isArray(id)) {
@@ -619,7 +663,7 @@ function generateTreeSync(ctx, current) {
619
663
  return semiPrimitive;
620
664
  }
621
665
  if (constructorCheck(current, Set)) {
622
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
666
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
623
667
  const len = current.size;
624
668
  const nodes = new Array(len);
625
669
  const deferred = new Array(len);
@@ -635,10 +679,10 @@ function generateTreeSync(ctx, current) {
635
679
  for (let i = 0; i < deferredSize; i++) {
636
680
  nodes[nodeSize + i] = generateTreeSync(ctx, deferred[i]);
637
681
  }
638
- return [4 /* Set */, nodes, id];
682
+ return [5 /* Set */, nodes, id];
639
683
  }
640
684
  if (constructorCheck(current, Map)) {
641
- assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
685
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
642
686
  const len = current.size;
643
687
  const keyNodes = new Array(len);
644
688
  const valueNodes = new Array(len);
@@ -661,7 +705,7 @@ function generateTreeSync(ctx, current) {
661
705
  keyNodes[nodeSize + i] = generateTreeSync(ctx, deferredKey[i]);
662
706
  valueNodes[nodeSize + i] = generateTreeSync(ctx, deferredValue[i]);
663
707
  }
664
- return [5 /* Map */, [keyNodes, valueNodes, len], id];
708
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
665
709
  }
666
710
  if (Array.isArray(current)) {
667
711
  const size = current.length;
@@ -681,27 +725,27 @@ function generateTreeSync(ctx, current) {
681
725
  nodes[i] = generateTreeSync(ctx, deferred[i]);
682
726
  }
683
727
  }
684
- return [6 /* Array */, nodes, id];
728
+ return [7 /* Array */, nodes, id];
685
729
  }
686
- if (current instanceof AggregateError && ctx.features.has(0 /* AggregateError */)) {
730
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
687
731
  const options = getErrorOptions(current);
688
732
  const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
689
733
  const errorsNode = generateTreeSync(ctx, current.errors);
690
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
734
+ return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
691
735
  }
692
736
  if (current instanceof Error) {
693
737
  const options = getErrorOptions(current);
694
738
  const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
695
739
  return [
696
- 10 /* Error */,
740
+ 11 /* Error */,
697
741
  [getErrorConstructor(current), current.message, optionsNode],
698
742
  id
699
743
  ];
700
744
  }
701
745
  if (isIterable(current)) {
702
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
746
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
703
747
  const options = getIterableOptions(current);
704
- return [12 /* Iterable */, [
748
+ return [13 /* Iterable */, [
705
749
  // Parse options first before the items
706
750
  options ? serializePropertiesSync(ctx, options) : void 0,
707
751
  generateTreeSync(ctx, Array.from(current))
@@ -710,13 +754,17 @@ function generateTreeSync(ctx, current) {
710
754
  const empty = current.constructor == null;
711
755
  if (current.constructor === Object || empty) {
712
756
  return [
713
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
757
+ empty ? 9 /* NullConstructor */ : 8 /* Object */,
714
758
  serializePropertiesSync(ctx, current),
715
759
  id
716
760
  ];
717
761
  }
718
762
  throw new Error("Unsupported value");
719
763
  }
764
+ function parseSync(ctx, current) {
765
+ const result = generateTreeSync(ctx, current);
766
+ return [result, createRef(ctx, current), result[0] === 8 /* Object */];
767
+ }
720
768
  async function serializePropertiesAsync(ctx, properties) {
721
769
  const keys = Object.keys(properties);
722
770
  const size = keys.length;
@@ -745,7 +793,7 @@ async function serializePropertiesAsync(ctx, properties) {
745
793
  }
746
794
  async function generateTreeAsync(ctx, current) {
747
795
  if (isPrimitive(current)) {
748
- return [0 /* Primitive */, current];
796
+ return [0 /* Primitive */, serializePrimitive(current)];
749
797
  }
750
798
  const id = generateRef(ctx, current);
751
799
  if (Array.isArray(id)) {
@@ -756,15 +804,15 @@ async function generateTreeAsync(ctx, current) {
756
804
  return semiPrimitive;
757
805
  }
758
806
  if (isPromise(current)) {
759
- assert(ctx.features.has(7 /* Promise */), 'Unsupported type "Promise"');
807
+ assert(ctx.features & 128 /* Promise */, 'Unsupported type "Promise"');
760
808
  return current.then(async (value) => [
761
- 9 /* Promise */,
809
+ 10 /* Promise */,
762
810
  await generateTreeAsync(ctx, value),
763
811
  id
764
812
  ]);
765
813
  }
766
814
  if (constructorCheck(current, Set)) {
767
- assert(ctx.features.has(8 /* Set */), 'Unsupported type "Set"');
815
+ assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
768
816
  const len = current.size;
769
817
  const nodes = new Array(len);
770
818
  const deferred = new Array(len);
@@ -780,10 +828,10 @@ async function generateTreeAsync(ctx, current) {
780
828
  for (let i = 0; i < deferredSize; i++) {
781
829
  nodes[nodeSize + i] = await generateTreeAsync(ctx, deferred[i]);
782
830
  }
783
- return [4 /* Set */, nodes, id];
831
+ return [5 /* Set */, nodes, id];
784
832
  }
785
833
  if (constructorCheck(current, Map)) {
786
- assert(ctx.features.has(4 /* Map */), 'Unsupported type "Map"');
834
+ assert(ctx.features & 16 /* Map */, 'Unsupported type "Map"');
787
835
  const len = current.size;
788
836
  const keyNodes = new Array(len);
789
837
  const valueNodes = new Array(len);
@@ -806,7 +854,7 @@ async function generateTreeAsync(ctx, current) {
806
854
  keyNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredKey[i]);
807
855
  valueNodes[nodeSize + i] = await generateTreeAsync(ctx, deferredValue[i]);
808
856
  }
809
- return [5 /* Map */, [keyNodes, valueNodes, len], id];
857
+ return [6 /* Map */, [keyNodes, valueNodes, len], id];
810
858
  }
811
859
  if (Array.isArray(current)) {
812
860
  const size = current.length;
@@ -827,27 +875,27 @@ async function generateTreeAsync(ctx, current) {
827
875
  nodes[i] = await generateTreeAsync(ctx, deferred[i]);
828
876
  }
829
877
  }
830
- return [6 /* Array */, nodes, id];
878
+ return [7 /* Array */, nodes, id];
831
879
  }
832
- if (current instanceof AggregateError) {
880
+ if (current instanceof AggregateError && ctx.features & 1 /* AggregateError */) {
833
881
  const options = getErrorOptions(current);
834
882
  const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
835
883
  const errorsNode = await generateTreeAsync(ctx, current.errors);
836
- return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
884
+ return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
837
885
  }
838
886
  if (current instanceof Error) {
839
887
  const options = getErrorOptions(current);
840
888
  const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
841
889
  return [
842
- 10 /* Error */,
890
+ 11 /* Error */,
843
891
  [getErrorConstructor(current), current.message, optionsNode],
844
892
  id
845
893
  ];
846
894
  }
847
895
  if (isIterable(current)) {
848
- assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
896
+ assert(ctx.features & 512 /* SymbolIterator */, 'Unsupported type "Iterable"');
849
897
  const options = getIterableOptions(current);
850
- return [12 /* Iterable */, [
898
+ return [13 /* Iterable */, [
851
899
  options ? await serializePropertiesAsync(ctx, options) : void 0,
852
900
  await generateTreeAsync(ctx, Array.from(current))
853
901
  ], id];
@@ -855,14 +903,18 @@ async function generateTreeAsync(ctx, current) {
855
903
  const empty = current.constructor == null;
856
904
  if (current.constructor === Object || empty) {
857
905
  return [
858
- empty ? 8 /* NullConstructor */ : 7 /* Object */,
906
+ empty ? 9 /* NullConstructor */ : 8 /* Object */,
859
907
  await serializePropertiesAsync(ctx, current),
860
908
  id
861
909
  ];
862
910
  }
863
911
  throw new Error("Unsupported value");
864
912
  }
865
- function serializePrimitive(ctx, value) {
913
+ async function parseAsync(ctx, current) {
914
+ const result = await generateTreeAsync(ctx, current);
915
+ return [result, createRef(ctx, current), result[0] === 8 /* Object */];
916
+ }
917
+ function serializePrimitive(value) {
866
918
  if (value === true) {
867
919
  return "!0";
868
920
  }
@@ -873,11 +925,7 @@ function serializePrimitive(ctx, value) {
873
925
  return "void 0";
874
926
  }
875
927
  if (value === null) {
876
- return "null";
877
- }
878
- if (typeof value === "bigint") {
879
- assert(ctx.features.has(3 /* BigInt */), 'Unsupported type "BigInt"');
880
- return `${value}n`;
928
+ return null;
881
929
  }
882
930
  if (typeof value === "string") {
883
931
  return quote(value);
@@ -891,7 +939,7 @@ function serializePrimitive(ctx, value) {
891
939
  if (Object.is(value, -Infinity)) {
892
940
  return "-1/0";
893
941
  }
894
- return String(value);
942
+ return value;
895
943
  }
896
944
  var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
897
945
  function serializeAssignments(ctx, targetRef, [keys, values, size]) {
@@ -906,15 +954,14 @@ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
906
954
  const check = Number(key);
907
955
  const parentAssignment = ctx.assignments;
908
956
  ctx.assignments = mainAssignments;
909
- if (IDENTIFIER_CHECK.test(key) || check >= 0) {
910
- if (!Number.isNaN(check)) {
911
- createObjectComputedAssign(ctx, targetRef, key, refParam);
912
- } else {
913
- createObjectIdentifierAssign(ctx, targetRef, key, refParam);
914
- }
915
- } else {
916
- createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
917
- }
957
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
958
+ createObjectAssign(
959
+ ctx,
960
+ targetRef,
961
+ isIdentifier ? key : quote(key),
962
+ refParam,
963
+ !(isIdentifier && Number.isNaN(check))
964
+ );
918
965
  ctx.assignments = parentAssignment;
919
966
  }
920
967
  ctx.stack.pop();
@@ -927,22 +974,19 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
927
974
  const key = keys[i];
928
975
  const val = values[i];
929
976
  const check = Number(key);
930
- const isIdentifier = IDENTIFIER_CHECK.test(key) || check >= 0;
977
+ const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
978
+ const validKey = isIdentifier ? key : quote(key);
931
979
  if (isReferenceInStack(ctx, val)) {
932
980
  const refParam = getRefParam(ctx, val[1]);
933
- if (isIdentifier) {
934
- if (!Number.isNaN(check)) {
935
- createObjectComputedAssign(ctx, sourceID, key, refParam);
936
- } else {
937
- createObjectIdentifierAssign(ctx, sourceID, key, refParam);
938
- }
939
- } else {
940
- createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
941
- }
942
- } else if (isIdentifier) {
943
- result += `${key}:${serializeTree(ctx, val)},`;
981
+ createObjectAssign(
982
+ ctx,
983
+ sourceID,
984
+ validKey,
985
+ refParam,
986
+ !(isIdentifier && Number.isNaN(check))
987
+ );
944
988
  } else {
945
- result += `${quote(key)}:${serializeTree(ctx, val)},`;
989
+ result += `${validKey}:${serializeTree(ctx, val)},`;
946
990
  }
947
991
  }
948
992
  ctx.stack.pop();
@@ -951,10 +995,10 @@ function serializeObject(ctx, sourceID, [keys, values, size]) {
951
995
  function serializeTree(ctx, [type, value, id]) {
952
996
  switch (type) {
953
997
  case 0 /* Primitive */:
954
- return serializePrimitive(ctx, value);
955
- case 1 /* Reference */:
998
+ return String(value);
999
+ case 2 /* Reference */:
956
1000
  return getRefParam(ctx, value);
957
- case 9 /* Promise */: {
1001
+ case 10 /* Promise */: {
958
1002
  let serialized;
959
1003
  if (isReferenceInStack(ctx, value)) {
960
1004
  serialized = `Promise.resolve().then(()=>${getRefParam(ctx, value[1])})`;
@@ -966,22 +1010,21 @@ function serializeTree(ctx, [type, value, id]) {
966
1010
  }
967
1011
  return assignRef(ctx, id, serialized);
968
1012
  }
969
- case 2 /* Date */:
970
- return assignRef(ctx, id, `new Date("${value.toISOString()}")`);
971
- case 3 /* RegExp */:
972
- return assignRef(ctx, id, String(value));
973
- case 13 /* TypedArray */: {
974
- let values = "";
975
- for (let i = 0, len = value[1].length; i < len; i++) {
976
- values += `${serializePrimitive(ctx, value[1][i])},`;
977
- }
978
- let args = values ? `[${values.substring(0, values.length - 1)}]` : "[]";
979
- if (value[1].byteOffset !== 0) {
980
- args += `,${value[1].byteOffset}`;
1013
+ case 1 /* BigInt */:
1014
+ return value;
1015
+ case 3 /* Date */:
1016
+ return assignRef(ctx, id, `new Date("${value}")`);
1017
+ case 4 /* RegExp */:
1018
+ return assignRef(ctx, id, value);
1019
+ case 15 /* BigIntTypedArray */:
1020
+ case 14 /* TypedArray */: {
1021
+ let args = `[${value[1]}]`;
1022
+ if (value[2] !== 0) {
1023
+ args += `,${value[2]}`;
981
1024
  }
982
1025
  return assignRef(ctx, id, `new ${value[0]}(${args})`);
983
1026
  }
984
- case 4 /* Set */: {
1027
+ case 5 /* Set */: {
985
1028
  let serialized = "new Set";
986
1029
  const size = value.length;
987
1030
  if (size) {
@@ -1002,7 +1045,7 @@ function serializeTree(ctx, [type, value, id]) {
1002
1045
  }
1003
1046
  return assignRef(ctx, id, serialized);
1004
1047
  }
1005
- case 5 /* Map */: {
1048
+ case 6 /* Map */: {
1006
1049
  let serialized = "new Map";
1007
1050
  const size = value[2];
1008
1051
  if (size) {
@@ -1039,13 +1082,13 @@ function serializeTree(ctx, [type, value, id]) {
1039
1082
  }
1040
1083
  return assignRef(ctx, id, serialized);
1041
1084
  }
1042
- case 6 /* Array */: {
1085
+ case 7 /* Array */: {
1043
1086
  const size = value.length;
1044
1087
  let values = "";
1045
1088
  ctx.stack.push(id);
1046
1089
  for (let i = 0; i < size; i++) {
1047
1090
  const item = value[i];
1048
- if (i in value) {
1091
+ if (item) {
1049
1092
  if (isReferenceInStack(ctx, item)) {
1050
1093
  createArrayAssign(ctx, id, i, getRefParam(ctx, item[1]));
1051
1094
  values += ",";
@@ -1062,12 +1105,12 @@ function serializeTree(ctx, [type, value, id]) {
1062
1105
  ctx.stack.pop();
1063
1106
  return assignRef(ctx, id, `[${values}]`);
1064
1107
  }
1065
- case 11 /* AggregateError */: {
1108
+ case 12 /* AggregateError */: {
1066
1109
  ctx.stack.push(id);
1067
1110
  let serialized = `new AggregateError(${serializeTree(ctx, value[2])},${quote(value[0])})`;
1068
1111
  ctx.stack.pop();
1069
1112
  if (value[1]) {
1070
- if (ctx.features.has(6 /* ObjectAssign */)) {
1113
+ if (ctx.features & 64 /* ObjectAssign */) {
1071
1114
  const options = serializeObject(ctx, id, value[1]);
1072
1115
  serialized = `Object.assign(${serialized},${options})`;
1073
1116
  } else {
@@ -1081,10 +1124,10 @@ function serializeTree(ctx, [type, value, id]) {
1081
1124
  }
1082
1125
  return assignRef(ctx, id, serialized);
1083
1126
  }
1084
- case 10 /* Error */: {
1127
+ case 11 /* Error */: {
1085
1128
  let serialized = `new ${value[0]}(${quote(value[1])})`;
1086
1129
  if (value[2]) {
1087
- if (ctx.features.has(6 /* ObjectAssign */)) {
1130
+ if (ctx.features & 64 /* ObjectAssign */) {
1088
1131
  const options = serializeObject(ctx, id, value[2]);
1089
1132
  serialized = `Object.assign(${serialized},${options})`;
1090
1133
  } else {
@@ -1098,26 +1141,26 @@ function serializeTree(ctx, [type, value, id]) {
1098
1141
  }
1099
1142
  return assignRef(ctx, id, serialized);
1100
1143
  }
1101
- case 12 /* Iterable */: {
1144
+ case 13 /* Iterable */: {
1102
1145
  const parent = ctx.stack;
1103
1146
  ctx.stack = [];
1104
1147
  const values = serializeTree(ctx, value[1]);
1105
1148
  ctx.stack = parent;
1106
1149
  let serialized;
1107
- if (ctx.features.has(1 /* ArrayPrototypeValues */)) {
1150
+ if (ctx.features & 2 /* ArrayPrototypeValues */) {
1108
1151
  serialized = `${values}.values()`;
1109
1152
  } else {
1110
1153
  serialized = `${values}[Symbol.iterator]()`;
1111
1154
  }
1112
- if (ctx.features.has(2 /* ArrowFunction */)) {
1155
+ if (ctx.features & 4 /* ArrowFunction */) {
1113
1156
  serialized = `{[Symbol.iterator]:()=>${serialized}}`;
1114
- } else if (ctx.features.has(5 /* MethodShorthand */)) {
1157
+ } else if (ctx.features & 32 /* MethodShorthand */) {
1115
1158
  serialized = `{[Symbol.iterator](){return ${serialized}}}`;
1116
1159
  } else {
1117
1160
  serialized = `{[Symbol.iterator]:function(){return ${serialized}}}`;
1118
1161
  }
1119
1162
  if (value[0]) {
1120
- if (ctx.features.has(6 /* ObjectAssign */)) {
1163
+ if (ctx.features & 64 /* ObjectAssign */) {
1121
1164
  const options = serializeObject(ctx, id, value[0]);
1122
1165
  serialized = `Object.assign(${serialized},${options})`;
1123
1166
  } else {
@@ -1131,9 +1174,9 @@ function serializeTree(ctx, [type, value, id]) {
1131
1174
  }
1132
1175
  return assignRef(ctx, id, serialized);
1133
1176
  }
1134
- case 8 /* NullConstructor */: {
1177
+ case 9 /* NullConstructor */: {
1135
1178
  let serialized = "Object.create(null)";
1136
- if (ctx.features.has(6 /* ObjectAssign */)) {
1179
+ if (ctx.features & 64 /* ObjectAssign */) {
1137
1180
  const fields = serializeObject(ctx, id, value);
1138
1181
  if (fields !== "{}") {
1139
1182
  serialized = `Object.assign(${serialized},${fields})`;
@@ -1148,7 +1191,7 @@ function serializeTree(ctx, [type, value, id]) {
1148
1191
  }
1149
1192
  return assignRef(ctx, id, serialized);
1150
1193
  }
1151
- case 7 /* Object */:
1194
+ case 8 /* Object */:
1152
1195
  return assignRef(ctx, id, serializeObject(ctx, id, value));
1153
1196
  default:
1154
1197
  throw new Error("Unsupported type");
@@ -1156,12 +1199,12 @@ function serializeTree(ctx, [type, value, id]) {
1156
1199
  }
1157
1200
 
1158
1201
  // src/index.ts
1159
- function finalize(ctx, source, result) {
1202
+ function finalize(ctx, rootID, isObject, result) {
1160
1203
  if (ctx.vars.length) {
1161
1204
  const patches = resolvePatches(ctx);
1162
1205
  let body = result;
1163
1206
  if (patches) {
1164
- const index = getRefParam(ctx, createRef(ctx, source));
1207
+ const index = getRefParam(ctx, rootID);
1165
1208
  if (result.startsWith(`${index}=`)) {
1166
1209
  body = `${result},${patches}${index}`;
1167
1210
  } else {
@@ -1169,37 +1212,73 @@ function finalize(ctx, source, result) {
1169
1212
  }
1170
1213
  }
1171
1214
  let params = ctx.vars.length > 1 ? ctx.vars.join(",") : ctx.vars[0];
1172
- if (ctx.features.has(2 /* ArrowFunction */)) {
1215
+ if (ctx.features & 4 /* ArrowFunction */) {
1173
1216
  params = ctx.vars.length > 1 || ctx.vars.length === 0 ? `(${params})` : params;
1174
1217
  return `(${params}=>(${body}))()`;
1175
1218
  }
1176
1219
  return `(function(${params}){return ${body}})()`;
1177
1220
  }
1178
- if (source.constructor === Object) {
1221
+ if (isObject) {
1179
1222
  return `(${result})`;
1180
1223
  }
1181
1224
  return result;
1182
1225
  }
1183
1226
  function serialize(source, options) {
1184
- const ctx = createSerializationContext(options);
1227
+ const ctx = createParserContext(options);
1185
1228
  if (isPrimitive(source)) {
1186
- return serializePrimitive(ctx, source);
1229
+ return String(serializePrimitive(source));
1187
1230
  }
1188
- const tree = generateTreeSync(ctx, source);
1189
- const result = serializeTree(ctx, tree);
1190
- return finalize(ctx, source, result);
1231
+ const [tree, rootID, isObject] = parseSync(ctx, source);
1232
+ const serial = createSerializationContext(ctx);
1233
+ const result = serializeTree(serial, tree);
1234
+ return finalize(serial, rootID, isObject, result);
1191
1235
  }
1192
1236
  async function serializeAsync(source, options) {
1193
- const ctx = createSerializationContext(options);
1237
+ const ctx = createParserContext(options);
1194
1238
  if (isPrimitive(source)) {
1195
- return serializePrimitive(ctx, source);
1239
+ return String(serializePrimitive(source));
1196
1240
  }
1197
- const tree = await generateTreeAsync(ctx, source);
1198
- const result = serializeTree(ctx, tree);
1199
- return finalize(ctx, source, result);
1241
+ const [tree, rootID, isObject] = await parseAsync(ctx, source);
1242
+ const serial = createSerializationContext(ctx);
1243
+ const result = serializeTree(serial, tree);
1244
+ return finalize(serial, rootID, isObject, result);
1200
1245
  }
1201
1246
  function deserialize(source) {
1202
1247
  return (0, eval)(source);
1203
1248
  }
1249
+ function toJSON(source, options) {
1250
+ const ctx = createParserContext(options);
1251
+ const [tree, root, isObject] = parseSync(ctx, source);
1252
+ return JSON.stringify([
1253
+ tree,
1254
+ root,
1255
+ isObject,
1256
+ ctx.features,
1257
+ [...ctx.markedRefs]
1258
+ ]);
1259
+ }
1260
+ async function toJSONAsync(source, options) {
1261
+ const ctx = createParserContext(options);
1262
+ const [tree, root, isObject] = await parseAsync(ctx, source);
1263
+ return JSON.stringify([
1264
+ tree,
1265
+ root,
1266
+ isObject,
1267
+ ctx.features,
1268
+ [...ctx.markedRefs]
1269
+ ]);
1270
+ }
1271
+ function compileJSON(source) {
1272
+ const parsed = JSON.parse(source);
1273
+ const serial = createSerializationContext({
1274
+ features: parsed[3],
1275
+ markedRefs: parsed[4]
1276
+ });
1277
+ const result = serializeTree(serial, parsed[0]);
1278
+ return finalize(serial, parsed[1], parsed[2], result);
1279
+ }
1280
+ function fromJSON(source) {
1281
+ return deserialize(compileJSON(source));
1282
+ }
1204
1283
  var src_default = serialize;
1205
1284
  //# sourceMappingURL=index.cjs.map