seroval 0.7.0 → 0.9.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.
Files changed (42) hide show
  1. package/dist/cjs/development/index.cjs +1247 -1069
  2. package/dist/cjs/development/index.cjs.map +4 -4
  3. package/dist/cjs/production/index.cjs +3 -3
  4. package/dist/esm/development/index.mjs +1247 -1069
  5. package/dist/esm/development/index.mjs.map +4 -4
  6. package/dist/esm/production/index.mjs +3 -3
  7. package/dist/types/assert.d.ts +1 -1
  8. package/dist/types/context.d.ts +8 -2
  9. package/dist/types/index.d.ts +3 -4
  10. package/dist/types/string.d.ts +1 -1
  11. package/dist/types/tree/UnsupportedTypeError.d.ts +3 -0
  12. package/dist/types/tree/async.d.ts +3 -3
  13. package/dist/types/tree/constants.d.ts +11 -0
  14. package/dist/types/tree/deserialize.d.ts +2 -2
  15. package/dist/types/tree/primitives.d.ts +9 -15
  16. package/dist/types/tree/serialize.d.ts +2 -2
  17. package/dist/types/tree/shared.d.ts +12 -8
  18. package/dist/types/tree/sync.d.ts +3 -3
  19. package/dist/types/tree/types.d.ts +74 -71
  20. package/dist/types/tree/web-api.d.ts +2 -2
  21. package/dist/types/types.d.ts +4 -4
  22. package/package.json +11 -9
  23. package/src/assert.ts +2 -2
  24. package/src/compat.ts +0 -1
  25. package/src/context.ts +20 -7
  26. package/src/get-identifier.ts +1 -1
  27. package/src/index.ts +35 -26
  28. package/src/string.ts +2 -2
  29. package/src/tree/UnsupportedTypeError.ts +8 -0
  30. package/src/tree/async.ts +256 -233
  31. package/src/tree/constants.ts +73 -0
  32. package/src/tree/deserialize.ts +112 -93
  33. package/src/tree/primitives.ts +93 -134
  34. package/src/tree/reference.ts +3 -3
  35. package/src/tree/serialize.ts +307 -201
  36. package/src/tree/shared.ts +45 -22
  37. package/src/tree/symbols.ts +13 -13
  38. package/src/tree/sync.ts +236 -198
  39. package/src/tree/types.ts +87 -94
  40. package/src/tree/web-api.ts +14 -7
  41. package/src/types.ts +7 -3
  42. package/src/tree/iterator.ts +0 -14
@@ -1,11 +1,11 @@
1
1
  import { Feature } from '../compat';
