seroval 0.4.0-alpha.6 → 0.4.0-alpha.8

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.
@@ -254,7 +254,7 @@ var DEFAULT_OPTIONS = {
254
254
  function createParserContext(options = {}) {
255
255
  const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
256
256
  return {
257
- markedRefs: /* @__PURE__ */ new Set(),
257
+ markedRefs: {},
258
258
  refs: /* @__PURE__ */ new Map(),
259
259
  features: parseTargets(result.target)
260
260
  };
@@ -264,22 +264,23 @@ function createSerializationContext(options) {
264
264
  stack: [],
265
265
  vars: [],
266
266
  assignments: [],
267
- validRefs: /* @__PURE__ */ new Map(),
267
+ validRefs: [],
268
+ refSize: 0,
268
269
  features: options.features,
269
- markedRefs: new Set(options.markedRefs)
270
+ markedRefs: options.markedRefs
270
271
  };
271
272
  }
272
273
  function markRef(ctx, current) {
273
- ctx.markedRefs.add(current);
274
+ ctx.markedRefs[current] = 1;
274
275
  }
275
276
  function createValidRef(ctx, index) {
276
- const current = ctx.validRefs.get(index);
277
- if (current != null) {
278
- return current;
277
+ const current = ctx.validRefs[index];
278
+ if (current == null) {
279
+ const value = ctx.refSize++;
280
+ ctx.validRefs[index] = value;
281
+ return value;
279
282
  }
280
- const id = ctx.validRefs.size;
281
- ctx.validRefs.set(index, id);
282
- return id;
283
+ return current;
283
284
  }
284
285
  function getRefParam(ctx, index) {
285
286
  const actualIndex = createValidRef(ctx, index);
@@ -471,21 +472,31 @@ function serializePrimitive(value) {
471
472
  }
472
473
 
473
474
  // src/tree/async.ts
474
- async function generateDictionary(ctx, properties) {
475
+ async function serializePropertiesAsync(ctx, properties) {
475
476
  const keys = Object.keys(properties);
476
- const nodes = {};
477
- const deferred = {};
477
+ const size = keys.length;
478
+ const keyNodes = new Array(size);
479
+ const valueNodes = new Array(size);
480
+ const deferredKeys = new Array(size);
481
+ const deferredValues = new Array(size);
482
+ let deferredSize = 0;
483
+ let nodesSize = 0;
478
484
  for (const key of keys) {
479
485
  if (isIterable(properties[key])) {
480
- deferred[key] = properties[key];
486
+ deferredKeys[deferredSize] = key;
487
+ deferredValues[deferredSize] = properties[key];
488
+ deferredSize++;
481
489
  } else {
482
- nodes[key] = await generateTreeAsync(ctx, properties[key]);
490
+ keyNodes[nodesSize] = key;
491
+ valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
492
+ nodesSize++;
483
493
  }
484
494
  }
485
- for (const key of Object.keys(deferred)) {
486
- nodes[key] = await generateTreeAsync(ctx, deferred[key]);
495
+ for (let i = 0; i < deferredSize; i++) {
496
+ keyNodes[nodesSize + i] = deferredKeys[i];
497
+ valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
487
498
  }
488
- return nodes;
499
+ return [keyNodes, valueNodes, size];
489
500
  }
490
501
  async function generateSetNode(ctx, current, id) {
491
502
  assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
@@ -557,7 +568,7 @@ async function generateArrayNode(ctx, current, id) {
557
568
  }
558
569
  async function generateAggregateErrorNode(ctx, current, id) {
559
570
  const options = getErrorOptions(current);
560
- const optionsNode = options ? await generateDictionary(ctx, options) : void 0;
571
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
561
572
  const errorsNode = await generateTreeAsync(ctx, current.errors);
562
573
  return [
563
574
  12 /* AggregateError */,
@@ -567,7 +578,7 @@ async function generateAggregateErrorNode(ctx, current, id) {
567
578
  }
568
579
  async function generateErrorNode(ctx, current, id) {
569
580
  const options = getErrorOptions(current);
570
- const optionsNode = options ? await generateDictionary(ctx, options) : void 0;
581
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
571
582
  return [
572
583
  11 /* Error */,
573
584
  [getErrorConstructor(current), current.message, optionsNode],
@@ -579,7 +590,7 @@ async function generateIterableNode(ctx, current, id) {
579
590
  const options = getIterableOptions(current);
580
591
  return [13 /* Iterable */, [
581
592
  // Parse options first before the items
582
- options ? await generateDictionary(ctx, options) : void 0,
593
+ options ? await serializePropertiesAsync(ctx, options) : void 0,
583
594
  await generateNodeList(ctx, Array.from(current))
584
595
  ], id];
585
596
  }
@@ -625,7 +636,7 @@ async function generateTreeAsync(ctx, current) {
625
636
  if (current.constructor === Object || empty) {
626
637
  return [
627
638
  empty ? 9 /* NullConstructor */ : 8 /* Object */,
628
- await generateDictionary(ctx, current),
639
+ await serializePropertiesAsync(ctx, current),
629
640
  id
630
641
  ];
631
642
  }
@@ -703,7 +714,7 @@ function resolvePatches(ctx) {
703
714
  return resolveAssignments(ctx.assignments);
704
715
  }
705
716
  function assignRef(ctx, index, value) {
706
- if (ctx.markedRefs.has(index)) {
717
+ if (ctx.markedRefs[index]) {
707
718
  return `${getRefParam(ctx, index)}=${value}`;
708
719
  }
709
720
  return value;
@@ -736,55 +747,54 @@ function createArrayAssign(ctx, ref, index, value) {
736
747
  markRef(ctx, ref);
737
748
  createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
738
749
  }
739
- function createObjectAssign(ctx, ref, key, value, computed) {
750
+ function createObjectAssign(ctx, ref, key, value) {
740
751
  markRef(ctx, ref);
741
- const member = computed ? `[${key}]` : `.${key}`;
742
- createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
752
+ createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
743
753
  }
744
754
  var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
745
- function serializeAssignments(ctx, targetRef, record) {
755
+ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
746
756
  ctx.stack.push(targetRef);
747
757
  const mainAssignments = [];
748
- for (const key of Object.keys(record)) {
758
+ for (let i = 0; i < size; i++) {
749
759
  const parentStack = ctx.stack;
750
760
  ctx.stack = [];
751
- const refParam = serializeTree(ctx, record[key]);
761
+ const refParam = serializeTree(ctx, values[i]);
752
762
  ctx.stack = parentStack;
763
+ const key = keys[i];
753
764
  const check = Number(key);
754
765
  const parentAssignment = ctx.assignments;
755
766
  ctx.assignments = mainAssignments;
756
767
  const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
757
- createObjectAssign(
758
- ctx,
759
- targetRef,
760
- isIdentifier ? key : quote(key),
761
- refParam,
762
- !(isIdentifier && Number.isNaN(check))
763
- );
768
+ if (isIdentifier && Number.isNaN(check)) {
769
+ createObjectAssign(ctx, targetRef, key, refParam);
770
+ } else {
771
+ createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
772
+ }
764
773
  ctx.assignments = parentAssignment;
765
774
  }
766
775
  ctx.stack.pop();
767
776
  return resolveAssignments(mainAssignments);
768
777
  }
769
- function serializeDictionary(ctx, sourceID, record) {
778
+ function serializeObject(ctx, sourceID, [keys, values, size]) {
779
+ if (size === 0) {
780
+ return "{}";
781
+ }
770
782
  let result = "";
771
783
  ctx.stack.push(sourceID);
772
- for (const key of Object.keys(record)) {
773
- const val = record[key];
784
+ for (let i = 0; i < size; i++) {
785
+ const key = keys[i];
786
+ const val = values[i];
774
787
  const check = Number(key);
775
788
  const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
776
- const validKey = isIdentifier ? key : quote(key);
777
789
  if (isReferenceInStack(ctx, val)) {
778
790
  const refParam = getRefParam(ctx, val[1]);
779
- createObjectAssign(
780
- ctx,
781
- sourceID,
782
- validKey,
783
- refParam,
784
- !(isIdentifier && Number.isNaN(check))
785
- );
791
+ if (isIdentifier && Number.isNaN(check)) {
792
+ createObjectAssign(ctx, sourceID, key, refParam);
793
+ } else {
794
+ createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
795
+ }
786
796
  } else {
787
- result += `${validKey}:${serializeTree(ctx, val)},`;
797
+ result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
788
798
  }
789
799
  }
790
800
  ctx.stack.pop();
@@ -894,7 +904,7 @@ function serializeArray(ctx, value, id) {
894
904
  return assignRef(ctx, id, result);
895
905
  }
896
906
  function serializeWithObjectAssign(ctx, value, id, serialized) {
897
- const fields = serializeDictionary(ctx, id, value);
907
+ const fields = serializeObject(ctx, id, value);
898
908
  if (fields !== "{}") {
899
909
  return `Object.assign(${serialized},${fields})`;
900
910
  }
@@ -1012,28 +1022,38 @@ function serializeTree(ctx, [type, value, id]) {
1012
1022
  case 9 /* NullConstructor */:
1013
1023
  return serializeNullConstructor(ctx, value, id);
1014
1024
  case 8 /* Object */:
1015
- return assignRef(ctx, id, serializeDictionary(ctx, id, value));
1025
+ return assignRef(ctx, id, serializeObject(ctx, id, value));
1016
1026
  default:
1017
1027
  throw new Error("Unsupported type");
1018
1028
  }
1019
1029
  }
1020
1030
 
1021
1031
  // src/tree/sync.ts
1022
- function generateDictionary2(ctx, properties) {
1032
+ function serializePropertiesSync(ctx, properties) {
1023
1033
  const keys = Object.keys(properties);
1024
- const nodes = {};
1025
- const deferred = {};
1034
+ const size = keys.length;
1035
+ const keyNodes = new Array(size);
1036
+ const valueNodes = new Array(size);
1037
+ const deferredKeys = new Array(size);
1038
+ const deferredValues = new Array(size);
1039
+ let deferredSize = 0;
1040
+ let nodesSize = 0;
1026
1041
  for (const key of keys) {
1027
1042
  if (isIterable(properties[key])) {
1028
- deferred[key] = properties[key];
1043
+ deferredKeys[deferredSize] = key;
1044
+ deferredValues[deferredSize] = properties[key];
1045
+ deferredSize++;
1029
1046
  } else {
1030
- nodes[key] = generateTreeSync(ctx, properties[key]);
1047
+ keyNodes[nodesSize] = key;
1048
+ valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
1049
+ nodesSize++;
1031
1050
  }
1032
1051
  }
1033
- for (const key of Object.keys(deferred)) {
1034
- nodes[key] = generateTreeSync(ctx, deferred[key]);
1052
+ for (let i = 0; i < deferredSize; i++) {
1053
+ keyNodes[nodesSize + i] = deferredKeys[i];
1054
+ valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
1035
1055
  }
1036
- return nodes;
1056
+ return [keyNodes, valueNodes, size];
1037
1057
  }
1038
1058
  function generateSetNode2(ctx, current, id) {
1039
1059
  assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
@@ -1105,13 +1125,13 @@ function generateArrayNode2(ctx, current, id) {
1105
1125
  }
1106
1126
  function generateAggregateErrorNode2(ctx, current, id) {
1107
1127
  const options = getErrorOptions(current);
1108
- const optionsNode = options ? generateDictionary2(ctx, options) : void 0;
1128
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1109
1129
  const errorsNode = generateTreeSync(ctx, current.errors);
1110
1130
  return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
1111
1131
  }
1112
1132
  function generateErrorNode2(ctx, current, id) {
1113
1133
  const options = getErrorOptions(current);
1114
- const optionsNode = options ? generateDictionary2(ctx, options) : void 0;
1134
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
1115
1135
  return [
1116
1136
  11 /* Error */,
1117
1137
  [getErrorConstructor(current), current.message, optionsNode],
@@ -1123,7 +1143,7 @@ function generateIterableNode2(ctx, current, id) {
1123
1143
  const options = getIterableOptions(current);
1124
1144
  return [13 /* Iterable */, [
1125
1145
  // Parse options first before the items
1126
- options ? generateDictionary2(ctx, options) : void 0,
1146
+ options ? serializePropertiesSync(ctx, options) : void 0,
1127
1147
  generateNodeList2(ctx, Array.from(current))
1128
1148
  ], id];
1129
1149
  }
@@ -1161,7 +1181,7 @@ function generateTreeSync(ctx, current) {
1161
1181
  if (current.constructor === Object || empty) {
1162
1182
  return [
1163
1183
  empty ? 9 /* NullConstructor */ : 8 /* Object */,
1164
- generateDictionary2(ctx, current),
1184
+ serializePropertiesSync(ctx, current),
1165
1185
  id
1166
1186
  ];
1167
1187
  }
@@ -1228,7 +1248,7 @@ function toJSON(source, options) {
1228
1248
  root,
1229
1249
  isObject,
1230
1250
  ctx.features,
1231
- [...ctx.markedRefs]
1251
+ ctx.markedRefs
1232
1252
  ]);
1233
1253
  }
1234
1254
  async function toJSONAsync(source, options) {
@@ -1239,7 +1259,7 @@ async function toJSONAsync(source, options) {
1239
1259
  root,
1240
1260
  isObject,
1241
1261
  ctx.features,
1242
- [...ctx.markedRefs]
1262
+ ctx.markedRefs
1243
1263
  ]);
1244
1264
  }
1245
1265
  function compileJSON(source) {