Skip to content

Commit

Permalink
Fix nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Mar 16, 2023
1 parent 9eefa7a commit 7aa37a5
Show file tree
Hide file tree
Showing 36 changed files with 386 additions and 248 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@types/node": "^18.15.3",
"esbuild": "^0.17.11",
"eslint": "^8.36.0",
"eslint-config-lxsmnsyc": "^0.5.1",
"eslint-config-lxsmnsyc": "^0.4.8",
"tslib": "^2.4.1",
"typescript": "^4.9.5"
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"devDependencies": {
"eslint": "^8.33.0",
"eslint-config-lxsmnsyc": "^0.5.1",
"eslint-config-lxsmnsyc": "^0.4.8",
"lerna": "^6.4.1",
"typescript": "^4.9.5"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/seroval/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"devDependencies": {
"@types/node": "^18.15.3",
"eslint": "^8.32.0",
"eslint-config-lxsmnsyc": "^0.5.1",
"eslint-config-lxsmnsyc": "^0.4.8",
"pridepack": "2.4.1",
"tslib": "^2.4.1",
"typescript": "^4.9.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/seroval/src/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ export default function quote(str: string) {
} else {
result += str.slice(lastPos);
}
return '"' + result + '"';
return result;
}
19 changes: 13 additions & 6 deletions packages/seroval/src/tree/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import assert from '../assert';
import { Feature } from '../compat';
import { createRef, ParserContext } from '../context';
import quote from '../quote';
import {
AsyncServerValue,
BigIntTypedArrayValue,
Expand All @@ -13,11 +12,15 @@ import {
createBigIntNode,
createBigIntTypedArrayNode,
createDateNode,
createPrimitiveNode,
createNumberNode,
createReferenceNode,
createRegExpNode,
createStringNode,
createTypedArrayNode,
FALSE_NODE,
INFINITY_NODE,
NAN_NODE,
NEG_INFINITY_NODE,
NEG_ZERO_NODE,
NULL_NODE,
TRUE_NODE,
Expand Down Expand Up @@ -335,18 +338,22 @@ async function parse(
case 'undefined':
return UNDEFINED_NODE;
case 'string':
return createPrimitiveNode(quote(current));
return createStringNode(current);
case 'number':
// eslint-disable-next-line no-self-compare
if (current !== current) {
return NAN_NODE;
}
if (Object.is(current, -0)) {
return NEG_ZERO_NODE;
}
if (Object.is(current, Infinity)) {
return createPrimitiveNode('1/0');
return INFINITY_NODE;
}
if (Object.is(current, -Infinity)) {
return createPrimitiveNode('-1/0');
return NEG_INFINITY_NODE;
}
return createPrimitiveNode(current);
return createNumberNode(current);
case 'bigint':
return createBigIntNode(ctx, current);
case 'object': {
Expand Down
128 changes: 115 additions & 13 deletions packages/seroval/src/tree/primitives.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,119 @@
import assert from '../assert';
import { Feature } from '../compat';
import { ParserContext } from '../context';
import quote from '../quote';
import { BigIntTypedArrayValue, TypedArrayValue } from '../types';
import {
SerovalBigIntNode,
SerovalBigIntTypedArrayNode,
SerovalBooleanNode,
SerovalDateNode,
SerovalInfinityNode,
SerovalNaNNode,
SerovalNegativeInfinityNode,
SerovalNegativeZeroNode,
SerovalNodeType,
SerovalPrimitiveNode,
SerovalNullNode,
SerovalNumberNode,
SerovalReferenceNode,
SerovalRegExpNode,
SerovalStringNode,
SerovalTypedArrayNode,
SerovalUndefinedNode,
} from './types';

export function createPrimitiveNode(
value: string | number | null,
): SerovalPrimitiveNode {
export const TRUE_NODE: SerovalBooleanNode = {
t: SerovalNodeType.Boolean,
i: undefined,
s: true,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: undefined,
n: undefined,
};
export const FALSE_NODE: SerovalBooleanNode = {
t: SerovalNodeType.Boolean,
i: undefined,
s: false,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: undefined,
n: undefined,
};
export const UNDEFINED_NODE: SerovalUndefinedNode = {
t: SerovalNodeType.Undefined,
i: undefined,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: undefined,
n: undefined,
};
export const NULL_NODE: SerovalNullNode = {
t: SerovalNodeType.Null,
i: undefined,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: undefined,
n: undefined,
};
export const NEG_ZERO_NODE: SerovalNegativeZeroNode = {
t: SerovalNodeType.NegativeZero,
i: undefined,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: undefined,
n: undefined,
};
export const INFINITY_NODE: SerovalInfinityNode = {
t: SerovalNodeType.Infinity,
i: undefined,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: undefined,
n: undefined,
};
export const NEG_INFINITY_NODE: SerovalNegativeInfinityNode = {
t: SerovalNodeType.NegativeInfinity,
i: undefined,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: undefined,
n: undefined,
};
export const NAN_NODE: SerovalNaNNode = {
t: SerovalNodeType.NaN,
i: undefined,
s: undefined,
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: undefined,
n: undefined,
};

export function createNumberNode(value: number): SerovalNumberNode {
return {
t: SerovalNodeType.Primitive,
t: SerovalNodeType.Number,
i: undefined,
s: value,
l: undefined,
Expand All @@ -29,13 +125,19 @@ export function createPrimitiveNode(
};
}

export const TRUE_NODE = createPrimitiveNode('!0');
export const FALSE_NODE = createPrimitiveNode('!1');
export const UNDEFINED_NODE = createPrimitiveNode('void 0');
export const NULL_NODE = createPrimitiveNode(null);
export const NEG_ZERO_NODE = createPrimitiveNode('-0');
export const INFINITY_NODE = createPrimitiveNode('1/0');
export const NEG_INFINITY_NODE = createPrimitiveNode('-1/0');
export function createStringNode(value: string): SerovalStringNode {
return {
t: SerovalNodeType.String,
i: undefined,
s: quote(value),
l: undefined,
c: undefined,
m: undefined,
d: undefined,
a: undefined,
n: undefined,
};
}

export function createBigIntNode(
ctx: ParserContext,
Expand All @@ -45,7 +147,7 @@ export function createBigIntNode(
return {
t: SerovalNodeType.BigInt,
i: undefined,
s: current.toString() + 'n',
s: current.toString(),
l: undefined,
c: undefined,
m: undefined,
Expand Down
30 changes: 23 additions & 7 deletions packages/seroval/src/tree/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,13 @@ function serializeObject(
if (isIdentifier && Number.isNaN(check)) {
createObjectAssign(ctx, sourceID, key, refParam);
} else {
createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
createArrayAssign(ctx, sourceID, isIdentifier ? key : ('"' + quote(key) + '"'), refParam);
}
} else {
if (hasPrev) {
result += ',';
}
result += isIdentifier ? key : quote(key);
result += isIdentifier ? key : ('"' + quote(key) + '"');
result += ':' + serializeTree(ctx, val);
hasPrev = true;
}
Expand Down Expand Up @@ -322,7 +322,7 @@ function serializeAssignments(
if (isIdentifier && Number.isNaN(check)) {
createObjectAssign(ctx, sourceID, key, refParam);
} else {
createArrayAssign(ctx, sourceID, isIdentifier ? key : quote(key), refParam);
createArrayAssign(ctx, sourceID, isIdentifier ? key : ('"' + quote(key) + '"'), refParam);
}
ctx.assignments = parentAssignment;
}
Expand Down Expand Up @@ -462,7 +462,7 @@ function serializeAggregateError(
) {
// Serialize the required arguments
ctx.stack.push(node.i);
const serialized = 'new AggregateError(' + serializeTree(ctx, node.n) + ',' + quote(node.m) + ')';
const serialized = 'new AggregateError(' + serializeTree(ctx, node.n) + ',"' + quote(node.m) + '")';
ctx.stack.pop();
// `AggregateError` might've been extended
// either through class or custom properties
Expand All @@ -474,7 +474,7 @@ function serializeError(
ctx: SerializationContext,
node: SerovalErrorNode,
) {
const serialized = 'new ' + node.c + '(' + quote(node.m) + ')';
const serialized = 'new ' + node.c + '("' + quote(node.m) + '")';
return serializeDictionary(ctx, node.i, node.d, serialized);
}

Expand Down Expand Up @@ -545,10 +545,26 @@ export default function serializeTree(
node: SerovalNode,
): string {
switch (node.t) {
case SerovalNodeType.Primitive:
case SerovalNodeType.Number:
return String(node.s);
case SerovalNodeType.String:
return '"' + node.s + '"';
case SerovalNodeType.Boolean:
return node.s ? '!0' : '!1';
case SerovalNodeType.Undefined:
return 'undefined';
case SerovalNodeType.Null:
return 'null';
case SerovalNodeType.NegativeZero:
return '-0';
case SerovalNodeType.Infinity:
return '1/0';
case SerovalNodeType.NegativeInfinity:
return '-1/0';
case SerovalNodeType.NaN:
return 'NaN';
case SerovalNodeType.BigInt:
return node.s;
return node.s + 'n';
case SerovalNodeType.Reference:
return getRefParam(ctx, node.i);
case SerovalNodeType.Array:
Expand Down
19 changes: 13 additions & 6 deletions packages/seroval/src/tree/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
import assert from '../assert';
import { Feature } from '../compat';
import { createRef, ParserContext } from '../context';
import quote from '../quote';
import { BigIntTypedArrayValue, ServerValue, TypedArrayValue } from '../types';
import {
createBigIntNode,
createBigIntTypedArrayNode,
createDateNode,
createPrimitiveNode,
createNumberNode,
createReferenceNode,
createRegExpNode,
createStringNode,
createTypedArrayNode,
FALSE_NODE,
INFINITY_NODE,
NAN_NODE,
NEG_INFINITY_NODE,
NEG_ZERO_NODE,
NULL_NODE,
TRUE_NODE,
Expand Down Expand Up @@ -294,18 +297,22 @@ function parse(
case 'undefined':
return UNDEFINED_NODE;
case 'string':
return createPrimitiveNode(quote(current));
return createStringNode(current);
case 'number':
// eslint-disable-next-line no-self-compare
if (current !== current) {
return NAN_NODE;
}
if (Object.is(current, -0)) {
return NEG_ZERO_NODE;
}
if (Object.is(current, Infinity)) {
return createPrimitiveNode('1/0');
return INFINITY_NODE;
}
if (Object.is(current, -Infinity)) {
return createPrimitiveNode('-1/0');
return NEG_INFINITY_NODE;
}
return createPrimitiveNode(current);
return createNumberNode(current);
case 'bigint':
return createBigIntNode(ctx, current);
case 'object': {
Expand Down
Loading

0 comments on commit 7aa37a5

Please sign in to comment.