seroval 0.2.1 → 0.4.0-alpha.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 CHANGED
@@ -58,25 +58,25 @@ const result = serialize(object);
58
58
  Output (as a string):
59
59
 
60
60
  ```js
61
- ((h,j,k,m)=>(h={number:[0.5906513825027921,-0,NaN,Infinity,-Infinity],string:["hello world","\x3Cscript>Hello World\x3C/script>"],boolean:[!0,!1],null:null,undefined:void 0,bigint:BigInt("9007199254740991"),array:j=[,,,,k=new Map([["hello","world"],["mutual",m=new Set(["hello","world"])]])],regexp:/[a-z0-9]+/i,date:new Date("2023-02-06T06:16:00.384Z"),map:k,set:m},j[3]=j,h.self=h,k.set("self",k),m.add(m).add(j),h))()
61
+ ((h,j,k,m)=>(m={number:[0.5623457676854244,-0,NaN,1/0,-1/0],string:["hello world","\x3Cscript>Hello World\x3C/script>"],boolean:[!0,!1],null:null,undefined:void 0,bigint:9007199254740991n,array:h=[,,,,j=new Map([["hello","world"],["mutual",k=new Set(["hello","world"])]])],regexp:/[a-z0-9]+/i,date:new Date("2023-03-14T11:16:24.879Z"),map:j,set:k},h[3]=h,j.set("self",j),k.add(k).add(h),m.self=m,m))()
62
62
 
63
63
  // Formatted for readability
64
- ((h, j, k, m) => (h = {
65
- number: [0.5906513825027921, -0, NaN, Infinity, -Infinity],
64
+ ((h, j, k, m) => (m = {
65
+ number: [0.5623457676854244, -0, NaN, 1 / 0, -1 / 0],
66
66
  string: ["hello world", "\x3Cscript>Hello World\x3C/script>"],
67
67
  boolean: [!0, !1],
68
68
  null: null,
69
69
  undefined: void 0,
70
- bigint: BigInt("9007199254740991"),
71
- array: j = [, , , , k = new Map([
70
+ bigint: 9007199254740991n,
71
+ array: h = [, , , , j = new Map([
72
72
  ["hello", "world"],
73
- ["mutual", m = new Set(["hello", "world"])]
73
+ ["mutual", k = new Set(["hello", "world"])]
74
74
  ])],
75
75
  regexp: /[a-z0-9]+/i,
76
- date: new Date("2023-02-06T06:16:00.384Z"),
77
- map: k,
78
- set: m
79
- }, j[3] = j, h.self = h, k.set("self", k), m.add(m).add(j), h))()
76
+ date: new Date("2023-03-14T11:16:24.879Z"),
77
+ map: j,
78
+ set: k
79
+ }, h[3] = h, j.set("self", j), k.add(k).add(h), m.self = m, m))()
80
80
  ```
81
81
 
82
82
  ### Mutual cyclic example
@@ -102,7 +102,7 @@ const result = serialize({ a, b, c, d });
102
102
  Output (as a string):
103
103
 
104
104
  ```js
105
- ((h,j,k,m,o,q)=>(q={a:h=new Map([["name","a"],["children",[j=new Map([["name","c"],["right",k=new Map([["name","b"],["children",o=[,m=new Map([["name","d"]])]]])]]),m]]]),b:k,c:j,d:m},o[0]=j,j.set("left",h),m.set("left",h).set("right",k),q))()
105
+ ((h,j,k,m,o,q)=>(q={a:h=new Map([["name","a"],["children",[j=new Map([["name","c"],["right",o=new Map([["name","b"],["children",k=[,m=new Map([["name","d"]])]]])]]),m]]]),b:o,c:j,d:m},j.set("left",h),k[0]=j,m.set("left",h).set("right",o),q))()
106
106
 
107
107
  // Formatted
108
108
  ((h, j, k, m, o, q) => (q = {
@@ -110,18 +110,18 @@ Output (as a string):
110
110
  ["name", "a"],
111
111
  ["children", [j = new Map([
112
112
  ["name", "c"],
113
- ["right", k = new Map([
113
+ ["right", o = new Map([
114
114
  ["name", "b"],
115
- ["children", o = [, m = new Map([
115
+ ["children", k = [, m = new Map([
116
116
  ["name", "d"]
117
117
  ])]]
118
118
  ])]
119
119
  ]), m]]
120
120
  ]),
121
- b: k,
121
+ b: o,
122
122
  c: j,
123
123
  d: m
124
- }, o[0] = j, j.set("left", h), m.set("left", h).set("right", k), q))()
124
+ }, j.set("left", h), k[0] = j, m.set("left", h).set("right", o), q))()
125
125
  ```
