mol_schema 0.0.22 → 0.0.24

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/node.js CHANGED
@@ -81,34 +81,36 @@ var $;
81
81
  static toString() {
82
82
  return $$.$mol_func_name(this);
83
83
  }
84
- /** Type-guard that checks value by schema. */
85
- static check(val) {
84
+ /** Type-predicate that checks value by schema. */
85
+ static check(value) {
86
86
  try {
87
- this.guard(val);
87
+ this.guard(value);
88
88
  return true;
89
89
  }
90
90
  catch (error) {
91
91
  return false;
92
92
  }
93
93
  }
94
- static [Symbol.hasInstance](val) {
95
- return this.check(val);
94
+ /** `instanceof` support */
95
+ static [Symbol.hasInstance](value) {
96
+ return this.check(value);
96
97
  }
97
- /** Strict parse. Fails of wrong values. */
98
+ /** Type-parser that fails of wrong values. */
98
99
  static guard(value) {
99
100
  return value;
100
101
  }
101
- /** Relaxed cast. Normalizes wrong values. */
102
+ /** Type-caster that normalizes wrong values. */
102
103
  static cast(value) {
103
104
  try {
104
- return this.guard(value);
105
+ this.guard(value);
106
+ return value;
105
107
  }
106
108
  catch (error) {
107
109
  return this.default;
108
110
  }
109
111
  }
110
112
  /** Default value which conforms schema. */
111
- static default = undefined;
113
+ static default = null;
112
114
  }
113
115
  $.$mol_schema_any = $mol_schema_any;
114
116
  })($ || ($ = {}));
@@ -210,6 +212,241 @@ var $;
210
212
  $.$mol_schema_string = $mol_schema_string;
211
213
  })($ || ($ = {}));
212
214
 
