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
|
@@ -471,21 +471,31 @@ function serializePrimitive(value) {
|
|
|
471
471
|
}
|
|
472
472
|
|
|
473
473
|
// src/tree/async.ts
|
|
474
|
-
async function
|
|
474
|
+
async function serializePropertiesAsync(ctx, properties) {
|
|
475
475
|
const keys = Object.keys(properties);
|
|
476
|
-
const
|
|
477
|
-
const
|
|
476
|
+
const size = keys.length;
|
|
477
|
+
const keyNodes = new Array(size);
|
|
478
|
+
const valueNodes = new Array(size);
|
|
479
|
+
const deferredKeys = new Array(size);
|
|
480
|
+
const deferredValues = new Array(size);
|
|
481
|
+
let deferredSize = 0;
|
|
482
|
+
let nodesSize = 0;
|
|
478
483
|
for (const key of keys) {
|
|
479
484
|
if (isIterable(properties[key])) {
|
|
480
|
-
|
|
485
|
+
deferredKeys[deferredSize] = key;
|
|
486
|
+
deferredValues[deferredSize] = properties[key];
|
|
487
|
+
deferredSize++;
|
|
481
488
|
} else {
|
|
482
|
-
|
|
489
|
+
keyNodes[nodesSize] = key;
|
|
490
|
+
valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
|
|
491
|
+
nodesSize++;
|
|
483
492
|
}
|
|
484
493
|
}
|
|
485
|
-
for (
|
|
486
|
-
|
|
494
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
495
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
496
|
+
valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
|
|
487
497
|
}
|
|
488
|
-
return
|
|
498
|
+
return [keyNodes, valueNodes, size];
|
|
489
499
|
}
|
|
490
500
|
async function generateSetNode(ctx, current, id) {
|
|
491
501
|
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
@@ -557,7 +567,7 @@ async function generateArrayNode(ctx, current, id) {
|
|
|
557
567
|
}
|
|
558
568
|
async function generateAggregateErrorNode(ctx, current, id) {
|
|
559
569
|
const options = getErrorOptions(current);
|
|
560
|
-
const optionsNode = options ? await
|
|
570
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
561
571
|
const errorsNode = await generateTreeAsync(ctx, current.errors);
|
|
562
572
|
return [
|
|
563
573
|
12 /* AggregateError */,
|
|
@@ -567,7 +577,7 @@ async function generateAggregateErrorNode(ctx, current, id) {
|
|
|
567
577
|
}
|
|
568
578
|
async function generateErrorNode(ctx, current, id) {
|
|
569
579
|
const options = getErrorOptions(current);
|
|
570
|
-
const optionsNode = options ? await
|
|
580
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
571
581
|
return [
|
|
572
582
|
11 /* Error */,
|
|
573
583
|
[getErrorConstructor(current), current.message, optionsNode],
|
|
@@ -579,7 +589,7 @@ async function generateIterableNode(ctx, current, id) {
|
|
|
579
589
|
const options = getIterableOptions(current);
|
|
580
590
|
return [13 /* Iterable */, [
|
|
581
591
|
// Parse options first before the items
|
|
582
|
-
options ? await
|
|
592
|
+
options ? await serializePropertiesAsync(ctx, options) : void 0,
|
|
583
593
|
await generateNodeList(ctx, Array.from(current))
|
|
584
594
|
], id];
|
|
585
595
|
}
|
|
@@ -625,7 +635,7 @@ async function generateTreeAsync(ctx, current) {
|
|
|
625
635
|
if (current.constructor === Object || empty) {
|
|
626
636
|
return [
|
|
627
637
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
628
|
-
await
|
|
638
|
+
await serializePropertiesAsync(ctx, current),
|
|
629
639
|
id
|
|
630
640
|
];
|
|
631
641
|
}
|
|
@@ -736,55 +746,54 @@ function createArrayAssign(ctx, ref, index, value) {
|
|
|
736
746
|
markRef(ctx, ref);
|
|
737
747
|
createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
|
|
738
748
|
}
|
|
739
|
-
function createObjectAssign(ctx, ref, key, value
|
|
749
|
+
function createObjectAssign(ctx, ref, key, value) {
|
|
740
750
|
markRef(ctx, ref);
|
|
741
|
-
|
|
742
|
-
createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
|
|
751
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
|
|
743
752
|
}
|
|
744
753
|
var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
|
|
745
|
-
function serializeAssignments(ctx, targetRef,
|
|
754
|
+
function serializeAssignments(ctx, targetRef, [keys, values, size]) {
|
|
746
755
|
ctx.stack.push(targetRef);
|
|
747
756
|
const mainAssignments = [];
|
|
748
|
-
for (
|
|
757
|
+
for (let i = 0; i < size; i++) {
|
|
749
758
|
const parentStack = ctx.stack;
|
|
750
759
|
ctx.stack = [];
|
|
751
|
-
const refParam = serializeTree(ctx,
|
|
760
|
+
const refParam = serializeTree(ctx, values[i]);
|
|
752
761
|
ctx.stack = parentStack;
|
|
762
|
+
const key = keys[i];
|
|
753
763
|
const check = Number(key);
|
|
754
764
|
const parentAssignment = ctx.assignments;
|
|
755
765
|
ctx.assignments = mainAssignments;
|
|
756
766
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
757
|
-
|
|
758
|
-
ctx,
|
|
759
|
-
|
|
760
|
-
isIdentifier ? key : quote(key),
|
|
761
|
-
|
|
762
|
-
!(isIdentifier && Number.isNaN(check))
|
|
763
|
-
);
|
|
767
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
768
|
+
createObjectAssign(ctx, targetRef, key, refParam);
|
|
769
|
+
} else {
|
|
770
|
+
createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
|
|
771
|
+
}
|
|
764
772
|
ctx.assignments = parentAssignment;
|
|
765
773
|
}
|
|
766
774
|
ctx.stack.pop();
|
|
767
775
|
return resolveAssignments(mainAssignments);
|
|
768
776
|
}
|
|
769
|
-
function
|
|
777
|
+
function serializeObject(ctx, sourceID, [keys, values, size]) {
|
|
778
|
+
if (size === 0) {
|
|
779
|
+
return "{}";
|
|
780
|
+
}
|
|
770
781
|
let result = "";
|
|
771
782
|
ctx.stack.push(sourceID);
|
|
772
|
-
for (
|
|
773
|
-
const
|
|
783
|
+
for (let i = 0; i < size; i++) {
|
|
784
|
+
const key = keys[i];
|
|
785
|
+
const val = values[i];
|
|
774
786
|
const check = Number(key);
|
|
775
787
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
776
|
-
const validKey = isIdentifier ? key : quote(key);
|
|
777
788
|
if (isReferenceInStack(ctx, val)) {
|
|
778
789
|
const refParam = getRefParam(ctx, val[1]);
|
|
779
|
-
|
|
780
|
-
ctx,
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
!(isIdentifier && Number.isNaN(check))
|
|
785
|
-
);
|
|
790
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
791
|
+
createObjectAssign(ctx, sourceID, key, refParam);
|
|
792
|
+
} else {
|
|
793
|
+
createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
|
|
794
|
+
}
|
|
786
795
|
} else {
|
|
787
|
-
result += `${
|
|
796
|
+
result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
|
|
788
797
|
}
|
|
789
798
|
}
|
|
790
799
|
ctx.stack.pop();
|
|
@@ -894,7 +903,7 @@ function serializeArray(ctx, value, id) {
|
|
|
894
903
|
return assignRef(ctx, id, result);
|
|
895
904
|
}
|
|
896
905
|
function serializeWithObjectAssign(ctx, value, id, serialized) {
|
|
897
|
-
const fields =
|
|
906
|
+
const fields = serializeObject(ctx, id, value);
|
|
898
907
|
if (fields !== "{}") {
|
|
899
908
|
return `Object.assign(${serialized},${fields})`;
|
|
900
909
|
}
|
|
@@ -1012,28 +1021,38 @@ function serializeTree(ctx, [type, value, id]) {
|
|
|
1012
1021
|
case 9 /* NullConstructor */:
|
|
1013
1022
|
return serializeNullConstructor(ctx, value, id);
|
|
1014
1023
|
case 8 /* Object */:
|
|
1015
|
-
return assignRef(ctx, id,
|
|
1024
|
+
return assignRef(ctx, id, serializeObject(ctx, id, value));
|
|
1016
1025
|
default:
|
|
1017
1026
|
throw new Error("Unsupported type");
|
|
1018
1027
|
}
|
|
1019
1028
|
}
|
|
1020
1029
|
|
|
1021
1030
|
// src/tree/sync.ts
|
|
1022
|
-
function
|
|
1031
|
+
function serializePropertiesSync(ctx, properties) {
|
|
1023
1032
|
const keys = Object.keys(properties);
|
|
1024
|
-
const
|
|
1025
|
-
const
|
|
1033
|
+
const size = keys.length;
|
|
1034
|
+
const keyNodes = new Array(size);
|
|
1035
|
+
const valueNodes = new Array(size);
|
|
1036
|
+
const deferredKeys = new Array(size);
|
|
1037
|
+
const deferredValues = new Array(size);
|
|
1038
|
+
let deferredSize = 0;
|
|
1039
|
+
let nodesSize = 0;
|
|
1026
1040
|
for (const key of keys) {
|
|
1027
1041
|
if (isIterable(properties[key])) {
|
|
1028
|
-
|
|
1042
|
+
deferredKeys[deferredSize] = key;
|
|
1043
|
+
deferredValues[deferredSize] = properties[key];
|
|
1044
|
+
deferredSize++;
|
|
1029
1045
|
} else {
|
|
1030
|
-
|
|
1046
|
+
keyNodes[nodesSize] = key;
|
|
1047
|
+
valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
|
|
1048
|
+
nodesSize++;
|
|
1031
1049
|
}
|
|
1032
1050
|
}
|
|
1033
|
-
for (
|
|
1034
|
-
|
|
1051
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1052
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
1053
|
+
valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
|
|
1035
1054
|
}
|
|
1036
|
-
return
|
|
1055
|
+
return [keyNodes, valueNodes, size];
|
|
1037
1056
|
}
|
|
1038
1057
|
function generateSetNode2(ctx, current, id) {
|
|
1039
1058
|
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
@@ -1105,13 +1124,13 @@ function generateArrayNode2(ctx, current, id) {
|
|
|
1105
1124
|
}
|
|
1106
1125
|
function generateAggregateErrorNode2(ctx, current, id) {
|
|
1107
1126
|
const options = getErrorOptions(current);
|
|
1108
|
-
const optionsNode = options ?
|
|
1127
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1109
1128
|
const errorsNode = generateTreeSync(ctx, current.errors);
|
|
1110
1129
|
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
1111
1130
|
}
|
|
1112
1131
|
function generateErrorNode2(ctx, current, id) {
|
|
1113
1132
|
const options = getErrorOptions(current);
|
|
1114
|
-
const optionsNode = options ?
|
|
1133
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1115
1134
|
return [
|
|
1116
1135
|
11 /* Error */,
|
|
1117
1136
|
[getErrorConstructor(current), current.message, optionsNode],
|
|
@@ -1123,7 +1142,7 @@ function generateIterableNode2(ctx, current, id) {
|
|
|
1123
1142
|
const options = getIterableOptions(current);
|
|
1124
1143
|
return [13 /* Iterable */, [
|
|
1125
1144
|
// Parse options first before the items
|
|
1126
|
-
options ?
|
|
1145
|
+
options ? serializePropertiesSync(ctx, options) : void 0,
|
|
1127
1146
|
generateNodeList2(ctx, Array.from(current))
|
|
1128
1147
|
], id];
|
|
1129
1148
|
}
|
|
@@ -1161,7 +1180,7 @@ function generateTreeSync(ctx, current) {
|
|
|
1161
1180
|
if (current.constructor === Object || empty) {
|
|
1162
1181
|
return [
|
|
1163
1182
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
1164
|
-
|
|
1183
|
+
serializePropertiesSync(ctx, current),
|
|
1165
1184
|
id
|
|
1166
1185
|
];
|
|
1167
1186
|
}
|