seroval 0.4.0-alpha.1 → 0.4.0-alpha.2

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.
@@ -551,30 +551,30 @@ function generateSemiPrimitiveValue(ctx, current, id) {
551
551
  }
552
552
  return void 0;
553
553
  }
554
- function generateTreeSync(ctx, current) {
555
- function serializeProperties(properties) {
556
- const keyNodes = [];
557
- const valueNodes = [];
558
- const deferredKeys = [];
559
- const deferredValues = [];
560
- const keys = Object.keys(properties);
561
- let deferredSize = 0;
562
- for (const key of keys) {
563
- if (isIterable(properties[key])) {
564
- deferredKeys.push(key);
565
- deferredValues.push(properties[key]);
566
- deferredSize++;
567
- } else {
568
- keyNodes.push(key);
569
- valueNodes.push(generateTreeSync(ctx, properties[key]));
570
- }
571
- }
572
- for (let i = 0; i < deferredSize; i++) {
573
- keyNodes.push(deferredKeys[i]);
574
- valueNodes.push(generateTreeSync(ctx, deferredValues[i]));
554
+ function serializePropertiesSync(ctx, properties) {
555
+ const keyNodes = [];
556
+ const valueNodes = [];
557
+ const deferredKeys = [];
558
+ const deferredValues = [];
559
+ const keys = Object.keys(properties);
560
+ let deferredSize = 0;
561
+ for (const key of keys) {
562
+ if (isIterable(properties[key])) {
563
+ deferredKeys.push(key);
564
+ deferredValues.push(properties[key]);
565
+ deferredSize++;
566
+ } else {
567
+ keyNodes.push(key);
568
+ valueNodes.push(generateTreeSync(ctx, properties[key]));
575
569
  }
576
- return [keyNodes, valueNodes, keys.length];
577
570
  }
571
+ for (let i = 0; i < deferredSize; i++) {
572
+ keyNodes.push(deferredKeys[i]);
573
+ valueNodes.push(generateTreeSync(ctx, deferredValues[i]));
574
+ }
575
+ return [keyNodes, valueNodes, keys.length];
576
+ }
577
+ function generateTreeSync(ctx, current) {
578
578
  if (isPrimitive(current)) {
579
579
  return [0 /* Primitive */, current];
580
580
  }
@@ -647,13 +647,13 @@ function generateTreeSync(ctx, current) {
647
647
  }
648
648
  if (current instanceof AggregateError && ctx.features.has(0 /* AggregateError */)) {
649
649
  const options = getErrorOptions(current);
650
- const optionsNode = options ? serializeProperties(options) : void 0;
650
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
651
651
  const errorsNode = generateTreeSync(ctx, current.errors);
652
652
  return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
653
653
  }
654
654
  if (current instanceof Error) {
655
655
  const options = getErrorOptions(current);
656
- const optionsNode = options ? serializeProperties(options) : void 0;
656
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
657
657
  return [
658
658
  10 /* Error */,
659
659
  [getErrorConstructor(current), current.message, optionsNode],
@@ -665,7 +665,7 @@ function generateTreeSync(ctx, current) {
665
665
  const options = getIterableOptions(current);
666
666
  return [12 /* Iterable */, [
667
667
  // Parse options first before the items
668
- options ? serializeProperties(options) : void 0,
668
+ options ? serializePropertiesSync(ctx, options) : void 0,
669
669
  generateTreeSync(ctx, Array.from(current))
670
670
  ], id];
671
671
  }
@@ -673,36 +673,36 @@ function generateTreeSync(ctx, current) {
673
673
  if (current.constructor === Object || empty) {
674
674
  return [
675
675
  empty ? 8 /* NullConstructor */ : 7 /* Object */,
676
- serializeProperties(current),
676
+ serializePropertiesSync(ctx, current),
677
677
  id
678
678
  ];
679
679
  }
680
680
  throw new Error("Unsupported value");
681
681
  }
682
- async function generateTreeAsync(ctx, current) {
683
- async function serializeProperties(properties) {
684
- const keyNodes = [];
685
- const valueNodes = [];
686
- const deferredKeys = [];
687
- const deferredValues = [];
688
- const keys = Object.keys(properties);
689
- let deferredSize = 0;
690
- for (const key of keys) {
691
- if (isIterable(properties[key])) {
692
- deferredKeys.push(key);
693
- deferredValues.push(properties[key]);
694
- deferredSize++;
695
- } else {
696
- keyNodes.push(key);
697
- valueNodes.push(await generateTreeAsync(ctx, properties[key]));
698
- }
699
- }
700
- for (let i = 0; i < deferredSize; i++) {
701
- keyNodes.push(deferredKeys[i]);
702
- valueNodes.push(await generateTreeAsync(ctx, deferredValues[i]));
682
+ async function serializePropertiesAsync(ctx, properties) {
683
+ const keyNodes = [];
684
+ const valueNodes = [];
685
+ const deferredKeys = [];
686
+ const deferredValues = [];
687
+ const keys = Object.keys(properties);
688
+ let deferredSize = 0;
689
+ for (const key of keys) {
690
+ if (isIterable(properties[key])) {
691
+ deferredKeys.push(key);
692
+ deferredValues.push(properties[key]);
693
+ deferredSize++;
694
+ } else {
695
+ keyNodes.push(key);
696
+ valueNodes.push(await generateTreeAsync(ctx, properties[key]));
703
697
  }
704
- return [keyNodes, valueNodes, keys.length];
705
698
  }
699
+ for (let i = 0; i < deferredSize; i++) {
700
+ keyNodes.push(deferredKeys[i]);
701
+ valueNodes.push(await generateTreeAsync(ctx, deferredValues[i]));
702
+ }
703
+ return [keyNodes, valueNodes, keys.length];
704
+ }
705
+ async function generateTreeAsync(ctx, current) {
706
706
  if (isPrimitive(current)) {
707
707
  return [0 /* Primitive */, current];
708
708
  }
@@ -784,13 +784,13 @@ async function generateTreeAsync(ctx, current) {
784
784
  }
785
785
  if (current instanceof AggregateError) {
786
786
  const options = getErrorOptions(current);
787
- const optionsNode = options ? await serializeProperties(options) : void 0;
787
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
788
788
  const errorsNode = await generateTreeAsync(ctx, current.errors);
789
789
  return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
790
790
  }
791
791
  if (current instanceof Error) {
792
792
  const options = getErrorOptions(current);
793
- const optionsNode = options ? await serializeProperties(options) : void 0;
793
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
794
794
  return [
795
795
  10 /* Error */,
796
796
  [getErrorConstructor(current), current.message, optionsNode],
@@ -801,7 +801,7 @@ async function generateTreeAsync(ctx, current) {
801
801
  assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
802
802
  const options = getIterableOptions(current);
803
803
  return [12 /* Iterable */, [
804
- options ? await serializeProperties(options) : void 0,
804
+ options ? await serializePropertiesAsync(ctx, options) : void 0,
805
805
  await generateTreeAsync(ctx, Array.from(current))
806
806
  ], id];
807
807
  }
@@ -809,7 +809,7 @@ async function generateTreeAsync(ctx, current) {
809
809
  if (current.constructor === Object || empty) {
810
810
  return [
811
811
  empty ? 8 /* NullConstructor */ : 7 /* Object */,
812
- await serializeProperties(current),
812
+ await serializePropertiesAsync(ctx, current),
813
813
  id
814
814
  ];
815
815
  }
@@ -846,61 +846,62 @@ function serializePrimitive(ctx, value) {
846
846
  }
847
847
  return String(value);
848
848
  }
849
- function serializeTree(ctx, [type, value, id]) {
850
- function serializeAssignments(targetRef, [keys, values, size]) {
851
- ctx.stack.push(targetRef);
852
- const mainAssignments = [];
853
- for (let i = 0; i < size; i++) {
854
- const parentStack = ctx.stack;
855
- ctx.stack = [];
856
- const refParam = serializeTree(ctx, values[i]);
857
- ctx.stack = parentStack;
858
- const key = keys[i];
859
- const check = Number(key);
860
- const parentAssignment = ctx.assignments;
861
- ctx.assignments = mainAssignments;
862
- if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
863
- if (!Number.isNaN(check)) {
864
- createObjectComputedAssign(ctx, targetRef, key, refParam);
865
- } else {
866
- createObjectIdentifierAssign(ctx, targetRef, key, refParam);
867
- }
849
+ var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
850
+ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
851
+ ctx.stack.push(targetRef);
852
+ const mainAssignments = [];
853
+ for (let i = 0; i < size; i++) {
854
+ const parentStack = ctx.stack;
855
+ ctx.stack = [];
856
+ const refParam = serializeTree(ctx, values[i]);
857
+ ctx.stack = parentStack;
858
+ const key = keys[i];
859
+ const check = Number(key);
860
+ const parentAssignment = ctx.assignments;
861
+ ctx.assignments = mainAssignments;
862
+ if (check >= 0 || Number.isNaN(check) && IDENTIFIER_CHECK.test(key)) {
863
+ if (!Number.isNaN(check)) {
864
+ createObjectComputedAssign(ctx, targetRef, key, refParam);
868
865
  } else {
869
- createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
866
+ createObjectIdentifierAssign(ctx, targetRef, key, refParam);
870
867
  }
871
- ctx.assignments = parentAssignment;
868
+ } else {
869
+ createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
872
870
  }
873
- ctx.stack.pop();
874
- return resolveAssignments(mainAssignments);
871
+ ctx.assignments = parentAssignment;
875
872
  }
876
- function serializeObject(sourceID, [keys, values, size]) {
877
- let result = "";
878
- ctx.stack.push(sourceID);
879
- for (let i = 0; i < size; i++) {
880
- const key = keys[i];
881
- const val = values[i];
882
- const check = Number(key);
883
- if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
884
- if (isReferenceInStack(ctx, val)) {
885
- const refParam = getRefParam(ctx, val[1]);
886
- if (!Number.isNaN(check)) {
887
- createObjectComputedAssign(ctx, sourceID, key, refParam);
888
- } else {
889
- createObjectIdentifierAssign(ctx, sourceID, key, refParam);
890
- }
873
+ ctx.stack.pop();
874
+ return resolveAssignments(mainAssignments);
875
+ }
876
+ function serializeObject(ctx, sourceID, [keys, values, size]) {
877
+ let result = "";
878
+ ctx.stack.push(sourceID);
879
+ for (let i = 0; i < size; i++) {
880
+ const key = keys[i];
881
+ const val = values[i];
882
+ const check = Number(key);
883
+ if (check >= 0 || Number.isNaN(check) && IDENTIFIER_CHECK.test(key)) {
884
+ if (isReferenceInStack(ctx, val)) {
885
+ const refParam = getRefParam(ctx, val[1]);
886
+ if (!Number.isNaN(check)) {
887
+ createObjectComputedAssign(ctx, sourceID, key, refParam);
891
888
  } else {
892
- result += `${key}:${serializeTree(ctx, val)},`;
889
+ createObjectIdentifierAssign(ctx, sourceID, key, refParam);
893
890
  }
894
- } else if (isReferenceInStack(ctx, val)) {
895
- const refParam = getRefParam(ctx, val[1]);
896
- createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
897
891
  } else {
898
- result += `${quote(key)}:${serializeTree(ctx, val)},`;
892
+ result += `${key}:${serializeTree(ctx, val)},`;
899
893
  }
894
+ } else if (isReferenceInStack(ctx, val)) {
895
+ const refParam = getRefParam(ctx, val[1]);
896
+ createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
897
+ } else {
898
+ result += `${quote(key)}:${serializeTree(ctx, val)},`;
900
899
  }
901
- ctx.stack.pop();
902
- return `{${result.substring(0, result.length - 1)}}`;
903
900
  }
901
+ ctx.stack.pop();
902
+ return `{${result.substring(0, result.length - 1)}}`;
903
+ }
904
+ function serializeTree(ctx, [type, value, id]) {
904
905
  switch (type) {
905
906
  case 0 /* Primitive */:
906
907
  return serializePrimitive(ctx, value);
@@ -1020,11 +1021,11 @@ function serializeTree(ctx, [type, value, id]) {
1020
1021
  ctx.stack.pop();
1021
1022
  if (value[1]) {
1022
1023
  if (ctx.features.has(6 /* ObjectAssign */)) {
1023
- const options = serializeObject(id, value[1]);
1024
+ const options = serializeObject(ctx, id, value[1]);
1024
1025
  serialized = `Object.assign(${serialized},${options})`;
1025
1026
  } else {
1026
1027
  markRef(ctx, id);
1027
- const assignments = serializeAssignments(id, value[1]);
1028
+ const assignments = serializeAssignments(ctx, id, value[1]);
1028
1029
  if (assignments) {
1029
1030
  const ref = getRefParam(ctx, id);
1030
1031
  return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
@@ -1037,11 +1038,11 @@ function serializeTree(ctx, [type, value, id]) {
1037
1038
  let serialized = `new ${value[0]}(${quote(value[1])})`;
1038
1039
  if (value[2]) {
1039
1040
  if (ctx.features.has(6 /* ObjectAssign */)) {
1040
- const options = serializeObject(id, value[2]);
1041
+ const options = serializeObject(ctx, id, value[2]);
1041
1042
  serialized = `Object.assign(${serialized},${options})`;
1042
1043
  } else {
1043
1044
  markRef(ctx, id);
1044
- const assignments = serializeAssignments(id, value[2]);
1045
+ const assignments = serializeAssignments(ctx, id, value[2]);
1045
1046
  if (assignments) {
1046
1047
  const ref = getRefParam(ctx, id);
1047
1048
  return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
@@ -1070,11 +1071,11 @@ function serializeTree(ctx, [type, value, id]) {
1070
1071
  }
1071
1072
  if (value[0]) {
1072
1073
  if (ctx.features.has(6 /* ObjectAssign */)) {
1073
- const options = serializeObject(id, value[0]);
1074
+ const options = serializeObject(ctx, id, value[0]);
1074
1075
  serialized = `Object.assign(${serialized},${options})`;
1075
1076
  } else {
1076
1077
  markRef(ctx, id);
1077
- const assignments = serializeAssignments(id, value[0]);
1078
+ const assignments = serializeAssignments(ctx, id, value[0]);
1078
1079
  if (assignments) {
1079
1080
  const ref = getRefParam(ctx, id);
1080
1081
  return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
@@ -1086,13 +1087,13 @@ function serializeTree(ctx, [type, value, id]) {
1086
1087
  case 8 /* NullConstructor */: {
1087
1088
  let serialized = "Object.create(null)";
1088
1089
  if (ctx.features.has(6 /* ObjectAssign */)) {
1089
- const fields = serializeObject(id, value);
1090
+ const fields = serializeObject(ctx, id, value);
1090
1091
  if (fields !== "{}") {
1091
1092
  serialized = `Object.assign(${serialized},${fields})`;
1092
1093
  }
1093
1094
  } else {
1094
1095
  markRef(ctx, id);
1095
- const assignments = serializeAssignments(id, value);
1096
+ const assignments = serializeAssignments(ctx, id, value);
1096
1097
  if (assignments) {
1097
1098
  const ref = getRefParam(ctx, id);
1098
1099
  return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
@@ -1101,7 +1102,7 @@ function serializeTree(ctx, [type, value, id]) {
1101
1102
  return assignRef(ctx, id, serialized);
1102
1103
  }
1103
1104
  case 7 /* Object */:
1104
- return assignRef(ctx, id, serializeObject(id, value));
1105
+ return assignRef(ctx, id, serializeObject(ctx, id, value));
1105
1106
  default:
1106
1107
  throw new Error("Unsupported type");
1107
1108
  }