seroval 0.4.0-alpha.8 → 0.4.0
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/README.md +81 -55
- package/dist/cjs/development/index.cjs +999 -831
- package/dist/cjs/development/index.cjs.map +4 -4
- package/dist/cjs/production/index.cjs +2 -2
- package/dist/esm/development/index.mjs +999 -831
- package/dist/esm/development/index.mjs.map +4 -4
- package/dist/esm/production/index.mjs +2 -2
- package/dist/types/compat.d.ts +10 -25
- package/dist/types/context.d.ts +17 -15
- package/dist/types/index.d.ts +2 -1
- package/dist/types/tree/primitives.d.ts +17 -0
- package/dist/types/tree/serialize.d.ts +1 -1
- package/dist/types/tree/shared.d.ts +4 -8
- package/dist/types/tree/types.d.ts +108 -59
- package/dist/types/types.d.ts +3 -2
- package/package.json +2 -2
- package/src/compat.ts +10 -246
- package/src/context.ts +37 -26
- package/src/index.ts +33 -41
- package/src/quote.ts +1 -1
- package/src/tree/async.ts +362 -163
- package/src/tree/primitives.ts +150 -0
- package/src/tree/serialize.ts +308 -282
- package/src/tree/shared.ts +43 -119
- package/src/tree/sync.ts +295 -146
- package/src/tree/types.ts +143 -65
- package/src/types.ts +4 -1
- package/dist/types/checks.d.ts +0 -5
- package/src/checks.ts +0 -40
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import assert from '../assert';
|
|
2
|
+
import { Feature } from '../compat';
|
|
3
|
+
import { ParserContext } from '../context';
|
|
4
|
+
import { BigIntTypedArrayValue, TypedArrayValue } from '../types';
|
|
5
|
+
import {
|
|
6
|
+
SerovalBigIntNode,
|
|
7
|
+
SerovalBigIntTypedArrayNode,
|
|
8
|
+
SerovalDateNode,
|
|
9
|
+
SerovalNodeType,
|
|
10
|
+
SerovalPrimitiveNode,
|
|
11
|
+
SerovalReferenceNode,
|
|
12
|
+
SerovalRegExpNode,
|
|
13
|
+
SerovalTypedArrayNode,
|
|
14
|
+
} from './types';
|
|
15
|
+
|
|
16
|
+
export function createPrimitiveNode(
|
|
17
|
+
value: string | number | null,
|
|
18
|
+
): SerovalPrimitiveNode {
|
|
19
|
+
return {
|
|
20
|
+
t: SerovalNodeType.Primitive,
|
|
21
|
+
i: undefined,
|
|
22
|
+
s: value,
|
|
23
|
+
l: undefined,
|
|
24
|
+
c: undefined,
|
|
25
|
+
m: undefined,
|
|
26
|
+
d: undefined,
|
|
27
|
+
a: undefined,
|
|
28
|
+
n: undefined,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const TRUE_NODE = createPrimitiveNode('!0');
|
|
33
|
+
export const FALSE_NODE = createPrimitiveNode('!1');
|
|
34
|
+
export const UNDEFINED_NODE = createPrimitiveNode('void 0');
|
|
35
|
+
export const NULL_NODE = createPrimitiveNode(null);
|
|
36
|
+
export const NEG_ZERO_NODE = createPrimitiveNode('-0');
|
|
37
|
+
export const INFINITY_NODE = createPrimitiveNode('1/0');
|
|
38
|
+
export const NEG_INFINITY_NODE = createPrimitiveNode('-1/0');
|
|
39
|
+
|
|
40
|
+
export function createBigIntNode(
|
|
41
|
+
ctx: ParserContext,
|
|
42
|
+
current: bigint,
|
|
43
|
+
): SerovalBigIntNode {
|
|
44
|
+
assert(ctx.features & Feature.BigInt, 'Unsupported type "BigInt"');
|
|
45
|
+
return {
|
|
46
|
+
t: SerovalNodeType.BigInt,
|
|
47
|
+
i: undefined,
|
|
48
|
+
s: current.toString() + 'n',
|
|
49
|
+
l: undefined,
|
|
50
|
+
c: undefined,
|
|
51
|
+
m: undefined,
|
|
52
|
+
d: undefined,
|
|
53
|
+
a: undefined,
|
|
54
|
+
n: undefined,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function createReferenceNode(id: number): SerovalReferenceNode {
|
|
59
|
+
return {
|
|
60
|
+
t: SerovalNodeType.Reference,
|
|
61
|
+
i: id,
|
|
62
|
+
s: undefined,
|
|
63
|
+
l: undefined,
|
|
64
|
+
c: undefined,
|
|
65
|
+
m: undefined,
|
|
66
|
+
d: undefined,
|
|
67
|
+
a: undefined,
|
|
68
|
+
n: undefined,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function createDateNode(id: number, current: Date): SerovalDateNode {
|
|
73
|
+
return {
|
|
74
|
+
t: SerovalNodeType.Date,
|
|
75
|
+
i: id,
|
|
76
|
+
s: current.toISOString(),
|
|
77
|
+
l: undefined,
|
|
78
|
+
c: undefined,
|
|
79
|
+
m: undefined,
|
|
80
|
+
d: undefined,
|
|
81
|
+
n: undefined,
|
|
82
|
+
a: undefined,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function createRegExpNode(id: number, current: RegExp): SerovalRegExpNode {
|
|
87
|
+
return {
|
|
88
|
+
t: SerovalNodeType.RegExp,
|
|
89
|
+
i: id,
|
|
90
|
+
s: String(current),
|
|
91
|
+
l: undefined,
|
|
92
|
+
c: undefined,
|
|
93
|
+
m: undefined,
|
|
94
|
+
d: undefined,
|
|
95
|
+
a: undefined,
|
|
96
|
+
n: undefined,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function createTypedArrayNode(
|
|
101
|
+
ctx: ParserContext,
|
|
102
|
+
id: number,
|
|
103
|
+
current: TypedArrayValue,
|
|
104
|
+
): SerovalTypedArrayNode {
|
|
105
|
+
const constructor = current.constructor.name;
|
|
106
|
+
assert(ctx.features & Feature.TypedArray, `Unsupported value type "${constructor}"`);
|
|
107
|
+
return {
|
|
108
|
+
t: SerovalNodeType.TypedArray,
|
|
109
|
+
i: id,
|
|
110
|
+
s: current.toString(),
|
|
111
|
+
l: current.byteOffset,
|
|
112
|
+
c: constructor,
|
|
113
|
+
m: undefined,
|
|
114
|
+
d: undefined,
|
|
115
|
+
a: undefined,
|
|
116
|
+
n: undefined,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const BIGINT_FLAG = Feature.BigIntTypedArray | Feature.BigInt;
|
|
121
|
+
|
|
122
|
+
export function createBigIntTypedArrayNode(
|
|
123
|
+
ctx: ParserContext,
|
|
124
|
+
id: number,
|
|
125
|
+
current: BigIntTypedArrayValue,
|
|
126
|
+
): SerovalBigIntTypedArrayNode {
|
|
127
|
+
const constructor = current.constructor.name;
|
|
128
|
+
assert(
|
|
129
|
+
(ctx.features & BIGINT_FLAG) === BIGINT_FLAG,
|
|
130
|
+
`Unsupported value type "${constructor}"`,
|
|
131
|
+
);
|
|
132
|
+
let result = '';
|
|
133
|
+
for (let i = 0, len = current.length; i < len; i++) {
|
|
134
|
+
if (i !== 0) {
|
|
135
|
+
result += ',';
|
|
136
|
+
}
|
|
137
|
+
result += current[i].toString() + 'n';
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
t: SerovalNodeType.BigIntTypedArray,
|
|
141
|
+
i: id,
|
|
142
|
+
s: result,
|
|
143
|
+
l: (current as BigInt64Array).byteOffset,
|
|
144
|
+
c: constructor,
|
|
145
|
+
m: undefined,
|
|
146
|
+
d: undefined,
|
|
147
|
+
a: undefined,
|
|
148
|
+
n: undefined,
|
|
149
|
+
};
|
|
150
|
+
}
|