seroval 0.4.0-alpha.9 → 0.4.1

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/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  /* eslint-disable no-await-in-loop */
2
- import { isPrimitive } from './checks';
3
2
  import { Feature } from './compat';
4
3
  import {
5
4
  SerializationContext,
@@ -10,7 +9,6 @@ import {
10
9
  } from './context';
11
10
  import parseAsync from './tree/async';
12
11
  import serializeTree, { resolvePatches } from './tree/serialize';
13
- import { serializePrimitive } from './tree/shared';
14
12
  import parseSync from './tree/sync';
15
13
  import { SerovalNode } from './tree/types';
16
14
  import {
@@ -29,6 +27,7 @@ export {
29
27
  CommonServerValue,
30
28
  SemiPrimitiveValue,
31
29
  ErrorValue,
30
+ Feature,
32
31
  };
33
32
 
34
33
  function finalize(
@@ -44,10 +43,9 @@ function finalize(
44
43
  if (patches) {
45
44
  // Get (or create) a ref from the source
46
45
  const index = getRefParam(ctx, rootID);
47
- if (result.startsWith(`${index}=`)) {
48
- body = `${result},${patches}${index}`;
49
- } else {
50
- body = `${index}=${result},${patches}${index}`;
46
+ body = result + ',' + patches + index;
47
+ if (!result.startsWith(index + '=')) {
48
+ body = index + '=' + body;
51
49
  }
52
50
  }
53
51
  let params = ctx.vars.length > 1
@@ -56,14 +54,14 @@ function finalize(
56
54
  // Source is probably already assigned
57
55
  if (ctx.features & Feature.ArrowFunction) {
58
56
  params = ctx.vars.length > 1 || ctx.vars.length === 0
59
- ? `(${params})`
57
+ ? '(' + params + ')'
60
58
  : params;
61
- return `(${params}=>(${body}))()`;
59
+ return '(' + params + '=>(' + body + '))()';
62
60
  }
63
- return `(function(${params}){return ${body}})()`;
61
+ return '(function(' + params + '){return ' + body + '})()';
64
62
  }
65
63
  if (isObject) {
66
- return `(${result})`;
64
+ return '(' + result + ')';
67
65
  }
68
66
  return result;
69
67
  }
@@ -73,9 +71,6 @@ export function serialize<T extends ServerValue>(
73
71
  options?: Partial<Options>,
74
72
  ) {
75
73
  const ctx = createParserContext(options);
76
- if (isPrimitive(source)) {
77
- return String(serializePrimitive(source));
78
- }
79
74
  const [tree, rootID, isObject] = parseSync(ctx, source);
80
75
  const serial = createSerializationContext(ctx);
81
76
  const result = serializeTree(serial, tree);
@@ -87,9 +82,6 @@ export async function serializeAsync<T extends AsyncServerValue>(
87
82
  options?: Partial<Options>,
88
83
  ) {
89
84
  const ctx = createParserContext(options);
90
- if (isPrimitive(source)) {
91
- return String(serializePrimitive(source));
92
- }
93
85
  const [tree, rootID, isObject] = await parseAsync(ctx, source);
94
86
  const serial = createSerializationContext(ctx);
95
87
  const result = serializeTree(serial, tree);
@@ -101,55 +93,54 @@ export function deserialize<T extends AsyncServerValue>(source: string): T {
101
93
  return (0, eval)(source) as T;
102
94
  }
103
95
 
104
- type SerovalJSON = [
105
- tree: SerovalNode,
106
- root: number,
107
- isObject: boolean,
108
- feature: number,
109
- markedRefs: Record<number, number>,
110
- ];
96
+ export interface SerovalJSON {
97
+ t: SerovalNode,
98
+ r: number,
99
+ i: boolean,
100
+ f: number,
101
+ m: number[],
102
+ }
111
103
 
112
104
  export function toJSON<T extends ServerValue>(
113
105
  source: T,
114
106
  options?: Partial<Options>,
115
- ) {
107
+ ): SerovalJSON {
116
108
  const ctx = createParserContext(options);
117
109
  const [tree, root, isObject] = parseSync(ctx, source);
118
- return JSON.stringify([
119
- tree,
120
- root,
121
- isObject,
122
- ctx.features,
123
- ctx.markedRefs,
124
- ]);
110
+ return {
111
+ t: tree,
112
+ r: root,
113
+ i: isObject,
114
+ f: ctx.features,
115
+ m: Array.from(ctx.markedRefs),
116
+ };
125
117
  }
126
118
 
127
119
  export async function toJSONAsync<T extends AsyncServerValue>(
128
120
  source: T,
129
121
  options?: Partial<Options>,
130
- ) {
122
+ ): Promise<SerovalJSON> {
131
123
  const ctx = createParserContext(options);
132
124
  const [tree, root, isObject] = await parseAsync(ctx, source);
133
- return JSON.stringify([
134
- tree,
135
- root,
136
- isObject,
137
- ctx.features,
138
- ctx.markedRefs,
139
- ]);
125
+ return {
126
+ t: tree,
127
+ r: root,
128
+ i: isObject,
129
+ f: ctx.features,
130
+ m: Array.from(ctx.markedRefs),
131
+ };
140
132
  }
141
133
 
142
- export function compileJSON(source: string): string {
143
- const parsed = JSON.parse(source) as SerovalJSON;
134
+ export function compileJSON(source: SerovalJSON): string {
144
135
  const serial = createSerializationContext({
145
- features: parsed[3],
146
- markedRefs: parsed[4],
136
+ features: source.f,
137
+ markedRefs: source.m,
147
138
  });
148
- const result = serializeTree(serial, parsed[0]);
149
- return finalize(serial, parsed[1], parsed[2], result);
139
+ const result = serializeTree(serial, source.t);
140
+ return finalize(serial, source.r, source.i, result);
150
141
  }
151
142
 
152
- export function fromJSON<T extends AsyncServerValue>(source: string): T {
143
+ export function fromJSON<T extends AsyncServerValue>(source: SerovalJSON): T {
153
144
  return deserialize<T>(compileJSON(source));
154
145
  }
155
146
 
package/src/quote.ts CHANGED
@@ -41,5 +41,5 @@ export default function quote(str: string) {
41
41
  } else {
42
42
  result += str.slice(lastPos);
43
43
  }
44
- return `"${result}"`;
44
+ return '"' + result + '"';
45
45
  }