2
- import { ParserContext } from '../context';
3
- import {
4
- AsyncServerValue,
2
+ import type { ParserContext } from '../context';
3
+ import type {
5
4
  ErrorValue,
6
5
  } from '../types';
6
+ import { SerovalObjectFlags } from './types';
7
7
 
8
- export function getErrorConstructorName(error: ErrorValue) {
8
+ export function getErrorConstructorName(error: ErrorValue): string {
9
9
  if (error instanceof EvalError) {
10
10
  return 'EvalError';
11
11
  }
@@ -27,7 +27,16 @@ export function getErrorConstructorName(error: ErrorValue) {
27
27
  return 'Error';
28
28
  }
29
29
 
30
- export function getErrorConstructor(errorName: string) {
30
+ type ErrorConstructors =
31
+ | ErrorConstructor
32
+ | EvalErrorConstructor
33
+ | RangeErrorConstructor
34
+ | ReferenceErrorConstructor
35
+ | SyntaxErrorConstructor
36
+ | TypeErrorConstructor
37
+ | URIErrorConstructor;
38
+
39
+ export function getErrorConstructor(errorName: string): ErrorConstructors {
31
40
  switch (errorName) {
32
41
  case 'Error': return Error;
33
42
  case 'EvalError': return EvalError;
@@ -44,8 +53,8 @@ export function getErrorConstructor(errorName: string) {
44
53
  export function getErrorOptions(
45
54
  ctx: ParserContext,
46
55
  error: Error,
47
- ) {
48
- let options: Record<string, any> | undefined;
56
+ ): Record<string, unknown> | undefined {
57
+ let options: Record<string, unknown> | undefined;
49
58
  const constructor = getErrorConstructorName(error);
50
59
  // Name has been modified
51
60
  if (error.name !== constructor) {
@@ -72,21 +81,9 @@ export function getErrorOptions(
72
81
  return options;
73
82
  }
74
83
 
75
- export function getIterableOptions(obj: Iterable<any>) {
76
- const names = Object.getOwnPropertyNames(obj);
77
- if (names.length) {
78
- const options: Record<string, unknown> = {};
79
- for (const name of names) {
80
- options[name] = obj[name as unknown as keyof typeof obj];
81
- }
82
- return options;
83
- }
84
- return undefined;
85
- }
86
-
87
84
  export function isIterable(
88
85
  value: unknown,
89
- ): value is Iterable<AsyncServerValue> {
86
+ ): value is Iterable<unknown> {
90
87
  if (!value || typeof value !== 'object') {
91
88
  return false;
92
89
  }
@@ -114,7 +111,20 @@ export function isIterable(
114
111
  return Symbol.iterator in value;
115
112
  }
116
113
 
117
- export function getTypedArrayConstructor(name: string) {
114
+ type TypedArrayConstructor =
115
+ | Int8ArrayConstructor
116
+ | Int16ArrayConstructor
117
+ | Int32ArrayConstructor
118
+ | Uint8ArrayConstructor
119
+ | Uint16ArrayConstructor
120
+ | Uint32ArrayConstructor
121
+ | Uint8ClampedArrayConstructor
122
+ | Float32ArrayConstructor
123
+ | Float64ArrayConstructor
124
+ | BigInt64ArrayConstructor
125
+ | BigUint64ArrayConstructor;
126
+
127
+ export function getTypedArrayConstructor(name: string): TypedArrayConstructor {
118
128
  switch (name) {
119
129
  case 'Int8Array': return Int8Array;
120
130
  case 'Int16Array': return Int16Array;
@@ -134,7 +144,7 @@ export function getTypedArrayConstructor(name: string) {
134
144
 
135
145
  const IDENTIFIER_CHECK = /^[$A-Z_][0-9A-Z_$]*$/i;
136
146
 
137
- export function isValidIdentifier(name: string) {
147
+ export function isValidIdentifier(name: string): boolean {
138
148
  const char = name[0];
139
149
  return (
140
150
  char === '$'
@@ -143,3 +153,16 @@ export function isValidIdentifier(name: string) {
143
153
  || (char >= 'a' && char <= 'z')
144
154
  ) && IDENTIFIER_CHECK.test(name);
145
155
  }
156
+
157
+ export function getObjectFlag(obj: unknown): SerovalObjectFlags {
158
+ if (Object.isFrozen(obj)) {
159
+ return SerovalObjectFlags.Frozen;
160
+ }
161
+ if (Object.isSealed(obj)) {
162
+ return SerovalObjectFlags.Sealed;
163
+ }
164
+ if (Object.isExtensible(obj)) {
165
+ return SerovalObjectFlags.None;
166
+ }
167
+ return SerovalObjectFlags.NonExtensible;
168
+ }
@@ -1,17 +1,17 @@
1
1
  export const enum Symbols {
2
- AsyncIterator,
3
- HasInstance,
4
- IsConcatSpreadable,
5
- Iterator,
6
- Match,
7
- MatchAll,
8
- Replace,
9
- Search,
10
- Species,
11
- Split,
12
- ToPrimitive,
13
- ToStringTag,
14
- Unscopables
2
+ AsyncIterator = 0,
3
+ HasInstance = 1,
4
+ IsConcatSpreadable = 2,
5
+ Iterator = 3,
6
+ Match = 4,
7
+ MatchAll = 5,
8
+ Replace = 6,
9
+ Search = 7,
10
+ Species = 8,
11
+ Split = 9,
12
+ ToPrimitive = 10,
13
+ ToStringTag = 11,
14
+ Unscopables = 12,
15
15
  }
16
16
 
17
17
  export const SYMBOL_STRING: Record<Symbols, string> = {