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
|
@@ -254,7 +254,7 @@ var DEFAULT_OPTIONS = {
|
|
|
254
254
|
function createParserContext(options = {}) {
|
|
255
255
|
const result = Object.assign({}, DEFAULT_OPTIONS, options || {});
|
|
256
256
|
return {
|
|
257
|
-
markedRefs:
|
|
257
|
+
markedRefs: {},
|
|
258
258
|
refs: /* @__PURE__ */ new Map(),
|
|
259
259
|
features: parseTargets(result.target)
|
|
260
260
|
};
|
|
@@ -264,22 +264,23 @@ function createSerializationContext(options) {
|
|
|
264
264
|
stack: [],
|
|
265
265
|
vars: [],
|
|
266
266
|
assignments: [],
|
|
267
|
-
validRefs:
|
|
267
|
+
validRefs: [],
|
|
268
|
+
refSize: 0,
|
|
268
269
|
features: options.features,
|
|
269
|
-
markedRefs:
|
|
270
|
+
markedRefs: options.markedRefs
|
|
270
271
|
};
|
|
271
272
|
}
|
|
272
273
|
function markRef(ctx, current) {
|
|
273
|
-
ctx.markedRefs
|
|
274
|
+
ctx.markedRefs[current] = 1;
|
|
274
275
|
}
|
|
275
276
|
function createValidRef(ctx, index) {
|
|
276
|
-
const current = ctx.validRefs
|
|
277
|
-
if (current
|
|
278
|
-
|
|
277
|
+
const current = ctx.validRefs[index];
|
|
278
|
+
if (current == null) {
|
|
279
|
+
const value = ctx.refSize++;
|
|
280
|
+
ctx.validRefs[index] = value;
|
|
281
|
+
return value;
|
|
279
282
|
}
|
|
280
|
-
|
|
281
|
-
ctx.validRefs.set(index, id);
|
|
282
|
-
return id;
|
|
283
|
+
return current;
|
|
283
284
|
}
|
|
284
285
|
function getRefParam(ctx, index) {
|
|
285
286
|
const actualIndex = createValidRef(ctx, index);
|
|
@@ -471,21 +472,31 @@ function serializePrimitive(value) {
|
|
|
471
472
|
}
|
|
472
473
|
|
|
473
474
|
// src/tree/async.ts
|
|
474
|
-
async function
|
|
475
|
+
async function serializePropertiesAsync(ctx, properties) {
|
|
475
476
|
const keys = Object.keys(properties);
|
|
476
|
-
const
|
|
477
|
-
const
|
|
477
|
+
const size = keys.length;
|
|
478
|
+
const keyNodes = new Array(size);
|
|
479
|
+
const valueNodes = new Array(size);
|
|
480
|
+
const deferredKeys = new Array(size);
|
|
481
|
+
const deferredValues = new Array(size);
|
|
482
|
+
let deferredSize = 0;
|
|
483
|
+
let nodesSize = 0;
|
|
478
484
|
for (const key of keys) {
|
|
479
485
|
if (isIterable(properties[key])) {
|
|
480
|
-
|
|
486
|
+
deferredKeys[deferredSize] = key;
|
|
487
|
+
deferredValues[deferredSize] = properties[key];
|
|
488
|
+
deferredSize++;
|
|
481
489
|
} else {
|
|
482
|
-
|
|
490
|
+
keyNodes[nodesSize] = key;
|
|
491
|
+
valueNodes[nodesSize] = await generateTreeAsync(ctx, properties[key]);
|
|
492
|
+
nodesSize++;
|
|
483
493
|
}
|
|
484
494
|
}
|
|
485
|
-
for (
|
|
486
|
-
|
|
495
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
496
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
497
|
+
valueNodes[nodesSize + i] = await generateTreeAsync(ctx, deferredValues[i]);
|
|
487
498
|
}
|
|
488
|
-
return
|
|
499
|
+
return [keyNodes, valueNodes, size];
|
|
489
500
|
}
|
|
490
501
|
async function generateSetNode(ctx, current, id) {
|
|
491
502
|
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
@@ -557,7 +568,7 @@ async function generateArrayNode(ctx, current, id) {
|
|
|
557
568
|
}
|
|
558
569
|
async function generateAggregateErrorNode(ctx, current, id) {
|
|
559
570
|
const options = getErrorOptions(current);
|
|
560
|
-
const optionsNode = options ? await
|
|
571
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
561
572
|
const errorsNode = await generateTreeAsync(ctx, current.errors);
|
|
562
573
|
return [
|
|
563
574
|
12 /* AggregateError */,
|
|
@@ -567,7 +578,7 @@ async function generateAggregateErrorNode(ctx, current, id) {
|
|
|
567
578
|
}
|
|
568
579
|
async function generateErrorNode(ctx, current, id) {
|
|
569
580
|
const options = getErrorOptions(current);
|
|
570
|
-
const optionsNode = options ? await
|
|
581
|
+
const optionsNode = options ? await serializePropertiesAsync(ctx, options) : void 0;
|
|
571
582
|
return [
|
|
572
583
|
11 /* Error */,
|
|
573
584
|
[getErrorConstructor(current), current.message, optionsNode],
|
|
@@ -579,7 +590,7 @@ async function generateIterableNode(ctx, current, id) {
|
|
|
579
590
|
const options = getIterableOptions(current);
|
|
580
591
|
return [13 /* Iterable */, [
|
|
581
592
|
// Parse options first before the items
|
|
582
|
-
options ? await
|
|
593
|
+
options ? await serializePropertiesAsync(ctx, options) : void 0,
|
|
583
594
|
await generateNodeList(ctx, Array.from(current))
|
|
584
595
|
], id];
|
|
585
596
|
}
|
|
@@ -625,7 +636,7 @@ async function generateTreeAsync(ctx, current) {
|
|
|
625
636
|
if (current.constructor === Object || empty) {
|
|
626
637
|
return [
|
|
627
638
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
628
|
-
await
|
|
639
|
+
await serializePropertiesAsync(ctx, current),
|
|
629
640
|
id
|
|
630
641
|
];
|
|
631
642
|
}
|
|
@@ -703,7 +714,7 @@ function resolvePatches(ctx) {
|
|
|
703
714
|
return resolveAssignments(ctx.assignments);
|
|
704
715
|
}
|
|
705
716
|
function assignRef(ctx, index, value) {
|
|
706
|
-
if (ctx.markedRefs
|
|
717
|
+
if (ctx.markedRefs[index]) {
|
|
707
718
|
return `${getRefParam(ctx, index)}=${value}`;
|
|
708
719
|
}
|
|
709
720
|
return value;
|
|
@@ -736,55 +747,54 @@ function createArrayAssign(ctx, ref, index, value) {
|
|
|
736
747
|
markRef(ctx, ref);
|
|
737
748
|
createAssignment(ctx, `${getRefParam(ctx, ref)}[${index}]`, value);
|
|
738
749
|
}
|
|
739
|
-
function createObjectAssign(ctx, ref, key, value
|
|
750
|
+
function createObjectAssign(ctx, ref, key, value) {
|
|
740
751
|
markRef(ctx, ref);
|
|
741
|
-
|
|
742
|
-
createAssignment(ctx, `${getRefParam(ctx, ref)}${member}`, value);
|
|
752
|
+
createAssignment(ctx, `${getRefParam(ctx, ref)}.${key}`, value);
|
|
743
753
|
}
|
|
744
754
|
var IDENTIFIER_CHECK = /^([$A-Z_][0-9A-Z_$]*)$/i;
|
|
745
|
-
function serializeAssignments(ctx, targetRef,
|
|
755
|
+
function serializeAssignments(ctx, targetRef, [keys, values, size]) {
|
|
746
756
|
ctx.stack.push(targetRef);
|
|
747
757
|
const mainAssignments = [];
|
|
748
|
-
for (
|
|
758
|
+
for (let i = 0; i < size; i++) {
|
|
749
759
|
const parentStack = ctx.stack;
|
|
750
760
|
ctx.stack = [];
|
|
751
|
-
const refParam = serializeTree(ctx,
|
|
761
|
+
const refParam = serializeTree(ctx, values[i]);
|
|
752
762
|
ctx.stack = parentStack;
|
|
763
|
+
const key = keys[i];
|
|
753
764
|
const check = Number(key);
|
|
754
765
|
const parentAssignment = ctx.assignments;
|
|
755
766
|
ctx.assignments = mainAssignments;
|
|
756
767
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
757
|
-
|
|
758
|
-
ctx,
|
|
759
|
-
|
|
760
|
-
isIdentifier ? key : quote(key),
|
|
761
|
-
|
|
762
|
-
!(isIdentifier && Number.isNaN(check))
|
|
763
|
-
);
|
|
768
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
769
|
+
createObjectAssign(ctx, targetRef, key, refParam);
|
|
770
|
+
} else {
|
|
771
|
+
createArrayAssign(ctx, targetRef, isIdentifier ? key : quote(key), refParam);
|
|
772
|
+
}
|
|
764
773
|
ctx.assignments = parentAssignment;
|
|
765
774
|
}
|
|
766
775
|
ctx.stack.pop();
|
|
767
776
|
return resolveAssignments(mainAssignments);
|
|
768
777
|
}
|
|
769
|
-
function
|
|
778
|
+
function serializeObject(ctx, sourceID, [keys, values, size]) {
|
|
779
|
+
if (size === 0) {
|
|
780
|
+
return "{}";
|
|
781
|
+
}
|
|
770
782
|
let result = "";
|
|
771
783
|
ctx.stack.push(sourceID);
|
|
772
|
-
for (
|
|
773
|
-
const
|
|
784
|
+
for (let i = 0; i < size; i++) {
|
|
785
|
+
const key = keys[i];
|
|
786
|
+
const val = values[i];
|
|
774
787
|
const check = Number(key);
|
|
775
788
|
const isIdentifier = check >= 0 || IDENTIFIER_CHECK.test(key);
|
|
776
|
-
const validKey = isIdentifier ? key : quote(key);
|
|
777
789
|
if (isReferenceInStack(ctx, val)) {
|
|
778
790
|
const refParam = getRefParam(ctx, val[1]);
|
|
779
|
-
|
|
780
|
-
ctx,
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
!(isIdentifier && Number.isNaN(check))
|
|
785
|
-
);
|
|
791
|
+
if (isIdentifier && Number.isNaN(check)) {
|
|
792
|
+
createObjectAssign(ctx, sourceID, key, refParam);
|
|
793
|
+
} else {
|
|
794
|
+
createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
|
|
795
|
+
}
|
|
786
796
|
} else {
|
|
787
|
-
result += `${
|
|
797
|
+
result += `${isIdentifier ? key : quote(key)}:${serializeTree(ctx, val)},`;
|
|
788
798
|
}
|
|
789
799
|
}
|
|
790
800
|
ctx.stack.pop();
|
|
@@ -894,7 +904,7 @@ function serializeArray(ctx, value, id) {
|
|
|
894
904
|
return assignRef(ctx, id, result);
|
|
895
905
|
}
|
|
896
906
|
function serializeWithObjectAssign(ctx, value, id, serialized) {
|
|
897
|
-
const fields =
|
|
907
|
+
const fields = serializeObject(ctx, id, value);
|
|
898
908
|
if (fields !== "{}") {
|
|
899
909
|
return `Object.assign(${serialized},${fields})`;
|
|
900
910
|
}
|
|
@@ -1012,28 +1022,38 @@ function serializeTree(ctx, [type, value, id]) {
|
|
|
1012
1022
|
case 9 /* NullConstructor */:
|
|
1013
1023
|
return serializeNullConstructor(ctx, value, id);
|
|
1014
1024
|
case 8 /* Object */:
|
|
1015
|
-
return assignRef(ctx, id,
|
|
1025
|
+
return assignRef(ctx, id, serializeObject(ctx, id, value));
|
|
1016
1026
|
default:
|
|
1017
1027
|
throw new Error("Unsupported type");
|
|
1018
1028
|
}
|
|
1019
1029
|
}
|
|
1020
1030
|
|
|
1021
1031
|
// src/tree/sync.ts
|
|
1022
|
-
function
|
|
1032
|
+
function serializePropertiesSync(ctx, properties) {
|
|
1023
1033
|
const keys = Object.keys(properties);
|
|
1024
|
-
const
|
|
1025
|
-
const
|
|
1034
|
+
const size = keys.length;
|
|
1035
|
+
const keyNodes = new Array(size);
|
|
1036
|
+
const valueNodes = new Array(size);
|
|
1037
|
+
const deferredKeys = new Array(size);
|
|
1038
|
+
const deferredValues = new Array(size);
|
|
1039
|
+
let deferredSize = 0;
|
|
1040
|
+
let nodesSize = 0;
|
|
1026
1041
|
for (const key of keys) {
|
|
1027
1042
|
if (isIterable(properties[key])) {
|
|
1028
|
-
|
|
1043
|
+
deferredKeys[deferredSize] = key;
|
|
1044
|
+
deferredValues[deferredSize] = properties[key];
|
|
1045
|
+
deferredSize++;
|
|
1029
1046
|
} else {
|
|
1030
|
-
|
|
1047
|
+
keyNodes[nodesSize] = key;
|
|
1048
|
+
valueNodes[nodesSize] = generateTreeSync(ctx, properties[key]);
|
|
1049
|
+
nodesSize++;
|
|
1031
1050
|
}
|
|
1032
1051
|
}
|
|
1033
|
-
for (
|
|
1034
|
-
|
|
1052
|
+
for (let i = 0; i < deferredSize; i++) {
|
|
1053
|
+
keyNodes[nodesSize + i] = deferredKeys[i];
|
|
1054
|
+
valueNodes[nodesSize + i] = generateTreeSync(ctx, deferredValues[i]);
|
|
1035
1055
|
}
|
|
1036
|
-
return
|
|
1056
|
+
return [keyNodes, valueNodes, size];
|
|
1037
1057
|
}
|
|
1038
1058
|
function generateSetNode2(ctx, current, id) {
|
|
1039
1059
|
assert(ctx.features & 256 /* Set */, 'Unsupported type "Set"');
|
|
@@ -1105,13 +1125,13 @@ function generateArrayNode2(ctx, current, id) {
|
|
|
1105
1125
|
}
|
|
1106
1126
|
function generateAggregateErrorNode2(ctx, current, id) {
|
|
1107
1127
|
const options = getErrorOptions(current);
|
|
1108
|
-
const optionsNode = options ?
|
|
1128
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1109
1129
|
const errorsNode = generateTreeSync(ctx, current.errors);
|
|
1110
1130
|
return [12 /* AggregateError */, [current.message, optionsNode, errorsNode], id];
|
|
1111
1131
|
}
|
|
1112
1132
|
function generateErrorNode2(ctx, current, id) {
|
|
1113
1133
|
const options = getErrorOptions(current);
|
|
1114
|
-
const optionsNode = options ?
|
|
1134
|
+
const optionsNode = options ? serializePropertiesSync(ctx, options) : void 0;
|
|
1115
1135
|
return [
|
|
1116
1136
|
11 /* Error */,
|
|
1117
1137
|
[getErrorConstructor(current), current.message, optionsNode],
|
|
@@ -1123,7 +1143,7 @@ function generateIterableNode2(ctx, current, id) {
|
|
|
1123
1143
|
const options = getIterableOptions(current);
|
|
1124
1144
|
return [13 /* Iterable */, [
|
|
1125
1145
|
// Parse options first before the items
|
|
1126
|
-
options ?
|
|
1146
|
+
options ? serializePropertiesSync(ctx, options) : void 0,
|
|
1127
1147
|
generateNodeList2(ctx, Array.from(current))
|
|
1128
1148
|
], id];
|
|
1129
1149
|
}
|
|
@@ -1161,7 +1181,7 @@ function generateTreeSync(ctx, current) {
|
|
|
1161
1181
|
if (current.constructor === Object || empty) {
|
|
1162
1182
|
return [
|
|
1163
1183
|
empty ? 9 /* NullConstructor */ : 8 /* Object */,
|
|
1164
|
-
|
|
1184
|
+
serializePropertiesSync(ctx, current),
|
|
1165
1185
|
id
|
|
1166
1186
|
];
|
|
1167
1187
|
}
|
|
@@ -1228,7 +1248,7 @@ function toJSON(source, options) {
|
|
|
1228
1248
|
root,
|
|
1229
1249
|
isObject,
|
|
1230
1250
|
ctx.features,
|
|
1231
|
-
|
|
1251
|
+
ctx.markedRefs
|
|
1232
1252
|
]);
|
|
1233
1253
|
}
|
|
1234
1254
|
async function toJSONAsync(source, options) {
|
|
@@ -1239,7 +1259,7 @@ async function toJSONAsync(source, options) {
|
|
|
1239
1259
|
root,
|
|
1240
1260
|
isObject,
|
|
1241
1261
|
ctx.features,
|
|
1242
|
-
|
|
1262
|
+
ctx.markedRefs
|
|
1243
1263
|
]);
|
|
1244
1264
|
}
|
|
1245
1265
|
function compileJSON(source) {
|