215
+ ;
216
+ "use strict";
217
+ var $;
218
+ (function ($) {
219
+ $.$mol_ambient_ref = Symbol('$mol_ambient_ref');
220
+ function $mol_ambient(overrides) {
221
+ return Object.setPrototypeOf(overrides, this || $);
222
+ }
223
+ $.$mol_ambient = $mol_ambient;
224
+ })($ || ($ = {}));
225
+
226
+ ;
227
+ "use strict";
228
+ var $;
229
+ (function ($) {
230
+ const instances = new WeakSet();
231
+ /**
232
+ * Proxy that delegates all to lazy returned target.
233
+ *
234
+ * $mol_delegate( Array.prototype , ()=> fetch_array() )
235
+ */
236
+ function $mol_delegate(proto, target) {
237
+ const proxy = new Proxy(proto, {
238
+ get: (_, field) => {
239
+ const obj = target();
240
+ let val = Reflect.get(obj, field);
241
+ if (typeof val === 'function') {
242
+ val = val.bind(obj);
243
+ }
244
+ return val;
245
+ },
246
+ has: (_, field) => Reflect.has(target(), field),
247
+ set: (_, field, value) => Reflect.set(target(), field, value),
248
+ getOwnPropertyDescriptor: (_, field) => Reflect.getOwnPropertyDescriptor(target(), field),
249
+ ownKeys: () => Reflect.ownKeys(target()),
250
+ getPrototypeOf: () => Reflect.getPrototypeOf(target()),
251
+ setPrototypeOf: (_, donor) => Reflect.setPrototypeOf(target(), donor),
252
+ isExtensible: () => Reflect.isExtensible(target()),
253
+ preventExtensions: () => Reflect.preventExtensions(target()),
254
+ apply: (_, self, args) => Reflect.apply(target(), self, args),
255
+ construct: (_, args, retarget) => Reflect.construct(target(), args, retarget),
256
+ defineProperty: (_, field, descr) => Reflect.defineProperty(target(), field, descr),
257
+ deleteProperty: (_, field) => Reflect.deleteProperty(target(), field),
258
+ });
259
+ instances.add(proxy);
260
+ return proxy;
261
+ }
262
+ $.$mol_delegate = $mol_delegate;
263
+ Reflect.defineProperty($mol_delegate, Symbol.hasInstance, {
264
+ value: (obj) => instances.has(obj),
265
+ });
266
+ })($ || ($ = {}));
267
+
268
+ ;
269
+ "use strict";
270
+ var $;
271
+ (function ($) {
272
+ $.$mol_owning_map = new WeakMap();
273
+ function $mol_owning_allow(having) {
274
+ try {
275
+ if (!having)
276
+ return false;
277
+ if (typeof having !== 'object' && typeof having !== 'function')
278
+ return false;
279
+ if (having instanceof $mol_delegate)
280
+ return false;
281
+ if (typeof having['destructor'] !== 'function')
282
+ return false;
283
+ return true;
284
+ }
285
+ catch {
286
+ return false;
287
+ }
288
+ }
289
+ $.$mol_owning_allow = $mol_owning_allow;
290
+ function $mol_owning_get(having, Owner) {
291
+ if (!$mol_owning_allow(having))
292
+ return null;
293
+ while (true) {
294
+ const owner = $.$mol_owning_map.get(having);
295
+ if (!owner)
296
+ return owner;
297
+ if (!Owner)
298
+ return owner;
299
+ if (owner instanceof Owner)
300
+ return owner;
301
+ having = owner;
302
+ }
303
+ }
304
+ $.$mol_owning_get = $mol_owning_get;
305
+ function $mol_owning_check(owner, having) {
306
+ if (!$mol_owning_allow(having))
307
+ return false;
308
+ if ($.$mol_owning_map.get(having) !== owner)
309
+ return false;
310
+ return true;
311
+ }
312
+ $.$mol_owning_check = $mol_owning_check;
313
+ function $mol_owning_catch(owner, having) {
314
+ if (!$mol_owning_allow(having))
315
+ return false;
316
+ if ($.$mol_owning_map.get(having))
317
+ return false;
318
+ $.$mol_owning_map.set(having, owner);
319
+ return true;
320
+ }
321
+ $.$mol_owning_catch = $mol_owning_catch;
322
+ })($ || ($ = {}));
323
+
324
+ ;
325
+ "use strict";
326
+ var $;
327
+ (function ($) {
328
+ function $mol_fail_hidden(error) {
329
+ throw error; /// Use 'Never Pause Here' breakpoint in DevTools or simply blackbox this script
330
+ }
331
+ $.$mol_fail_hidden = $mol_fail_hidden;
332
+ })($ || ($ = {}));
333
+
334
+ ;
335
+ "use strict";
336
+
337
+ ;
338
+ "use strict";
339
+ var $;
340
+ (function ($) {
341
+ if (!Symbol.dispose)
342
+ Symbol.dispose = Symbol('Symbol.dispose');
343
+ class $mol_object2 {
344
+ static $ = $;
345
+ [Symbol.toStringTag];
346
+ [$mol_ambient_ref] = null;
347
+ get $() {
348
+ if (this[$mol_ambient_ref])
349
+ return this[$mol_ambient_ref];
350
+ const owner = $mol_owning_get(this);
351
+ return this[$mol_ambient_ref] = owner?.$ || this.constructor.$ || $mol_object2.$;
352
+ }
353
+ set $(next) {
354
+ if (this[$mol_ambient_ref])
355
+ $mol_fail_hidden(new Error('Context already defined'));
356
+ this[$mol_ambient_ref] = next;
357
+ }
358
+ static create(init) {
359
+ const obj = new this;
360
+ if (init)
361
+ init(obj);
362
+ return obj;
363
+ }
364
+ static [Symbol.toPrimitive]() {
365
+ return this.toString();
366
+ }
367
+ static toString() {
368
+ return this[Symbol.toStringTag] || this.$.$mol_func_name(this);
369
+ }
370
+ static toJSON() {
371
+ return this.toString();
372
+ }
373
+ static [$mol_key_handle]() {
374
+ return this.toString();
375
+ }
376
+ destructor() { }
377
+ static destructor() { }
378
+ [Symbol.dispose]() {
379
+ this.destructor();
380
+ }
381
+ //[ Symbol.toPrimitive ]( hint: string ) {
382
+ // return hint === 'number' ? this.valueOf() : this.toString()
383
+ //}
384
+ toString() {
385
+ return this[Symbol.toStringTag] || this.constructor.name + '<>';
386
+ }
387
+ }
388
+ $.$mol_object2 = $mol_object2;
389
+ })($ || ($ = {}));
390
+
391
+ ;
392
+ "use strict";
393
+ var $;
394
+ (function ($) {
395
+ class $mol_wrapper extends $mol_object2 {
396
+ static wrap;
397
+ static run(task) {
398
+ return this.func(task)();
399
+ }
400
+ static func(func) {
401
+ return this.wrap(func);
402
+ }
403
+ static get class() {
404
+ return (Class) => {
405
+ const construct = (target, args) => new Class(...args);
406
+ const handler = {
407
+ construct: this.func(construct)
408
+ };
409
+ handler[Symbol.toStringTag] = Class.name + '#';
410
+ return new Proxy(Class, handler);
411
+ };
412
+ }
413
+ static get method() {
414
+ return (obj, name, descr = Reflect.getOwnPropertyDescriptor(obj, name)) => {
415
+ descr.value = this.func(descr.value);
416
+ return descr;
417
+ };
418
+ }
419
+ static get field() {
420
+ return (obj, name, descr = Reflect.getOwnPropertyDescriptor(obj, name)) => {
421
+ descr.get = descr.set = this.func(descr.get);
422
+ return descr;
423
+ };
424
+ }
425
+ }
426
+ $.$mol_wrapper = $mol_wrapper;
427
+ })($ || ($ = {}));
428
+
429
+ ;
430
+ "use strict";
431
+ var $;
432
+ (function ($) {
433
+ class $mol_memo extends $mol_wrapper {
434
+ static wrap(task) {
435
+ const store = new WeakMap();
436
+ const fun = function (next) {
437
+ if (next === undefined && store.has(this ?? fun))
438
+ return store.get(this ?? fun);
439
+ const val = task.call(this, next) ?? next;
440
+ store.set(this ?? fun, val);
441
+ return val;
442
+ };
443
+ Reflect.defineProperty(fun, 'name', { value: task.name + ' ' });
444
+ return fun;
445
+ }
446
+ }
447
+ $.$mol_memo = $mol_memo;
448
+ })($ || ($ = {}));
449
+
213
450
  ;
