seroval 0.4.0-alpha.0 → 0.4.0-alpha.2

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/compat.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable guard-for-in */
1
2
  /**
2
3
  * References
3
4
  * - https://kangax.github.io/compat-table/es6/
@@ -38,25 +39,27 @@ export interface PlatformVersion {
38
39
 
39
40
  export type Platform = keyof PlatformVersion;
40
41
 
41
- interface VersionTable {
42
- 'aggregate-error': PlatformVersion;
43
- 'array-values': PlatformVersion;
44
- 'arrow-function': PlatformVersion;
45
- 'bigint': PlatformVersion;
46
- 'map': PlatformVersion;
47
- 'method-shorthand': PlatformVersion;
48
- 'object-assign': PlatformVersion;
49
- 'promise': PlatformVersion;
50
- 'set': PlatformVersion;
51
- 'symbol-iterator': PlatformVersion;
52
- 'typed-arrays': PlatformVersion;
42
+ export const enum Feature {
43
+ AggregateError,
44
+ ArrayPrototypeValues,
45
+ ArrowFunction,
46
+ BigInt,
47
+ Map,
48
+ MethodShorthand,
49
+ ObjectAssign,
50
+ Promise,
51
+ Set,
52
+ SymbolIterator,
53
+ TypedArray,
53
54
  }
54
55
 
55
- export type Feature = keyof VersionTable;
56
+ type VersionTable = {
57
+ [key in Feature]: PlatformVersion;
58
+ }
56
59
 
57
60
  const VERSION_TABLE: VersionTable = {
58
61
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError#browser_compatibility
59
- 'aggregate-error': {
62
+ [Feature.AggregateError]: {
60
63
  es: [2021, 0, 0],
61
64
  chrome: [85, 0, 0],
62
65
  edge: [85, 0, 0],
@@ -69,7 +72,7 @@ const VERSION_TABLE: VersionTable = {
69
72
  node: [15, 0, 0],
70
73
  },
71
74
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values#browser_compatibility
72
- 'array-values': {
75
+ [Feature.ArrayPrototypeValues]: {
73
76
  es: [6, 0, 0],
74
77
  chrome: [66, 0, 0],
75
78
  edge: [14, 0, 0],
@@ -82,7 +85,7 @@ const VERSION_TABLE: VersionTable = {
82
85
  node: [10, 9, 0],
83
86
  },
84
87
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#browser_compatibility
85
- 'arrow-function': {
88
+ [Feature.ArrowFunction]: {
86
89
  es: [6, 0, 0],
87
90
  chrome: [45, 0, 0],
88
91
  edge: [12, 0, 0],
@@ -95,7 +98,7 @@ const VERSION_TABLE: VersionTable = {
95
98
  node: [4, 0, 0],
96
99
  },
97
100
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#browser_compatibility
98
- bigint: {
101
+ [Feature.BigInt]: {
99
102
  es: [2020, 0, 0],
100
103
  chrome: [67, 0, 0],
101
104
  edge: [79, 0, 0],
@@ -108,7 +111,7 @@ const VERSION_TABLE: VersionTable = {
108
111
  node: [10, 4, 0],
109
112
  },
110
113
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#browser_compatibility
111
- map: {
114
+ [Feature.Map]: {
112
115
  es: [6, 0, 0],
113
116
  chrome: [38, 0, 0],
114
117
  edge: [12, 0, 0],
@@ -121,7 +124,7 @@ const VERSION_TABLE: VersionTable = {
121
124
  node: [0, 12, 0],
122
125
  },
123
126
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#browser_compatibility
124
- 'method-shorthand': {
127
+ [Feature.MethodShorthand]: {
125
128
  es: [6, 0, 0],
126
129
  chrome: [39, 0, 0],
127
130
  edge: [12, 0, 0],
@@ -134,7 +137,7 @@ const VERSION_TABLE: VersionTable = {
134
137
  node: [4, 0, 0],
135
138
  },
136
139
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#browser_compatibility
137
- 'object-assign': {
140
+ [Feature.ObjectAssign]: {
138
141
  es: [6, 0, 0],
139
142
  chrome: [45, 0, 0],
140
143
  edge: [12, 0, 0],
@@ -147,7 +150,7 @@ const VERSION_TABLE: VersionTable = {
147
150
  node: [4, 0, 0],
148
151
  },
149
152
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#browser_compatibility
150
- promise: {
153
+ [Feature.Promise]: {
151
154
  es: [6, 0, 0],
152
155
  chrome: [32, 0, 0],
153
156
  edge: [12, 0, 0],
@@ -160,7 +163,7 @@ const VERSION_TABLE: VersionTable = {
160
163
  node: [0, 12, 0],
161
164
  },
162
165
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#browser_compatibility
163
- set: {
166
+ [Feature.Set]: {
164
167
  es: [6, 0, 0],
165
168
  chrome: [38, 0, 0],
166
169
  edge: [12, 0, 0],
@@ -173,7 +176,7 @@ const VERSION_TABLE: VersionTable = {
173
176
  node: [0, 12, 0],
174
177
  },
175
178
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator#browser_compatibility
176
- 'symbol-iterator': {
179
+ [Feature.SymbolIterator]: {
177
180
  es: [6, 0, 0],
178
181
  chrome: [43, 0, 0],
179
182
  edge: [12, 0, 0],
@@ -186,7 +189,7 @@ const VERSION_TABLE: VersionTable = {
186
189
  node: [0, 12, 0],
187
190
  },
188
191
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#browser_compatibility
189
- 'typed-arrays': {
192
+ [Feature.TypedArray]: {
190
193
  es: [6, 0, 0],
191
194
  chrome: [7, 0, 0],
192
195
  edge: [12, 0, 0],
@@ -223,23 +226,19 @@ function getTargetVersions(targets: string | string[]): Target[] {
223
226
  return [parseTarget(targets)];
224
227
  }
225
228
 
226
- function isFeatureSupported(key: Feature, targets: Target[]) {
227
- const base = VERSION_TABLE[key];
228
- let flag = true;
229
- for (const [platform, version] of targets) {
230
- const baseVersion = base[platform];
231
- flag = flag && !!baseVersion && (compareVersion(baseVersion, version) <= 0);
232
- }
233
- return flag;
234
- }
235
-
236
229
  export function parseTargets(targets: string | string[]): Set<Feature> {
237
230
  const parsed = getTargetVersions(targets);
238
231
  const flags = new Set<Feature>();
239
232
 
240
233
  for (const key in VERSION_TABLE) {
241
- if (isFeatureSupported(key as Feature, parsed)) {
242
- flags.add(key as Feature);
234
+ const base = VERSION_TABLE[key as unknown as Feature];
235
+ let flag = true;
236
+ for (const [platform, version] of parsed) {
237
+ const baseVersion = base[platform];
238
+ flag = flag && !!baseVersion && (compareVersion(baseVersion, version) <= 0);
239
+ }
240
+ if (flag) {
241
+ flags.add(Number(key) as unknown as Feature);
243
242
  }
244
243
  }
245
244
 
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  /* eslint-disable no-await-in-loop */
2
2
  import { isPrimitive } from './checks';
3
+ import { Feature } from './compat';
3
4
  import {
4
5
  createRef,
5
6
  createSerializationContext,
@@ -53,7 +54,7 @@ function finalize<T extends NonPrimitiveServerValue<ServerValue | AsyncServerVal
53
54
  ? ctx.vars.join(',')
54
55
  : ctx.vars[0];
55
56
  // Source is probably already assigned
56
- if (ctx.features.has('arrow-function')) {
57
+ if (ctx.features.has(Feature.ArrowFunction)) {
57
58
  params = ctx.vars.length > 1 || ctx.vars.length === 0
58
59
  ? `(${params})`
59
60
  : params;