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.
- package/dist/cjs/development/index.cjs +84 -64
- package/dist/cjs/development/index.cjs.map +3 -3
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +84 -64
- package/dist/esm/development/index.mjs.map +3 -3
- package/dist/esm/production/index.mjs +2 -2
- package/dist/types/context.d.ts +5 -4
- package/dist/types/tree/types.d.ts +1 -1
- package/package.json +2 -2
- package/src/context.ts +16 -14
- package/src/index.ts +3 -3
- package/src/tree/async.ts +23 -15
- package/src/tree/serialize.ts +28 -30
- package/src/tree/sync.ts +23 -15
- package/src/tree/types.ts +1 -1
|
@@ -287,7 +287,7 @@ var DEFAULT_OPTIONS = {
|
|
|
287
287
|
function createParserContext(options = {}) {
|
|
288
288
|
const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
|
|
289
289
|
return {
|
|
290
|
-
markedRefs:
|
|
290
|
+
markedRefs: {},
|
|
291
291
|
refs: /* @__PURE__ */ new Map(),
|
|
292
292
|
features: parseTargets(result.target)
|
|
293
293
|
};
|
|
@@ -297,22 +297,23 @@ function createSerializationContext(options) {
|
|
|
297
297
|
stack: [],
|
|
298
298
|
vars: [],
|
|
299
299
|
assignments: [],
|
|
300
|
-
validRefs:
|
|
300
|
+
validRefs: [],
|
|
301
|
+
refSize: 0,
|
|
301
302
|
features: options.features,
|
|
302
|
-
markedRefs:
|
|
303
|
+
markedRefs: options.markedRefs
|
|
303
304
|
};
|
|
304
305
|
}
|
|
305
306
|
function markRef(ctx, current) {
|
|
306
|
-
ctx.markedRefs
|
|
307
|
+
ctx.markedRefs[current] = 1;
|
|
307
308
|
}
|
|
308
309
|
function createValidRef(ctx, index) {
|
|
309
|
-
const current = ctx.validRefs
|
|
310
|
-
if (current
|
|
311
|
-
|
|
310
|
+
const current = ctx.validRefs[index];
|
|
311
|
+
if (current == null) {
|
|
312
|
+
const value = ctx.refSize++;
|
|
313
|
+
ctx.validRefs[index] = value;
|
|
314
|
+
return value;
|
|
312
315
|
}
|
|
313
|
-
|
|
314
|
-
ctx.validRefs.set(index, id);
|
|
315
|
-
return id;
|
|
316
|
+
return current;
|
|
316
317
|
}
|
|
317
318
|
function getRefParam(ctx, index) {
|
|
318
319
|
const actualIndex = createValidRef(ctx, index);
|
|
@@ -504,21 +505,31 @@ function serializePrimitive(value) {
|
|
|
504
505
|
}
|
|
505
506
|
|
|
506
507
|
// src/tree/async.ts
|
|
507
|
-
async function
|
|
508
|
+
async function serializePropertiesAsync(ctx, properties) {
|
|
508
509
|
const keys = Object.keys(properties);
|
|
509
|
-
const
|
|
510
|
-
const
|
|
510
|
+
const size = keys.length;
|
|
511
|
+
const keyNodes = new Array(size);
|
|
512
|
+
const valueNodes = new Array(size);
|
|
513
|
+
const deferredKeys = new Array(size);
|
|
514
|
+
const deferredValues = new Array(size);
|
|
515
|
+
let deferredSize = 0;
|
|
516
|
+
let nodesSize = 0;
|
|
511
517
|
for (const key of keys) {
|
|
512
518
|
if (isIterable(properties[key])) {
|
|
513
|
-
|
|
519
|
+
deferredKeys[deferredSize] = key;
|
|
520
|
+
deferredValues[deferredSize] = properties[key];
|
|
521
|
+
deferredSize++;
|
|
514
522
|
} else {
|
|
515
|
-
|
|
523
|
+
keyNodes[nodesSize] = key;
|
|
524
|
+
valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
|
|
525
|
+
nodesSize++;
|
|
516
526
|
}
|
|
517
527
|
}
|
|
518
|
-
for (
|
|
519
|
-
|
|
528
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
529
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
530
|
+
valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
|
|
520
531
|
}
|
|
521
|
-
return
|
|
532
|
+
return [keyNodes, valueNodes, size];
|
|
522
533
|
}
|
|
523
534
|
async function generateSetNode(ctx, current, id) {
|
|
524
535
|
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
@@ -590,7 +601,7 @@ async function generateArrayNode(ctx, current, id) {
|
|
|
590
601
|
}
|
|
591
602
|
async function generateAggregateErrorNode(ctx, current, id) {
|
|
592
603
|
const options = getErrorOptions(current);
|
|
593
|
-
const optionsNode = options ? await
|
|
604
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
594
605
|
const errorsNode = await generateTreeAsync(ctx, current.errors);
|
|
595
606
|
return [
|
|
596
607
|
12 /* AggregateError */,
|
|
@@ -600,7 +611,7 @@ async function generateAggregateErrorNode(ctx, current, id) {
|
|
|
600
611
|
}
|
|
601
612
|
async function generateErrorNode(ctx, current, id) {
|
|
602
613
|
const options = getErrorOptions(current);
|
|
603
|
-
const optionsNode = options ? await
|
|
614
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
604
615
|
return [
|
|
605
616
|
11 /* Error */,
|
|
606
617
|
[getErrorConstructor(current), current.message, optionsNode],
|
|
@@ -612,7 +623,7 @@ async function generateIterableNode(ctx, current, id) {
|
|
|
612
623
|
const options = getIterableOptions(current);
|
|
613
624
|
return [13 /* Iterable */, [
|
|
614
625
|
// Parse options first before the items
|
|
615
|
-
options ? await
|
|
626
|
+
options ? await serializePropertiesAsync(ctx, options) : void 0,
|
|
616
627
|
await generateNodeList(ctx, Array.from(current))
|
|
617
628
|
], id];
|
|
618
629
|
}
|
|
@@ -658,7 +669,7 @@ async function generateTreeAsync(ctx, current) {
|
|
|
658
669
|
if (current.constructor === Object || empty) {
|
|
659
670
|
return [
|
|
660
671
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
661
|
-
await
|
|
672
|
+
await serializePropertiesAsync(ctx, current),
|
|
662
673
|
id
|
|
663
674
|
];
|
|
664
675
|
}
|
|
@@ -736,7 +747,7 @@ function resolvePatches(ctx) {
|
|
|
736
747
|
return resolveAssignments(ctx.assignments);
|
|
737
748
|
}
|
|
738
749
|
function assignRef(ctx, index, value) {
|
|
739
|
-
if (ctx.markedRefs
|
|
750
|
+
if (ctx.markedRefs[index]) {
|
|
740
751
|
return `${getRefParam(ctx, index)}=${value}`;
|
|
741
752
|
}
|
|
742
753
|
return value;
|
|
@@ -769,55 +780,54 @@ function createArrayAssign(ctx, ref, index, value) {
|
|
|
769
780
|
markRef(ctx, ref);
|
|
770
781
|
createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
|
|
771
782
|
}
|
|
772
|
-
function createObjectAssign(ctx, ref, key, value
|
|
783
|
+
function createObjectAssign(ctx, ref, key, value) {
|
|
773
784
|
markRef(ctx, ref);
|
|
774
|
-
|
|
775
|
-
createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
|
|
785
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
|
|
776
786
|
}
|
|
777
787
|
var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
|
|
778
|
-
function serializeAssignments(ctx, targetRef,
|
|
788
|
+
function serializeAssignments(ctx, targetRef, [keys, values, size]) {
|
|
779
789
|
ctx.stack.push(targetRef);
|
|
780
790
|
const mainAssignments = [];
|
|
781
|
-
for (
|
|
791
|
+
for (let i = 0; i < size; i++) {
|
|
782
792
|
const parentStack = ctx.stack;
|
|
783
793
|
ctx.stack = [];
|
|
784
|
-
const refParam = serializeTree(ctx,
|
|
794
|
+
const refParam = serializeTree(ctx, values[i]);
|
|
785
795
|
ctx.stack = parentStack;
|
|
796
|
+
const key = keys[i];
|
|
786
797
|
const check = Number(key);
|
|
787
798
|
const parentAssignment = ctx.assignments;
|
|
788
799
|
ctx.assignments = mainAssignments;
|
|
789
800
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
790
|
-
|
|
791
|
-
ctx,
|
|
792
|
-
|
|
793
|
-
isIdentifier ? key : quote(key),
|
|
794
|
-
|
|
795
|
-
!(isIdentifier && Number.isNaN(check))
|
|
796
|
-
);
|
|
801
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
802
|
+
createObjectAssign(ctx, targetRef, key, refParam);
|
|
803
|
+
} else {
|
|
804
|
+
createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
|
|
805
|
+
}
|
|
797
806
|
ctx.assignments = parentAssignment;
|
|
798
807
|
}
|
|
799
808
|
ctx.stack.pop();
|
|
800
809
|
return resolveAssignments(mainAssignments);
|
|
801
810
|
}
|
|
802
|
-
function
|
|
811
|
+
function serializeObject(ctx, sourceID, [keys, values, size]) {
|
|
812
|
+
if (size === 0) {
|
|
813
|
+
return "{}";
|
|
814
|
+
}
|
|
803
815
|
let result = "";
|
|
804
816
|
ctx.stack.push(sourceID);
|
|
805
|
-
for (
|
|
806
|
-
const
|
|
817
|
+
for (let i = 0; i < size; i++) {
|
|
818
|
+
const key = keys[i];
|
|
819
|
+
const val = values[i];
|
|
807
820
|
const check = Number(key);
|
|
808
821
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
809
|
-
const validKey = isIdentifier ? key : quote(key);
|
|
810
822
|
if (isReferenceInStack(ctx, val)) {
|
|
811
823
|
const refParam = getRefParam(ctx, val[1]);
|
|
812
|
-
|
|
813
|
-
ctx,
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
!(isIdentifier && Number.isNaN(check))
|
|
818
|
-
);
|
|
824
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
825
|
+
createObjectAssign(ctx, sourceID, key, refParam);
|
|
826
|
+
} else {
|
|
827
|
+
createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
|
|
828
|
+
}
|
|
819
829
|
} else {
|
|
820
|
-
result += `${
|
|
830
|
+
result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
|
|
821
831
|
}
|
|
822
832
|
}
|
|
823
833
|
ctx.stack.pop();
|
|
@@ -927,7 +937,7 @@ function serializeArray(ctx, value, id) {
|
|
|
927
937
|
return assignRef(ctx, id, result);
|
|
928
938
|
}
|
|
929
939
|
function serializeWithObjectAssign(ctx, value, id, serialized) {
|
|
930
|
-
const fields =
|
|
940
|
+
const fields = serializeObject(ctx, id, value);
|
|
931
941
|
if (fields !== "{}") {
|
|
932
942
|
return `Object.assign(${serialized},${fields})`;
|
|
933
943
|
}
|
|
@@ -1045,28 +1055,38 @@ function serializeTree(ctx, [type, value, id]) {
|
|
|
1045
1055
|
case 9 /* NullConstructor */:
|
|
1046
1056
|
return serializeNullConstructor(ctx, value, id);
|
|
1047
1057
|
case 8 /* Object */:
|
|
1048
|
-
return assignRef(ctx, id,
|
|
1058
|
+
return assignRef(ctx, id, serializeObject(ctx, id, value));
|
|
1049
1059
|
default:
|
|
1050
1060
|
throw new Error("Unsupported type");
|
|
1051
1061
|
}
|
|
1052
1062
|
}
|
|
1053
1063
|
|
|
1054
1064
|
// src/tree/sync.ts
|
|
1055
|
-
function
|
|
1065
|
+
function serializePropertiesSync(ctx, properties) {
|
|
1056
1066
|
const keys = Object.keys(properties);
|
|
1057
|
-
const
|
|
1058
|
-
const
|
|
1067
|
+
const size = keys.length;
|
|
1068
|
+
const keyNodes = new Array(size);
|
|
1069
|
+
const valueNodes = new Array(size);
|
|
1070
|
+
const deferredKeys = new Array(size);
|
|
1071
|
+
const deferredValues = new Array(size);
|
|
1072
|
+
let deferredSize = 0;
|
|
1073
|
+
let nodesSize = 0;
|
|
1059
1074
|
for (const key of keys) {
|
|
1060
1075
|
if (isIterable(properties[key])) {
|
|
1061
|
-
|
|
1076
|
+
deferredKeys[deferredSize] = key;
|
|
1077
|
+
deferredValues[deferredSize] = properties[key];
|
|
1078
|
+
deferredSize++;
|
|
1062
1079
|
} else {
|
|
1063
|
-
|
|
1080
|
+
keyNodes[nodesSize] = key;
|
|
1081
|
+
valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
|
|
1082
|
+
nodesSize++;
|
|
1064
1083
|
}
|
|
1065
1084
|
}
|
|
1066
|
-
for (
|
|
1067
|
-
|
|
1085
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1086
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
1087
|
+
valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
|
|
1068
1088
|
}
|
|
1069
|
-
return
|
|
1089
|
+
return [keyNodes, valueNodes, size];
|
|
1070
1090
|
}
|
|
1071
1091
|
function generateSetNode2(ctx, current, id) {
|
|
1072
1092
|
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
@@ -1138,13 +1158,13 @@ function generateArrayNode2(ctx, current, id) {
|
|
|
1138
1158
|
}
|
|
1139
1159
|
function generateAggregateErrorNode2(ctx, current, id) {
|
|
1140
1160
|
const options = getErrorOptions(current);
|
|
1141
|
-
const optionsNode = options ?
|
|
1161
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1142
1162
|
const errorsNode = generateTreeSync(ctx, current.errors);
|
|
1143
1163
|
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
1144
1164
|
}
|
|
1145
1165
|
function generateErrorNode2(ctx, current, id) {
|
|
1146
1166
|
const options = getErrorOptions(current);
|
|
1147
|
-
const optionsNode = options ?
|
|
1167
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1148
1168
|
return [
|
|
1149
1169
|
11 /* Error */,
|
|
1150
1170
|
[getErrorConstructor(current), current.message, optionsNode],
|
|
@@ -1156,7 +1176,7 @@ function generateIterableNode2(ctx, current, id) {
|
|
|
1156
1176
|
const options = getIterableOptions(current);
|
|
1157
1177
|
return [13 /* Iterable */, [
|
|
1158
1178
|
// Parse options first before the items
|
|
1159
|
-
options ?
|
|
1179
|
+
options ? serializePropertiesSync(ctx, options) : void 0,
|
|
1160
1180
|
generateNodeList2(ctx, Array.from(current))
|
|
1161
1181
|
], id];
|
|
1162
1182
|
}
|
|
@@ -1194,7 +1214,7 @@ function generateTreeSync(ctx, current) {
|
|
|
1194
1214
|
if (current.constructor === Object || empty) {
|
|
1195
1215
|
return [
|
|
1196
1216
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
1197
|
-
|
|
1217
|
+
serializePropertiesSync(ctx, current),
|
|
1198
1218
|
id
|
|
1199
1219
|
];
|
|
1200
1220
|
}
|
|
@@ -1261,7 +1281,7 @@ function toJSON(source, options) {
|
|
|
1261
1281
|
root,
|
|
1262
1282
|
isObject,
|
|
1263
1283
|
ctx.features,
|
|
1264
|
-
|
|
1284
|
+
ctx.markedRefs
|
|
1265
1285
|
]);
|
|
1266
1286
|
}
|
|
1267
1287
|
async function toJSONAsync(source, options) {
|
|
@@ -1272,7 +1292,7 @@ async function toJSONAsync(source, options) {
|
|
|
1272
1292
|
root,
|
|
1273
1293
|
isObject,
|
|
1274
1294
|
ctx.features,
|
|
1275
|
-
|
|
1295
|
+
ctx.markedRefs
|
|
1276
1296
|
]);
|
|
1277
1297
|
}
|
|
1278
1298
|
function compileJSON(source) {
|