214
451
  "use strict";
215
452
  var $;
@@ -283,7 +520,32 @@ var $;
283
520
  "use strict";
284
521
  var $;
285
522
  (function ($) {
286
- function $mol_schema_pattern(Pattern) {
523
+ class $mol_memo_key extends $mol_wrapper {
524
+ static wrap(task) {
525
+ const store = new WeakMap();
526
+ const fun = function (key, next) {
527
+ let store2 = store.get(this ?? fun);
528
+ if (!store2)
529
+ store.set(this ?? fun, store2 = new Map);
530
+ const key_str = $mol_key(key);
531
+ if (next === undefined && store2.has(key_str))
532
+ return store2.get(key_str);
533
+ const val = task.call(this, key, next) ?? next;
534
+ store2.set(key_str, val);
535
+ return val;
536
+ };
537
+ Reflect.defineProperty(fun, 'name', { value: task.name + ' ' });
538
+ return fun;
539
+ }
540
+ }
541
+ $.$mol_memo_key = $mol_memo_key;
542
+ })($ || ($ = {}));
543
+
544
+ ;
545
+ "use strict";
546
+ var $;
547
+ (function ($) {
548
+ $.$mol_schema_pattern = $mol_memo_key.func(function $mol_schema_pattern(Pattern) {
287
549
  return class $mol_schema_pattern_ extends $mol_schema_string {
288
550
  static Pattern = Pattern;
289
551
  static toString() {
@@ -301,15 +563,14 @@ var $;
301
563
  }
302
564
  static default = '';
303
565
  };
304
- }
305
- $.$mol_schema_pattern = $mol_schema_pattern;
566
+ });
306
567
  })($ || ($ = {}));
