seroval 0.4.0-alpha.6 → 0.4.0-alpha.7
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/dist/cjs/development/index.cjs +70 -51
- package/dist/cjs/development/index.cjs.map +3 -3
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +70 -51
- package/dist/esm/development/index.mjs.map +3 -3
- package/dist/esm/production/index.mjs +2 -2
- package/dist/types/tree/types.d.ts +1 -1
- package/package.json +2 -2
- package/src/tree/async.ts +23 -15
- package/src/tree/serialize.ts +27 -29
- package/src/tree/sync.ts +23 -15
- package/src/tree/types.ts +1 -1
|
@@ -504,21 +504,31 @@ function serializePrimitive(value) {
|
|
|
504
504
|
}
|
|
505
505
|
|
|
506
506
|
// src/tree/async.ts
|
|
507
|
-
async function
|
|
507
|
+
async function serializePropertiesAsync(ctx, properties) {
|
|
508
508
|
const keys = Object.keys(properties);
|
|
509
|
-
const
|
|
510
|
-
const
|
|
509
|
+
const size = keys.length;
|
|
510
|
+
const keyNodes = new Array(size);
|
|
511
|
+
const valueNodes = new Array(size);
|
|
512
|
+
const deferredKeys = new Array(size);
|
|
513
|
+
const deferredValues = new Array(size);
|
|
514
|
+
let deferredSize = 0;
|
|
515
|
+
let nodesSize = 0;
|
|
511
516
|
for (const key of keys) {
|
|
512
517
|
if (isIterable(properties[key])) {
|
|
513
|
-
|
|
518
|
+
deferredKeys[deferredSize] = key;
|
|
519
|
+
deferredValues[deferredSize] = properties[key];
|
|
520
|
+
deferredSize++;
|
|
514
521
|
} else {
|
|
515
|
-
|
|
522
|
+
keyNodes[nodesSize] = key;
|
|
523
|
+
valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
|
|
524
|
+
nodesSize++;
|
|
516
525
|
}
|
|
517
526
|
}
|
|
518
|
-
for (
|
|
519
|
-
|
|
527
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
528
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
529
|
+
valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
|
|
520
530
|
}
|
|
521
|
-
return
|
|
531
|
+
return [keyNodes, valueNodes, size];
|
|
522
532
|
}
|
|
523
533
|
async function generateSetNode(ctx, current, id) {
|
|
524
534
|
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
@@ -590,7 +600,7 @@ async function generateArrayNode(ctx, current, id) {
|
|
|
590
600
|
}
|
|
591
601
|
async function generateAggregateErrorNode(ctx, current, id) {
|
|
592
602
|
const options = getErrorOptions(current);
|
|
593
|
-
const optionsNode = options ? await
|
|
603
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
594
604
|
const errorsNode = await generateTreeAsync(ctx, current.errors);
|
|
595
605
|
return [
|
|
596
606
|
12 /* AggregateError */,
|
|
@@ -600,7 +610,7 @@ async function generateAggregateErrorNode(ctx, current, id) {
|
|
|
600
610
|
}
|
|
601
611
|
async function generateErrorNode(ctx, current, id) {
|
|
602
612
|
const options = getErrorOptions(current);
|
|
603
|
-
const optionsNode = options ? await
|
|
613
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
604
614
|
return [
|
|
605
615
|
11 /* Error */,
|
|
606
616
|
[getErrorConstructor(current), current.message, optionsNode],
|
|
@@ -612,7 +622,7 @@ async function generateIterableNode(ctx, current, id) {
|
|
|
612
622
|
const options = getIterableOptions(current);
|
|
613
623
|
return [13 /* Iterable */, [
|
|
614
624
|
// Parse options first before the items
|
|
615
|
-
options ? await
|
|
625
|
+
options ? await serializePropertiesAsync(ctx, options) : void 0,
|
|
616
626
|
await generateNodeList(ctx, Array.from(current))
|
|
617
627
|
], id];
|
|
618
628
|
}
|
|
@@ -658,7 +668,7 @@ async function generateTreeAsync(ctx, current) {
|
|
|
658
668
|
if (current.constructor === Object || empty) {
|
|
659
669
|
return [
|
|
660
670
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
661
|
-
await
|
|
671
|
+
await serializePropertiesAsync(ctx, current),
|
|
662
672
|
id
|
|
663
673
|
];
|
|
664
674
|
}
|
|
@@ -769,55 +779,54 @@ function createArrayAssign(ctx, ref, index, value) {
|
|
|
769
779
|
markRef(ctx, ref);
|
|
770
780
|
createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
|
|
771
781
|
}
|
|
772
|
-
function createObjectAssign(ctx, ref, key, value
|
|
782
|
+
function createObjectAssign(ctx, ref, key, value) {
|
|
773
783
|
markRef(ctx, ref);
|
|
774
|
-
|
|
775
|
-
createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
|
|
784
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
|
|
776
785
|
}
|
|
777
786
|
var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
|
|
778
|
-
function serializeAssignments(ctx, targetRef,
|
|
787
|
+
function serializeAssignments(ctx, targetRef, [keys, values, size]) {
|
|
779
788
|
ctx.stack.push(targetRef);
|
|
780
789
|
const mainAssignments = [];
|
|
781
|
-
for (
|
|
790
|
+
for (let i = 0; i < size; i++) {
|
|
782
791
|
const parentStack = ctx.stack;
|
|
783
792
|
ctx.stack = [];
|
|
784
|
-
const refParam = serializeTree(ctx,
|
|
793
|
+
const refParam = serializeTree(ctx, values[i]);
|
|
785
794
|
ctx.stack = parentStack;
|
|
795
|
+
const key = keys[i];
|
|
786
796
|
const check = Number(key);
|
|
787
797
|
const parentAssignment = ctx.assignments;
|
|
788
798
|
ctx.assignments = mainAssignments;
|
|
789
799
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
790
|
-
|
|
791
|
-
ctx,
|
|
792
|
-
|
|
793
|
-
isIdentifier ? key : quote(key),
|
|
794
|
-
|
|
795
|
-
!(isIdentifier && Number.isNaN(check))
|
|
796
|
-
);
|
|
800
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
801
|
+
createObjectAssign(ctx, targetRef, key, refParam);
|
|
802
|
+
} else {
|
|
803
|
+
createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
|
|
804
|
+
}
|
|
797
805
|
ctx.assignments = parentAssignment;
|
|
798
806
|
}
|
|
799
807
|
ctx.stack.pop();
|
|
800
808
|
return resolveAssignments(mainAssignments);
|
|
801
809
|
}
|
|
802
|
-
function
|
|
810
|
+
function serializeObject(ctx, sourceID, [keys, values, size]) {
|
|
811
|
+
if (size === 0) {
|
|
812
|
+
return "{}";
|
|
813
|
+
}
|
|
803
814
|
let result = "";
|
|
804
815
|
ctx.stack.push(sourceID);
|
|
805
|
-
for (
|
|
806
|
-
const
|
|
816
|
+
for (let i = 0; i < size; i++) {
|
|
817
|
+
const key = keys[i];
|
|
818
|
+
const val = values[i];
|
|
807
819
|
const check = Number(key);
|
|
808
820
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
809
|
-
const validKey = isIdentifier ? key : quote(key);
|
|
810
821
|
if (isReferenceInStack(ctx, val)) {
|
|
811
822
|
const refParam = getRefParam(ctx, val[1]);
|
|
812
|
-
|
|
813
|
-
ctx,
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
!(isIdentifier && Number.isNaN(check))
|
|
818
|
-
);
|
|
823
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
824
|
+
createObjectAssign(ctx, sourceID, key, refParam);
|
|
825
|
+
} else {
|
|
826
|
+
createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
|
|
827
|
+
}
|
|
819
828
|
} else {
|
|
820
|
-
result += `${
|
|
829
|
+
result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
|
|
821
830
|
}
|
|
822
831
|
}
|
|
823
832
|
ctx.stack.pop();
|
|
@@ -927,7 +936,7 @@ function serializeArray(ctx, value, id) {
|
|
|
927
936
|
return assignRef(ctx, id, result);
|
|
928
937
|
}
|
|
929
938
|
function serializeWithObjectAssign(ctx, value, id, serialized) {
|
|
930
|
-
const fields =
|
|
939
|
+
const fields = serializeObject(ctx, id, value);
|
|
931
940
|
if (fields !== "{}") {
|
|
932
941
|
return `Object.assign(${serialized},${fields})`;
|
|
933
942
|
}
|
|
@@ -1045,28 +1054,38 @@ function serializeTree(ctx, [type, value, id]) {
|
|
|
1045
1054
|
case 9 /* NullConstructor */:
|
|
1046
1055
|
return serializeNullConstructor(ctx, value, id);
|
|
1047
1056
|
case 8 /* Object */:
|
|
1048
|
-
return assignRef(ctx, id,
|
|
1057
|
+
return assignRef(ctx, id, serializeObject(ctx, id, value));
|
|
1049
1058
|
default:
|
|
1050
1059
|
throw new Error("Unsupported type");
|
|
1051
1060
|
}
|
|
1052
1061
|
}
|
|
1053
1062
|
|
|
1054
1063
|
// src/tree/sync.ts
|
|
1055
|
-
function
|
|
1064
|
+
function serializePropertiesSync(ctx, properties) {
|
|
1056
1065
|
const keys = Object.keys(properties);
|
|
1057
|
-
const
|
|
1058
|
-
const
|
|
1066
|
+
const size = keys.length;
|
|
1067
|
+
const keyNodes = new Array(size);
|
|
1068
|
+
const valueNodes = new Array(size);
|
|
1069
|
+
const deferredKeys = new Array(size);
|
|
1070
|
+
const deferredValues = new Array(size);
|
|
1071
|
+
let deferredSize = 0;
|
|
1072
|
+
let nodesSize = 0;
|
|
1059
1073
|
for (const key of keys) {
|
|
1060
1074
|
if (isIterable(properties[key])) {
|
|
1061
|
-
|
|
1075
|
+
deferredKeys[deferredSize] = key;
|
|
1076
|
+
deferredValues[deferredSize] = properties[key];
|
|
1077
|
+
deferredSize++;
|
|
1062
1078
|
} else {
|
|
1063
|
-
|
|
1079
|
+
keyNodes[nodesSize] = key;
|
|
1080
|
+
valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
|
|
1081
|
+
nodesSize++;
|
|
1064
1082
|
}
|
|
1065
1083
|
}
|
|
1066
|
-
for (
|
|
1067
|
-
|
|
1084
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1085
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
1086
|
+
valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
|
|
1068
1087
|
}
|
|
1069
|
-
return
|
|
1088
|
+
return [keyNodes, valueNodes, size];
|
|
1070
1089
|
}
|
|
1071
1090
|
function generateSetNode2(ctx, current, id) {
|
|
1072
1091
|
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
@@ -1138,13 +1157,13 @@ function generateArrayNode2(ctx, current, id) {
|
|
|
1138
1157
|
}
|
|
1139
1158
|
function generateAggregateErrorNode2(ctx, current, id) {
|
|
1140
1159
|
const options = getErrorOptions(current);
|
|
1141
|
-
const optionsNode = options ?
|
|
1160
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1142
1161
|
const errorsNode = generateTreeSync(ctx, current.errors);
|
|
1143
1162
|
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
1144
1163
|
}
|
|
1145
1164
|
function generateErrorNode2(ctx, current, id) {
|
|
1146
1165
|
const options = getErrorOptions(current);
|
|
1147
|
-
const optionsNode = options ?
|
|
1166
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1148
1167
|
return [
|
|
1149
1168
|
11 /* Error */,
|
|
1150
1169
|
[getErrorConstructor(current), current.message, optionsNode],
|
|
@@ -1156,7 +1175,7 @@ function generateIterableNode2(ctx, current, id) {
|
|
|
1156
1175
|
const options = getIterableOptions(current);
|
|
1157
1176
|
return [13 /* Iterable */, [
|
|
1158
1177
|
// Parse options first before the items
|
|
1159
|
-
options ?
|
|
1178
|
+
options ? serializePropertiesSync(ctx, options) : void 0,
|
|
1160
1179
|
generateNodeList2(ctx, Array.from(current))
|
|
1161
1180
|
], id];
|
|
1162
1181
|
}
|
|
@@ -1194,7 +1213,7 @@ function generateTreeSync(ctx, current) {
|
|
|
1194
1213
|
if (current.constructor === Object || empty) {
|
|
1195
1214
|
return [
|
|
1196
1215
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
1197
|
-
|
|
1216
|
+
serializePropertiesSync(ctx, current),
|
|
1198
1217
|
id
|
|
1199
1218
|
];
|
|
1200
1219
|
}
|