@kaiko.io/rescript-deser 6.0.1 → 7.0.0-alpha.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.
@@ -1,7 +1,6 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
  'use strict';
3
3
 
4
- var Js_json = require("rescript/lib/js/js_json.js");
5
4
  var Belt_Array = require("rescript/lib/js/belt_Array.js");
6
5
  var Core__Dict = require("@rescript/core/lib/js/src/Core__Dict.js");
7
6
  var Caml_option = require("rescript/lib/js/caml_option.js");
@@ -139,28 +138,26 @@ function toString(type_) {
139
138
  }
140
139
 
141
140
  function _taggedToString(tagged) {
142
- if (typeof tagged !== "object") {
143
- switch (tagged) {
144
- case "JSONFalse" :
145
- return "Boolean(false)";
146
- case "JSONTrue" :
141
+ if (!Array.isArray(tagged) && (tagged === null || typeof tagged !== "object") && typeof tagged !== "number" && typeof tagged !== "string" && typeof tagged !== "boolean") {
142
+ return "Null";
143
+ }
144
+ if (Array.isArray(tagged)) {
145
+ return "Array(" + Core__Option.getOr(JSON.stringify(tagged), "...") + ")";
146
+ }
147
+ switch (typeof tagged) {
148
+ case "boolean" :
149
+ if (tagged) {
147
150
  return "Boolean(true)";
148
- case "JSONNull" :
149
- return "Null";
150
-
151
- }
152
- } else {
153
- switch (tagged.TAG) {
154
- case "JSONString" :
155
- return "String(\"" + tagged._0 + "\")";
156
- case "JSONNumber" :
157
- return "Number(" + tagged._0.toString() + ")";
158
- case "JSONObject" :
159
- return "Object(" + Core__Option.getOr(JSON.stringify(tagged._0), "...") + ")";
160
- case "JSONArray" :
161
- return "Array(" + Core__Option.getOr(JSON.stringify(tagged._0), "...") + ")";
162
-
163
- }
151
+ } else {
152
+ return "Boolean(false)";
153
+ }
154
+ case "string" :
155
+ return "String(\"" + tagged + "\")";
156
+ case "number" :
157
+ return "Number(" + tagged.toString() + ")";
158
+ case "object" :
159
+ return "Object(" + Core__Option.getOr(JSON.stringify(tagged), "...") + ")";
160
+
164
161
  }
165
162
  }
166
163
 
@@ -190,93 +187,34 @@ function extractValue(values, field, shape, self) {
190
187
  function fromUntagged(untagged, _shape, self) {
191
188
  while(true) {
192
189
  var shape = _shape;
193
- var match = Js_json.classify(untagged);
194
190
  var exit = 0;
195
- var s;
196
- var f;
197
191
  if (typeof shape !== "object") {
198
192
  switch (shape) {
199
193
  case "Any" :
200
194
  return untagged;
201
195
  case "String" :
202
- if (typeof match !== "object") {
203
- exit = 1;
204
- } else {
205
- if (match.TAG === "JSONString") {
206
- return match._0;
207
- }
208
- exit = 1;
196
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && typeof untagged === "string") {
197
+ return untagged;
209
198
  }
210
199
  break;
211
200
  case "Int" :
212
- if (typeof match !== "object") {
213
- exit = 1;
214
- } else {
215
- if (match.TAG === "JSONNumber") {
216
- return match._0 | 0;
217
- }
218
- exit = 1;
201
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && typeof untagged === "number") {
202
+ return untagged | 0;
219
203
  }
220
204
  break;
221
205
  case "Float" :
222
- if (typeof match !== "object") {
223
- exit = 1;
224
- } else {
225
- if (match.TAG === "JSONNumber") {
226
- return match._0;
227
- }
228
- exit = 1;
206
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && typeof untagged === "number") {
207
+ return untagged;
229
208
  }
230
209
  break;
231
210
  case "Boolean" :
232
- if (typeof match !== "object") {
233
- switch (match) {
234
- case "JSONFalse" :
235
- return false;
236
- case "JSONTrue" :
237
- return true;
238
- default:
239
- exit = 1;
240
- }
241
- } else {
242
- exit = 1;
211
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && typeof untagged === "boolean") {
212
+ return untagged;
243
213
  }
244
214
  break;
245
215
  case "Date" :
246
- if (typeof match !== "object") {
247
- exit = 1;
248
- } else {
249
- switch (match.TAG) {
250
- case "JSONString" :
251
- s = match._0;
252
- exit = 2;
253
- break;
254
- case "JSONNumber" :
255
- f = match._0;
256
- exit = 3;
257
- break;
258
- default:
259
- exit = 1;
260
- }
261
- }
262
- break;
263
216
  case "Datetime" :
264
- if (typeof match !== "object") {
265
- exit = 1;
266
- } else {
267
- switch (match.TAG) {
268
- case "JSONString" :
269
- s = match._0;
270
- exit = 2;
271
- break;
272
- case "JSONNumber" :
273
- f = match._0;
274
- exit = 3;
275
- break;
276
- default:
277
- exit = 1;
278
- }
279
- }
217
+ exit = 2;
280
218
  break;
281
219
  case "Self" :
282
220
  _shape = self;
@@ -286,133 +224,91 @@ function fromUntagged(untagged, _shape, self) {
286
224
  } else {
287
225
  switch (shape.TAG) {
288
226
  case "Literal" :
289
- if (typeof match !== "object") {
290
- exit = 1;
291
- } else {
292
- if (match.TAG === "JSONString") {
293
- var text = match._0;
294
- var expected = shape._0;
295
- if (text === expected) {
296
- return text;
297
- }
298
- throw {
299
- RE_EXN_ID: $$TypeError,
300
- _1: "Expecting literal " + expected + ", got " + text,
301
- Error: new Error()
302
- };
227
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && typeof untagged === "string") {
228
+ var expected = shape._0;
229
+ if (untagged === expected) {
230
+ return untagged;
303
231
  }
304
- exit = 1;
232
+ throw {
233
+ RE_EXN_ID: $$TypeError,
234
+ _1: "Expecting literal " + expected + ", got " + untagged,
235
+ Error: new Error()
236
+ };
305
237
  }
306
238
  break;
307
239
  case "Array" :
308
- if (typeof match !== "object") {
309
- exit = 1;
310
- } else {
311
- if (match.TAG === "JSONArray") {
312
- var shape$1 = shape._0;
313
- return match._0.map((function(shape$1){
314
- return function (item) {
315
- return fromUntagged(item, shape$1, self);
316
- }
317
- }(shape$1)));
318
- }
319
- exit = 1;
240
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && Array.isArray(untagged)) {
241
+ var shape$1 = shape._0;
242
+ return untagged.map((function(shape$1){
243
+ return function (item) {
244
+ return fromUntagged(item, shape$1, self);
245
+ }
246
+ }(shape$1)));
320
247
  }
321
248
  break;
322
249
  case "Tuple" :
323
- if (typeof match !== "object") {
324
- exit = 1;
325
- } else {
326
- if (match.TAG === "JSONArray") {
327
- var items = match._0;
328
- var bases = shape._0;
329
- var lenbases = bases.length;
330
- var lenitems = items.length;
331
- if (lenbases === lenitems) {
332
- return Belt_Array.zipBy(items, bases, (function (i, b) {
333
- return fromUntagged(i, b, self);
334
- }));
335
- }
336
- throw {
337
- RE_EXN_ID: $$TypeError,
338
- _1: "Expecting " + lenbases.toString() + " items, got " + lenitems.toString(),
339
- Error: new Error()
340
- };
250
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && Array.isArray(untagged)) {
251
+ var bases = shape._0;
252
+ var lenbases = bases.length;
253
+ var lenitems = untagged.length;
254
+ if (lenbases === lenitems) {
255
+ return Belt_Array.zipBy(untagged, bases, (function (i, b) {
256
+ return fromUntagged(i, b, self);
257
+ }));
341
258
  }
342
- exit = 1;
259
+ throw {
260
+ RE_EXN_ID: $$TypeError,
261
+ _1: "Expecting " + lenbases.toString() + " items, got " + lenitems.toString(),
262
+ Error: new Error()
263
+ };
343
264
  }
344
265
  break;
345
266
  case "Object" :
346
- if (typeof match !== "object") {
347
- exit = 1;
348
- } else {
349
- if (match.TAG === "JSONObject") {
350
- var values = match._0;
351
- return Object.fromEntries(shape._0.map((function(values){
352
- return function (param) {
353
- var field = param[0];
354
- var value;
355
- try {
356
- value = extractValue(values, field, param[1], self);
357
- }
358
- catch (raw_msg){
359
- var msg = Caml_js_exceptions.internalToOCamlException(raw_msg);
360
- if (msg.RE_EXN_ID === $$TypeError) {
361
- throw {
362
- RE_EXN_ID: $$TypeError,
363
- _1: "Field \"" + field + "\": " + msg._1,
364
- Error: new Error()
365
- };
366
- }
367
- throw msg;
267
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && typeof untagged === "object" && !Array.isArray(untagged)) {
268
+ return Object.fromEntries(shape._0.map(function (param) {
269
+ var field = param[0];
270
+ var value;
271
+ try {
272
+ value = extractValue(untagged, field, param[1], self);
273
+ }
274
+ catch (raw_msg){
275
+ var msg = Caml_js_exceptions.internalToOCamlException(raw_msg);
276
+ if (msg.RE_EXN_ID === $$TypeError) {
277
+ throw {
278
+ RE_EXN_ID: $$TypeError,
279
+ _1: "Field \"" + field + "\": " + msg._1,
280
+ Error: new Error()
281
+ };
368
282
  }
369
- return [
370
- field,
371
- value
372
- ];
283
+ throw msg;
373
284
  }
374
- }(values))));
375
- }
376
- exit = 1;
285
+ return [
286
+ field,
287
+ value
288
+ ];
289
+ }));
377
290
  }
378
291
  break;
379
292
  case "Optional" :
380
- var shape$2 = shape._0;
381
- if (typeof match !== "object") {
382
- if (match === "JSONNull") {
383
- return undefined;
384
- }
385
- _shape = shape$2;
386
- continue ;
387
- } else {
388
- _shape = shape$2;
389
- continue ;
293
+ if (!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") {
294
+ return undefined;
390
295
  }
296
+ _shape = shape._0;
297
+ continue ;
391
298
  case "OptionalWithDefault" :
392
- var shape$3 = shape._0;
393
- if (typeof match !== "object") {
394
- if (match === "JSONNull") {
395
- return shape._1;
396
- }
397
- _shape = shape$3;
398
- continue ;
399
- } else {
400
- _shape = shape$3;
401
- continue ;
299
+ if (!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") {
300
+ return shape._1;
402
301
  }
302
+ _shape = shape._0;
303
+ continue ;
403
304
  case "Mapping" :
404
- if (typeof match !== "object") {
405
- exit = 1;
406
- } else {
407
- if (match.TAG === "JSONObject") {
408
- var f$1 = shape._0;
409
- return Core__Dict.mapValues(match._0, (function(f$1){
410
- return function (v) {
411
- return fromUntagged(v, f$1, self);
412
- }
413
- }(f$1)));
414
- }
415
- exit = 1;
305
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && typeof untagged === "object" && !Array.isArray(untagged)) {
306
+ var f = shape._0;
307
+ return Core__Dict.mapValues(untagged, (function(f){
308
+ return function (v) {
309
+ return fromUntagged(v, f, self);
310
+ }
311
+ }(f)));
416
312
  }
417
313
  break;
418
314
  case "Deserializer" :
@@ -426,20 +322,15 @@ function fromUntagged(untagged, _shape, self) {
426
322
  Error: new Error()
427
323
  };
428
324
  case "Collection" :
429
- if (typeof match !== "object") {
430
- exit = 1;
431
- } else {
432
- if (match.TAG === "JSONArray") {
433
- return Core__Array.filterMap(match._0.map(shape._0.fromJSON), (function (x) {
434
- if (x.TAG === "Ok") {
435
- return Caml_option.some(x._0);
436
- }
437
- console.warn("Deser", "Could not deserialize value in the collection", x._0);
438
- })).map(function (prim) {
439
- return prim;
440
- });
441
- }
442
- exit = 1;
325
+ if (!(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean") && Array.isArray(untagged)) {
326
+ return Core__Array.filterMap(untagged.map(shape._0.fromJSON), (function (x) {
327
+ if (x.TAG === "Ok") {
328
+ return Caml_option.some(x._0);
329
+ }
330
+ console.warn("Deser", "Could not deserialize value in the collection", x._0);
331
+ })).map(function (prim) {
332
+ return prim;
333
+ });
443
334
  }
444
335
  break;
445
336
  case "DefaultWhenInvalid" :
@@ -459,35 +350,37 @@ function fromUntagged(untagged, _shape, self) {
459
350
 
460
351
  }
461
352
  }
462
- switch (exit) {
463
- case 1 :
464
- throw {
465
- RE_EXN_ID: $$TypeError,
466
- _1: "Expected " + toString(shape) + ", but got " + _taggedToString(match) + " instead",
467
- Error: new Error()
468
- };
469
- case 2 :
470
- var r = new Date(s);
471
- if (Number.isNaN(r.getDate())) {
472
- throw {
473
- RE_EXN_ID: $$TypeError,
474
- _1: "Invalid date " + s,
475
- Error: new Error()
476
- };
477
- }
478
- return r;
479
- case 3 :
480
- var r$1 = new Date(f);
481
- if (Number.isNaN(r$1.getDate())) {
482
- throw {
483
- RE_EXN_ID: $$TypeError,
484
- _1: "Invalid date " + f.toString(),
485
- Error: new Error()
486
- };
487
- }
488
- return r$1;
489
-
353
+ if (exit === 2 && !(!Array.isArray(untagged) && (untagged === null || typeof untagged !== "object") && typeof untagged !== "number" && typeof untagged !== "string" && typeof untagged !== "boolean")) {
354
+ switch (typeof untagged) {
355
+ case "string" :
356
+ var r = new Date(untagged);
357
+ if (isNaN(r.getDate())) {
358
+ throw {
359
+ RE_EXN_ID: $$TypeError,
360
+ _1: "Invalid date " + untagged,
361
+ Error: new Error()
362
+ };
363
+ }
364
+ return r;
365
+ case "number" :
366
+ var r$1 = new Date(untagged);
367
+ if (isNaN(r$1.getDate())) {
368
+ throw {
369
+ RE_EXN_ID: $$TypeError,
370
+ _1: "Invalid date " + untagged.toString(),
371
+ Error: new Error()
372
+ };
373
+ }
374
+ return r$1;
375
+ default:
376
+
377
+ }
490
378
  }
379
+ throw {
380
+ RE_EXN_ID: $$TypeError,
381
+ _1: "Expected " + toString(shape) + ", but got " + _taggedToString(untagged) + " instead",
382
+ Error: new Error()
383
+ };
491
384
  };
492
385
  }
493
386
 
@@ -620,7 +513,7 @@ var Field = {
620
513
 
621
514
  function MakeDeserializer(S) {
622
515
  var fields = S.fields;
623
- var name = "Deserializer " + "Deser" + ", " + "File \"Deser.res\", line 329, characters 39-47";
516
+ var name = "Deserializer " + "Deser" + ", " + "File \"Deser.res\", line 328, characters 39-47";
624
517
  var checkFieldsSanity$1 = function () {
625
518
  return checkFieldsSanity(name, fields, false);
626
519
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaiko.io/rescript-deser",
3
- "version": "6.0.1",
3
+ "version": "7.0.0-alpha.1",
4
4
  "keywords": [
5
5
  "json",
6
6
  "deserializer",
@@ -25,6 +25,7 @@
25
25
  "devDependencies": {
26
26
  "@babel/code-frame": "7.18.6",
27
27
  "@jihchi/vite-plugin-rescript": "^6.0",
28
+ "@playwright/test": "1.56.1",
28
29
  "@rescript/core": "^1.6.0",
29
30
  "@rescript/tools": "0.6.4",
30
31
  "qunit": "^2.16.0",