307
568
 
308
569
  ;
309
570
  "use strict";
310
571
  var $;
311
572
  (function ($) {
312
- function $mol_schema_instance(Class, ...args) {
573
+ $.$mol_schema_instance = $mol_memo_key.func(function $mol_schema_instance(Class) {
313
574
  class $mol_schema_instance_ extends $mol_schema_any {
314
575
  static Class = Class;
315
576
  static toString() {
@@ -322,108 +583,129 @@ var $;
322
583
  return value;
323
584
  return $mol_fail(new TypeError('Wrong class', { cause: { value, schema: this } }));
324
585
  }
325
- static default = new Class(...args);
586
+ static cast(value) {
587
+ return this.guard(value);
588
+ }
589
+ static default;
326
590
  }
327
- return ((Class[Symbol.hasInstance] === $mol_schema_any[Symbol.hasInstance])
591
+ return ((Class?.[Symbol.hasInstance] === $mol_schema_any[Symbol.hasInstance])
328
592
  ? Class
329
593
  : $mol_schema_instance_);
330
- }
331
- $.$mol_schema_instance = $mol_schema_instance;
594
+ });
332
595
  })($ || ($ = {}));
333
596
 
334
597
  ;
335
598
  "use strict";
599
+ var $;
600
+ (function ($) {
601
+ function $mol_schema_lazy(Schema) {
602
+ // return $mol_delegate( $mol_schema_any, Schema )
603
+ return class $mol_schema_lazy_ extends $mol_schema_any {
604
+ static Schema = $mol_memo.func(Schema);
605
+ static guard(value) {
606
+ return this.Schema().guard(value);
607
+ }
608
+ static cast(value) {
609
+ return this.Schema().cast(value);
610
+ }
611
+ static get default() {
612
+ return this.Schema().default;
613
+ }
614
+ };
615
+ }
616
+ $.$mol_schema_lazy = $mol_schema_lazy;
617
+ })($ || ($ = {}));
336
618
 
337
619
  ;
338
620
  "use strict";
339
621
  var $;
340
622
  (function ($) {
341
- function $mol_schema_every(Schemas) {
342
- return class $mol_schema_every_ extends $mol_schema_any {
343
- static Schemas = Schemas;
623
+ $.$mol_schema_some = $mol_memo_key.func(function $mol_schema_some(Variants) {
624
+ return class $mol_schema_some_ extends $mol_schema_any {
625
+ static Variants = Variants;
344
626
  static toString() {
345
- if (this !== $mol_schema_every_)
627
+ if (this !== $mol_schema_some_)
346
628
  return super.toString();
347
- return '$mol_schema_every<' + $mol_key(Schemas) + '>';
629
+ return '$mol_schema_some<' + $mol_key(Variants) + '>';
348
630
  }
349
631
  static guard(value) {
350
- for (const Schema of Schemas) {
351
- Schema.guard(value);
632
+ const errors = [];
633
+ for (const Variant of Variants) {
634
+ try {
635
+ return Variant.guard(value);
636
+ }
637
+ catch (error) {
638
+ errors.push(error);
639
+ }
352
640
  }
353
- return value;
641
+ return $mol_fail(new AggregateError(errors, 'Wrong variant', { cause: { value, schema: this } }));
354
642
  }
355
643
  static cast(value) {
356
- for (const Scheme of Schemas)
357
- value = Scheme.cast(value);
358
- return value;
644
+ try {
645
+ return this.guard(value);
646
+ }
647
+ catch (error) {
648
+ return Variants[0].cast(value);
649
+ }
359
650
  }
360
- static default = Schemas.find(Scheme => this.check(Scheme.default));
651
+ static default = Variants[0].default;
361
652
  };
362
- }
363
- $.$mol_schema_every = $mol_schema_every;
653
+ });
364
654
  })($ || ($ = {}));
365
655
 
366
656
  ;
367
657
  "use strict";
368
658
  var $;
369
659
  (function ($) {
370
- function $mol_schema_range(Min, Max) {
371
- return class $mol_schema_range_ extends $mol_schema_any {
372
- static Min = Min;
373
- static Max = Max;
660
+ $.$mol_schema_dict = $mol_memo_key.func(function $mol_schema_dict(Pair) {
661
+ return class $mol_schema_dict_ extends $mol_schema_any {
662
+ static Pair = Pair;
374
663
  static toString() {
375
- if (this !== $mol_schema_range_)
664
+ if (this !== $mol_schema_dict_)
376
665
  return super.toString();
377
- return '$mol_schema_range<' + $mol_key(Min) + ',' + $mol_key(Max) + '>';
666
+ return '$mol_schema_dict<' + $mol_key(Pair) + '>';
378
667
  }
379
668
  static guard(value) {
380
- if (typeof value !== 'number' && typeof value !== 'bigint')
381
- return $mol_fail(new TypeError('Uncomparable type', { cause: { value, schema: this } }));
382
- if (!(value <= Max))
383
- return $mol_fail(new TypeError('Too large', { cause: { value, schema: this } }));
384
- if (!(value >= Min))
385
- return $mol_fail(new TypeError('Too small', { cause: { value, schema: this } }));
669
+ if (Object.getPrototypeOf(Object.getPrototypeOf(value))) {
670
+ return $mol_fail(new TypeError('Non dictionary', { cause: { value, schema: this } }));
671
+ }
672
+ for (const key in value) {
673
+ try {
674
+ Pair[0].guard(key);
675
+ }
676
+ catch (error) {
677
+ return $mol_fail(new TypeError('Wrong key', { cause: { key, error, value, schema: this } }));
678
+ }
679
+ try {
680
+ Pair[1].guard(value[key]);
681
+ }
682
+ catch (error) {
683
+ return $mol_fail(new TypeError('Wrong val', { cause: { key, error, value, schema: this } }));
684
+ }
685
+ }
386
686
  return value;
387
687
  }
388
- static cast(val) {
389
- if (val > Max)
390
- return Max;
391
- if (val >= Min)
392
- return val;
393
- return Min;
688
+ static cast(value) {
689
+ if (Object.getPrototypeOf(Object.getPrototypeOf(value)))
690
+ return this.default;
691
+ const res = {};
692
+ for (const key in value) {
693
+ if (!Pair[0].check(key))
694
+ continue;
695
+ res[key] = Pair[1].cast(value[key]);
696
+ }
697
+ return res;
394
698
  }
395
- static default = Min;
699
+ static default = {};
396
700
  };
397
- }
398
- $.$mol_schema_range = $mol_schema_range;
701
+ });
399
702
  })($ || ($ = {}));
400
703
 
401
704
  ;
402
705
  "use strict";
403
706
  var $;
404
707
  (function ($) {
405
- class $mol_schema_positive extends $mol_schema_range(0, Number.POSITIVE_INFINITY) {
406
- }
407
- $.$mol_schema_positive = $mol_schema_positive;
408
- })($ || ($ = {}));
409
-
410
- ;
411
- "use strict";
412
- var $;
413
- (function ($) {
414
- class $mol_schema_natural extends $mol_schema_every([
415
- $mol_schema_integer,
416
- $mol_schema_positive,
417
- ]) {
418
- }
419
- $.$mol_schema_natural = $mol_schema_natural;
420
- })($ || ($ = {}));
421
-
422
- ;
423
- "use strict";
424
- var $;
425
- (function ($) {
426
- function $mol_schema_enum(Options) {
708
+ $.$mol_schema_enum = $mol_memo_key.func(function $mol_schema_enum(Options) {
427
709
  return class $mol_schema_enum_ extends $mol_schema_any {
428
710
  static Options = Options;
429
711
  static toString() {
@@ -434,174 +716,188 @@ var $;
434
716
  static guard(value) {
435
717
  if (Options.some(Option => Object.is(Option, value)))
436
718
  return value;
437
- return $mol_fail(new TypeError('No one option', { cause: { value, schema: this } }));
719
+ return $mol_fail(new TypeError('Wrong option', { cause: { value, schema: this } }));
438
720
  }
439
- static cast(val) {
440
- if (this.check(val))
441
- return val;
721
+ static cast(value) {
722
+ if (this.check(value))
723
+ return value;
442
724
  return Options[0];
443
725
  }
444
726
  static default = Options[0];
445
727
  };
446
- }
447
- $.$mol_schema_enum = $mol_schema_enum;
448
- })($ || ($ = {}));
449
-
450
- ;
451
- "use strict";
452
- var $;
453
- (function ($) {
454
- class $mol_schema_negative extends $mol_schema_range(Number.NEGATIVE_INFINITY, 0) {
455
- }
456
- $.$mol_schema_negative = $mol_schema_negative;
728
+ });
457
729
  })($ || ($ = {}));
458
730
 
459
731
  ;
460
732
  "use strict";
461
733
  var $;
462
734
  (function ($) {
463
- function $mol_schema_some(Variants) {
464
- return class $mol_schema_some_ extends $mol_schema_any {
465
- static Variants = Variants;
735
+ $.$mol_schema_list = $mol_memo_key.func(function $mol_schema_list(Item) {
736
+ return class $mol_schema_list_ extends $mol_schema_any {
737
+ static Item = Item;
466
738
  static toString() {
467
- if (this !== $mol_schema_some_)
739
+ if (this !== $mol_schema_list_)
468
740
  return super.toString();
469
- return '$mol_schema_some<' + $mol_key(Variants) + '>';
741
+ return '$mol_schema_list<' + $mol_key(Item) + '>';
470
742
  }
471
743
  static guard(value) {
472
- const errors = [];
473
- for (const Variant of Variants) {
744
+ if (!Array.isArray(value))
745
+ return $mol_fail(new TypeError('Non array', { cause: { value, schema: this } }));
746
+ for (const [index, item] of super.guard(value).entries()) {
474
747
  try {
475
- return Variant.guard(value);
748
+ Item.guard(item);
476
749
  }
477
750
  catch (error) {
478
- errors.push(error);
751
+ return $mol_fail(new TypeError('Wrong item', { cause: { index, error, value, schema: this } }));
479
752
  }
480
753
  }
481
- return $mol_fail(new AggregateError(errors, 'No one variant', { cause: { value, schema: this } }));
754
+ return value;
482
755
  }
483
756
  static cast(value) {
484
- try {
485
- return this.guard(value);
486
- }
487
- catch (error) {
488
- return Variants[0].cast(value);
489
- }
757
+ if (!Array.isArray(value))
758
+ return this.default;
759
+ return value.map(item => Item.cast(item));
490
760
  }
491
- static default = Variants[0].default;
761
+ static default = [];
492
762
  };
493
- }
494
- $.$mol_schema_some = $mol_schema_some;
763
+ });
495
764
  })($ || ($ = {}));
496
765
 
497
766
  ;
498
767
  "use strict";
499
768
  var $;
500
769
  (function ($) {
501
- function $mol_schema_maybe(Some) {
502
- return class $mol_schema_maybe_ extends $mol_schema_some([$mol_schema_enum([undefined, null]), Some]) {
503
- static Some = Some;
504
- static toString() {
505
- if (this !== $mol_schema_maybe_)
506
- return super.toString();
507
- return '$mol_schema_maybe<' + $mol_key(Some) + '>';
508
- }
509
- };
770
+ class $mol_schema_json extends $mol_schema_lazy(() => $mol_schema_some([
771
+ $mol_schema_dict([$mol_schema_string, $mol_schema_json]),
772
+ $mol_schema_enum([null]),
773
+ $mol_schema_boolean, $mol_schema_float, $mol_schema_string,
774
+ $mol_schema_list($mol_schema_json),
775
+ ])) {
510
776
  }
511
- $.$mol_schema_maybe = $mol_schema_maybe;
777
+ $.$mol_schema_json = $mol_schema_json;
512
778
  })($ || ($ = {}));
513
779
 
780
+ ;
781
+ "use strict";
782
+
514
783
  ;
515
784
  "use strict";
516
785
  var $;
517
786
  (function ($) {
518
- function $mol_schema_list(Item) {
519
- return class $mol_schema_list_ extends $mol_schema_any {
520
- static Item = Item;
787
+ $.$mol_schema_every = $mol_memo_key.func(function $mol_schema_every(Schemas) {
788
+ return class $mol_schema_every_ extends $mol_schema_any {
789
+ static Schemas = Schemas;
521
790
  static toString() {
522
- if (this !== $mol_schema_list_)
791
+ if (this !== $mol_schema_every_)
523
792
  return super.toString();
524
- return '$mol_schema_list<' + $mol_key(Item) + '>';
793
+ return '$mol_schema_every<' + $mol_key(Schemas) + '>';
525
794
  }
526
795
  static guard(value) {
527
- if (!Array.isArray(value))
528
- return $mol_fail(new TypeError('Non array', { cause: { value, schema: this } }));
529
- for (const [index, item] of super.guard(value).entries()) {
530
- try {
531
- Item.guard(item);
532
- }
533
- catch (error) {
534
- return $mol_fail(new TypeError('Wrong item', { cause: { index, error, value, schema: this } }));
535
- }
796
+ for (const Schema of Schemas) {
797
+ Schema.guard(value);
536
798
  }
537
799
  return value;
538
800
  }
539
801
  static cast(value) {
540
- if (!Array.isArray(value))
541
- return this.default;
542
- return value.map(item => Item.cast(item));
802
+ for (const Scheme of Schemas)
803
+ value = Scheme.cast(value);
804
+ return value;
543
805
  }
544
- static default = [];
806
+ static default = Schemas.find(Scheme => this.check(Scheme.default));
545
807
  };
546
- }
547
- $.$mol_schema_list = $mol_schema_list;
808
+ });
548
809
  })($ || ($ = {}));
549
810
 
550
811
  ;
551
812
  "use strict";
552
813
  var $;
553
814
  (function ($) {
554
- function $mol_schema_dict(Key, Val) {
555
- return class $mol_schema_dict_ extends $mol_schema_any {
556
- static Key = Key;
557
- static Val = Val;
815
+ $.$mol_schema_range = $mol_memo_key.func(function $mol_schema_range(Range) {
816
+ return class $mol_schema_range_ extends $mol_schema_any {
817
+ static Range = Range;
558
818
  static toString() {
559
- if (this !== $mol_schema_dict_)
819
+ if (this !== $mol_schema_range_)
560
820
  return super.toString();
561
- return '$mol_schema_dict<' + $mol_key(Key) + ',' + $mol_key(Key) + '>';
821
+ return '$mol_schema_range<' + $mol_key(Range) + '>';
562
822
  }
563
823
  static guard(value) {
564
- if (Object.getPrototypeOf(Object.getPrototypeOf(value))) {
565
- return $mol_fail(new TypeError('Non dictionary', { cause: { value, schema: this } }));
566
- }
567
- for (const key in value) {
568
- try {
569
- Key.guard(key);
570
- }
571
- catch (error) {
572
- return $mol_fail(new TypeError('Wrong key', { cause: { key, error, value, schema: this } }));
573
- }
574
- try {
575
- Val.guard(value[key]);
576
- }
577
- catch (error) {
578
- return $mol_fail(new TypeError('Wrong val', { cause: { key, error, value, schema: this } }));
579
- }
580
- }
824
+ if (typeof value !== 'number' && typeof value !== 'bigint')
825
+ return $mol_fail(new TypeError('Uncomparable type', { cause: { value, schema: this } }));
826
+ if (!(value <= Range[1]))
827
+ return $mol_fail(new TypeError('Too large', { cause: { value, schema: this } }));
828
+ if (!(value >= Range[0]))
829
+ return $mol_fail(new TypeError('Too small', { cause: { value, schema: this } }));
581
830
  return value;
582
831
  }
583
832
  static cast(value) {
584
- if (Object.getPrototypeOf(Object.getPrototypeOf(value)))
585
- return this.default;
586
- const res = {};
587
- for (const key in value) {
588
- if (!Key.check(key))
589
- continue;
590
- res[key] = Val.cast(value[key]);
591
- }
592
- return res;
833
+ if (value > Range[1])
834
+ return Range[1];
835
+ if (value >= Range[0])
836
+ return value;
837
+ return Range[0];
593
838
  }
594
- static default = {};
839
+ static default = Range[0];
595
840
  };
841
+ });
842
+ })($ || ($ = {}));
843
+
844
+ ;
845
+ "use strict";
846
+ var $;
847
+ (function ($) {
848
+ class $mol_schema_positive extends $mol_schema_range([0, Number.POSITIVE_INFINITY]) {
849
+ }
850
+ $.$mol_schema_positive = $mol_schema_positive;
851
+ })($ || ($ = {}));
852
+
853
+ ;
854
+ "use strict";
855
+ var $;
856
+ (function ($) {
857
+ class $mol_schema_natural extends $mol_schema_every([
858
+ $mol_schema_integer,
859
+ $mol_schema_positive,
860
+ ]) {
596
861
  }
597
- $.$mol_schema_dict = $mol_schema_dict;
862
+ $.$mol_schema_natural = $mol_schema_natural;
598
863
  })($ || ($ = {}));
599
864
 
600
865
  ;
601
866
  "use strict";
602
867
  var $;
603
868
  (function ($) {
604
- function $mol_schema_record(Fields) {
869
+ class $mol_schema_negative extends $mol_schema_range([Number.NEGATIVE_INFINITY, 0]) {
870
+ }
871
+ $.$mol_schema_negative = $mol_schema_negative;
872
+ })($ || ($ = {}));
873
+
874
+ ;
875
+ "use strict";
876
+ var $;
877
+ (function ($) {
878
+ $.$mol_schema_maybe = $mol_memo_key.func(function $mol_schema_maybe(Some) {
879
+ return class $mol_schema_maybe_ extends $mol_schema_any {
880
+ static Some = Some;
881
+ static toString() {
882
+ if (this !== $mol_schema_maybe_)
883
+ return super.toString();
884
+ return '$mol_schema_maybe<' + $mol_key(Some) + '>';
885
+ }
886
+ static guard(value) {
887
+ if (value == null)
888
+ return value;
889
+ return Some.guard(value);
890
+ }
891
+ static default = null;
892
+ };
893
+ });
894
+ })($ || ($ = {}));
895
+
896
+ ;
897
+ "use strict";
898
+ var $;
899
+ (function ($) {
900
+ $.$mol_schema_record = $mol_memo_key.func(function $mol_schema_record(Fields) {
605
901
  return class $mol_schema_record_ extends $mol_schema_any {
606
902
  static Fields = Fields;
607
903
  static toString() {
@@ -633,8 +929,7 @@ var $;
633
929
  }
634
930
  static default = Object.fromEntries(Object.entries(Fields).map(([field, Field]) => [field, Field.default]));
635
931
  };
636
- }
637
- $.$mol_schema_record = $mol_schema_record;
932
+ });
638
933
  })($ || ($ = {}));
639
934
 
640
935
  ;