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.
@@ -580,30 +580,30 @@ function generateSemiPrimitiveValue(ctx, current, id) {
580
580
  }
581
581
  return void 0;
582
582
  }
583
- function generateTreeSync(ctx, current) {
584
- function serializeProperties(properties) {
585
- const keyNodes = [];
586
- const valueNodes = [];
587
- const deferredKeys = [];
588
- const deferredValues = [];
589
- const keys = Object.keys(properties);
590
- let deferredSize = 0;
591
- for (const key of keys) {
592
- if (isIterable(properties[key])) {
593
- deferredKeys.push(key);
594
- deferredValues.push(properties[key]);
595
- deferredSize++;
596
- } else {
597
- keyNodes.push(key);
598
- valueNodes.push(generateTreeSync(ctx, properties[key]));
599
- }
600
- }
601
- for (let i = 0; i < deferredSize; i++) {
602
- keyNodes.push(deferredKeys[i]);
603
- valueNodes.push(generateTreeSync(ctx, deferredValues[i]));
583
+ function serializePropertiesSync(ctx, properties) {
584
+ const keyNodes = [];
585
+ const valueNodes = [];
586
+ const deferredKeys = [];
587
+ const deferredValues = [];
588
+ const keys = Object.keys(properties);
589
+ let deferredSize = 0;
590
+ for (const key of keys) {
591
+ if (isIterable(properties[key])) {
592
+ deferredKeys.push(key);
593
+ deferredValues.push(properties[key]);
594
+ deferredSize++;
595
+ } else {
596
+ keyNodes.push(key);
597
+ valueNodes.push(generateTreeSync(ctx, properties[key]));
604
598
  }
605
- return [keyNodes, valueNodes, keys.length];
606
599
  }
600
+ for (let i = 0; i < deferredSize; i++) {
601
+ keyNodes.push(deferredKeys[i]);
602
+ valueNodes.push(generateTreeSync(ctx, deferredValues[i]));
603
+ }
604
+ return [keyNodes, valueNodes, keys.length];
605
+ }
606
+ function generateTreeSync(ctx, current) {
607
607
  if (isPrimitive(current)) {
608
608
  return [0 /* Primitive */, current];
609
609
  }
@@ -676,13 +676,13 @@ function generateTreeSync(ctx, current) {
676
676
  }
677
677
  if (current instanceof AggregateError && ctx.features.has(0 /* AggregateError */)) {
678
678
  const options = getErrorOptions(current);
679
- const optionsNode = options ? serializeProperties(options) : void 0;
679
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
680
680
  const errorsNode = generateTreeSync(ctx, current.errors);
681
681
  return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
682
682
  }
683
683
  if (current instanceof Error) {
684
684
  const options = getErrorOptions(current);
685
- const optionsNode = options ? serializeProperties(options) : void 0;
685
+ const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
686
686
  return [
687
687
  10 /* Error */,
688
688
  [getErrorConstructor(current), current.message, optionsNode],
@@ -694,7 +694,7 @@ function generateTreeSync(ctx, current) {
694
694
  const options = getIterableOptions(current);
695
695
  return [12 /* Iterable */, [
696
696
  // Parse options first before the items
697
- options ? serializeProperties(options) : void 0,
697
+ options ? serializePropertiesSync(ctx, options) : void 0,
698
698
  generateTreeSync(ctx, Array.from(current))
699
699
  ], id];
700
700
  }
@@ -702,36 +702,36 @@ function generateTreeSync(ctx, current) {
702
702
  if (current.constructor === Object || empty) {
703
703
  return [
704
704
  empty ? 8 /* NullConstructor */ : 7 /* Object */,
705
- serializeProperties(current),
705
+ serializePropertiesSync(ctx, current),
706
706
  id
707
707
  ];
708
708
  }
709
709
  throw new Error("Unsupported value");
710
710
  }
711
- async function generateTreeAsync(ctx, current) {
712
- async function serializeProperties(properties) {
713
- const keyNodes = [];
714
- const valueNodes = [];
715
- const deferredKeys = [];
716
- const deferredValues = [];
717
- const keys = Object.keys(properties);
718
- let deferredSize = 0;
719
- for (const key of keys) {
720
- if (isIterable(properties[key])) {
721
- deferredKeys.push(key);
722
- deferredValues.push(properties[key]);
723
- deferredSize++;
724
- } else {
725
- keyNodes.push(key);
726
- valueNodes.push(await generateTreeAsync(ctx, properties[key]));
727
- }
728
- }
729
- for (let i = 0; i < deferredSize; i++) {
730
- keyNodes.push(deferredKeys[i]);
731
- valueNodes.push(await generateTreeAsync(ctx, deferredValues[i]));
711
+ async function serializePropertiesAsync(ctx, properties) {
712
+ const keyNodes = [];
713
+ const valueNodes = [];
714
+ const deferredKeys = [];
715
+ const deferredValues = [];
716
+ const keys = Object.keys(properties);
717
+ let deferredSize = 0;
718
+ for (const key of keys) {
719
+ if (isIterable(properties[key])) {
720
+ deferredKeys.push(key);
721
+ deferredValues.push(properties[key]);
722
+ deferredSize++;
723
+ } else {
724
+ keyNodes.push(key);
725
+ valueNodes.push(await generateTreeAsync(ctx, properties[key]));
732
726
  }
733
- return [keyNodes, valueNodes, keys.length];
734
727
  }
728
+ for (let i = 0; i < deferredSize; i++) {
729
+ keyNodes.push(deferredKeys[i]);
730
+ valueNodes.push(await generateTreeAsync(ctx, deferredValues[i]));
731
+ }
732
+ return [keyNodes, valueNodes, keys.length];
733
+ }
734
+ async function generateTreeAsync(ctx, current) {
735
735
  if (isPrimitive(current)) {
736
736
  return [0 /* Primitive */, current];
737
737
  }
@@ -813,13 +813,13 @@ async function generateTreeAsync(ctx, current) {
813
813
  }
814
814
  if (current instanceof AggregateError) {
815
815
  const options = getErrorOptions(current);
816
- const optionsNode = options ? await serializeProperties(options) : void 0;
816
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
817
817
  const errorsNode = await generateTreeAsync(ctx, current.errors);
818
818
  return [11 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
819
819
  }
820
820
  if (current instanceof Error) {
821
821
  const options = getErrorOptions(current);
822
- const optionsNode = options ? await serializeProperties(options) : void 0;
822
+ const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
823
823
  return [
824
824
  10 /* Error */,
825
825
  [getErrorConstructor(current), current.message, optionsNode],
@@ -830,7 +830,7 @@ async function generateTreeAsync(ctx, current) {
830
830
  assert(ctx.features.has(9 /* SymbolIterator */), 'Unsupported type "Iterable"');
831
831
  const options = getIterableOptions(current);
832
832
  return [12 /* Iterable */, [
833
- options ? await serializeProperties(options) : void 0,
833
+ options ? await serializePropertiesAsync(ctx, options) : void 0,
834
834
  await generateTreeAsync(ctx, Array.from(current))
835
835
  ], id];
836
836
  }
@@ -838,7 +838,7 @@ async function generateTreeAsync(ctx, current) {
838
838
  if (current.constructor === Object || empty) {
839
839
  return [
840
840
  empty ? 8 /* NullConstructor */ : 7 /* Object */,
841
- await serializeProperties(current),
841
+ await serializePropertiesAsync(ctx, current),
842
842
  id
843
843
  ];
844
844
  }
@@ -875,61 +875,62 @@ function serializePrimitive(ctx, value) {
875
875
  }
876
876
  return String(value);
877
877
  }
878
- function serializeTree(ctx, [type, value, id]) {
879
- function serializeAssignments(targetRef, [keys, values, size]) {
880
- ctx.stack.push(targetRef);
881
- const mainAssignments = [];
882
- for (let i = 0; i < size; i++) {
883
- const parentStack = ctx.stack;
884
- ctx.stack = [];
885
- const refParam = serializeTree(ctx, values[i]);
886
- ctx.stack = parentStack;
887
- const key = keys[i];
888
- const check = Number(key);
889
- const parentAssignment = ctx.assignments;
890
- ctx.assignments = mainAssignments;
891
- if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
892
- if (!Number.isNaN(check)) {
893
- createObjectComputedAssign(ctx, targetRef, key, refParam);
894
- } else {
895
- createObjectIdentifierAssign(ctx, targetRef, key, refParam);
896
- }
878
+ var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
879
+ function serializeAssignments(ctx, targetRef, [keys, values, size]) {
880
+ ctx.stack.push(targetRef);
881
+ const mainAssignments = [];
882
+ for (let i = 0; i < size; i++) {
883
+ const parentStack = ctx.stack;
884
+ ctx.stack = [];
885
+ const refParam = serializeTree(ctx, values[i]);
886
+ ctx.stack = parentStack;
887
+ const key = keys[i];
888
+ const check = Number(key);
889
+ const parentAssignment = ctx.assignments;
890
+ ctx.assignments = mainAssignments;
891
+ if (check >= 0 || Number.isNaN(check) && IDENTIFIER_CHECK.test(key)) {
892
+ if (!Number.isNaN(check)) {
893
+ createObjectComputedAssign(ctx, targetRef, key, refParam);
897
894
  } else {
898
- createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
895
+ createObjectIdentifierAssign(ctx, targetRef, key, refParam);
899
896
  }
900
- ctx.assignments = parentAssignment;
897
+ } else {
898
+ createObjectComputedAssign(ctx, targetRef, quote(key), refParam);
901
899
  }
902
- ctx.stack.pop();
903
- return resolveAssignments(mainAssignments);
900
+ ctx.assignments = parentAssignment;
904
901
  }
905
- function serializeObject(sourceID, [keys, values, size]) {
906
- let result = "";
907
- ctx.stack.push(sourceID);
908
- for (let i = 0; i < size; i++) {
909
- const key = keys[i];
910
- const val = values[i];
911
- const check = Number(key);
912
- if (check >= 0 || Number.isNaN(check) && /^([$A-Z_][0-9A-Z_$]*)$/i.test(key)) {
913
- if (isReferenceInStack(ctx, val)) {
914
- const refParam = getRefParam(ctx, val[1]);
915
- if (!Number.isNaN(check)) {
916
- createObjectComputedAssign(ctx, sourceID, key, refParam);
917
- } else {
918
- createObjectIdentifierAssign(ctx, sourceID, key, refParam);
919
- }
902
+ ctx.stack.pop();
903
+ return resolveAssignments(mainAssignments);
904
+ }
905
+ function serializeObject(ctx, sourceID, [keys, values, size]) {
906
+ let result = "";
907
+ ctx.stack.push(sourceID);
908
+ for (let i = 0; i < size; i++) {
909
+ const key = keys[i];
910
+ const val = values[i];
911
+ const check = Number(key);
912
+ if (check >= 0 || Number.isNaN(check) && IDENTIFIER_CHECK.test(key)) {
913
+ if (isReferenceInStack(ctx, val)) {
914
+ const refParam = getRefParam(ctx, val[1]);
915
+ if (!Number.isNaN(check)) {
916
+ createObjectComputedAssign(ctx, sourceID, key, refParam);
920
917
  } else {
921
- result += `${key}:${serializeTree(ctx, val)},`;
918
+ createObjectIdentifierAssign(ctx, sourceID, key, refParam);
922
919
  }
923
- } else if (isReferenceInStack(ctx, val)) {
924
- const refParam = getRefParam(ctx, val[1]);
925
- createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
926
920
  } else {
927
- result += `${quote(key)}:${serializeTree(ctx, val)},`;
921
+ result += `${key}:${serializeTree(ctx, val)},`;
928
922
  }
923
+ } else if (isReferenceInStack(ctx, val)) {
924
+ const refParam = getRefParam(ctx, val[1]);
925
+ createObjectComputedAssign(ctx, sourceID, quote(key), refParam);
926
+ } else {
927
+ result += `${quote(key)}:${serializeTree(ctx, val)},`;
929
928
  }
930
- ctx.stack.pop();
931
- return `{${result.substring(0, result.length - 1)}}`;
932
929
  }
930
+ ctx.stack.pop();
931
+ return `{${result.substring(0, result.length - 1)}}`;
932
+ }
933
+ function serializeTree(ctx, [type, value, id]) {
933
934
  switch (type) {
934
935
  case 0 /* Primitive */:
935
936
  return serializePrimitive(ctx, value);
@@ -1049,11 +1050,11 @@ function serializeTree(ctx, [type, value, id]) {
1049
1050
  ctx.stack.pop();
1050
1051
  if (value[1]) {
1051
1052
  if (ctx.features.has(6 /* ObjectAssign */)) {
1052
- const options = serializeObject(id, value[1]);
1053
+ const options = serializeObject(ctx, id, value[1]);
1053
1054
  serialized = `Object.assign(${serialized},${options})`;
1054
1055
  } else {
1055
1056
  markRef(ctx, id);
1056
- const assignments = serializeAssignments(id, value[1]);
1057
+ const assignments = serializeAssignments(ctx, id, value[1]);
1057
1058
  if (assignments) {
1058
1059
  const ref = getRefParam(ctx, id);
1059
1060
  return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
@@ -1066,11 +1067,11 @@ function serializeTree(ctx, [type, value, id]) {
1066
1067
  let serialized = `new ${value[0]}(${quote(value[1])})`;
1067
1068
  if (value[2]) {
1068
1069
  if (ctx.features.has(6 /* ObjectAssign */)) {
1069
- const options = serializeObject(id, value[2]);
1070
+ const options = serializeObject(ctx, id, value[2]);
1070
1071
  serialized = `Object.assign(${serialized},${options})`;
1071
1072
  } else {
1072
1073
  markRef(ctx, id);
1073
- const assignments = serializeAssignments(id, value[2]);
1074
+ const assignments = serializeAssignments(ctx, id, value[2]);
1074
1075
  if (assignments) {
1075
1076
  const ref = getRefParam(ctx, id);
1076
1077
  return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
@@ -1099,11 +1100,11 @@ function serializeTree(ctx, [type, value, id]) {
1099
1100
  }
1100
1101
  if (value[0]) {
1101
1102
  if (ctx.features.has(6 /* ObjectAssign */)) {
1102
- const options = serializeObject(id, value[0]);
1103
+ const options = serializeObject(ctx, id, value[0]);
1103
1104
  serialized = `Object.assign(${serialized},${options})`;
1104
1105
  } else {
1105
1106
  markRef(ctx, id);
1106
- const assignments = serializeAssignments(id, value[0]);
1107
+ const assignments = serializeAssignments(ctx, id, value[0]);
1107
1108
  if (assignments) {
1108
1109
  const ref = getRefParam(ctx, id);
1109
1110
  return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
@@ -1115,13 +1116,13 @@ function serializeTree(ctx, [type, value, id]) {
1115
1116
  case 8 /* NullConstructor */: {
1116
1117
  let serialized = "Object.create(null)";
1117
1118
  if (ctx.features.has(6 /* ObjectAssign */)) {
1118
- const fields = serializeObject(id, value);
1119
+ const fields = serializeObject(ctx, id, value);
1119
1120
  if (fields !== "{}") {
1120
1121
  serialized = `Object.assign(${serialized},${fields})`;
1121
1122
  }
1122
1123
  } else {
1123
1124
  markRef(ctx, id);
1124
- const assignments = serializeAssignments(id, value);
1125
+ const assignments = serializeAssignments(ctx, id, value);
1125
1126
  if (assignments) {
1126
1127
  const ref = getRefParam(ctx, id);
1127
1128
  return `(${assignRef(ctx, id, serialized)},${assignments}${ref})`;
@@ -1130,7 +1131,7 @@ function serializeTree(ctx, [type, value, id]) {
1130
1131
  return assignRef(ctx, id, serialized);
1131
1132
  }
1132
1133
  case 7 /* Object */:
1133
- return assignRef(ctx, id, serializeObject(id, value));
1134
+ return assignRef(ctx, id, serializeObject(ctx, id, value));
1134
1135
  default:
1135
1136
  throw new Error("Unsupported type");
1136
1137
  }