126
126
 
127
127
  ## Deserialization
@@ -172,9 +172,115 @@ The following values are the only values accepted by `seroval`:
172
172
  - `Date`
173
173
  - `Map`
174
174
  - `Set`
175
+ - `TypedArray`
176
+ - `Int8Array`
177
+ - `Int16Array`
178
+ - `Int32Array`
179
+ - `Uint8Array`
180
+ - `Uint16Array`
181
+ - `Uint32Array`
182
+ - `Uint8ClampedArray`
183
+ - `Float32Array`
184
+ - `Float64Array`
185
+ - `BigInt64Array`
186
+ - `BigUint64Array`
187
+ - `Error`
188
+ - `AggregateError`
189
+ - `EvalError`
190
+ - `RangeError`
191
+ - `ReferenceError`
192
+ - `SyntaxError`
193
+ - `TypeError`
194
+ - `URIError`
175
195
  - `Promise` (with `serializeAsync`)
196
+ - [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)
176
197
  - Cyclic references (both self and mutual)
177
198
 
199
+ ## Compat
200
+
201
+ `serialize` and `serializeAsync` can accept a `{ target: string | string[] }` options. The `target` property decides what the serialization output would look like. The default target is `es2023`.
202
+
203
+ ```js
204
+ import { serialize } from 'seroval';
205
+
206
+ const y = Object.create(null);
207
+ y.self = y;
208
+ y.example = 'Hello World';
209
+
210
+ function serializeWithTarget(value, target) {
211
+ console.log('Target is', target)
212
+ const result = serialize(value, {
213
+ target,
214
+ });
215
+ console.log(result);
216
+ }
217
+
218
+ serializeWithTarget(y, 'es5');
219
+ serializeWithTarget(y, 'es2023');
220
+ ```
221
+
222
+ Output
223
+
224
+ ```
225
+ Target is es5
226
+ (function(h){return (h=Object.create(null),h.self=h,h.example="Hello World",h)})()
227
+ Target is es2023
228
+ (h=>(h=Object.assign(Object.create(null),{example:"Hello World"}),h.self=h,h))()
229
+ ```
230
+
231
+ You can also combine targets:
232
+
233
+ ```js
234
+ serialize(value, {
235
+ targets: ['chrome85', 'edge85'],
236
+ });
237
+ ```
238
+
239
+ Supported runtimes:
240
+
241
+ - `es`
242
+ - Valid values are `es5`, `es6` and above (e.g. `es2020`).
243
+ - Desktop
244
+ - `chrome`
245
+ - `edge`
246
+ - `safari`
247
+ - `firefox`
248
+ - `opera`
249
+ - Mobile
250
+ - `ios`
251
+ - `samsung`
252
+ - Runtimes
253
+ - `deno`
254
+ - `node`
255
+
256
+ > **Note**
257
+ > Version for runtimes excluding `es` can use semver format (`major.minor.patch`) e.g. `chrome110`, `node0.12`
258
+
259
+ Feature flags and compat attempt:
260
+
261
+ - `AggregateError`
262
+ - Compiles down to `Error` instead.
263
+ - `Array.prototype.values`
264
+ - Used for iterables, uses `Symbol.iterator` instead.
265
+ - Arrow functions
266
+ - Uses function expressions for top-level, and either method shorthands or function expressions for iterables.
267
+ - `BigInt`
268
+ - Throws when attempted to use, includes `BigInt64Array` and `BigUint64Array`
269
+ - `Map`
270
+ - Throws when attempted to use.
271
+ - Method shorthands
272
+ - Uses function expressions instead.
273
+ - `Object.assign`
274
+ - Uses manual object assignments instead.
275
+ - `Promise`
276
+ - Throws when attempted to use, specially in `serializeAsync`.
277
+ - `Set`
278
+ - Throws when attempted to use.
279
+ - `Symbol.iterator`
280
+ - Throws when attempted to use.
281
+ - `TypedArray`
282
+ - Throws when attempted to use.
283
+
178
284
  ## Sponsors
179
285
 
180
286
  ![Sponsors](https://github.com/lxsmnsyc/sponsors/blob/main/sponsors.svg?raw=true)