mol_schema 0.0.21 → 0.0.23

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
@@ -91,6 +91,9 @@ var $;
91
91
  return false;
92
92
  }
93
93
  }
94
+ static [Symbol.hasInstance](val) {
95
+ return this.check(val);
96
+ }
94
97
  /** Strict parse. Fails of wrong values. */
95
98
  static guard(value) {
96
99
  return value;
@@ -120,6 +123,60 @@ var $;
120
123
  $.$mol_fail = $mol_fail;
121
124
  })($ || ($ = {}));
122
125
 
126
+ ;
127
+ "use strict";
128
+ var $;
129
+ (function ($) {
130
+ class $mol_schema_float extends $mol_schema_any {
131
+ static guard(value) {
132
+ if (typeof value === 'number')
133
+ return value;
134
+ return $mol_fail(new TypeError('Wrong type', { cause: { value, schema: this } }));
135
+ }
136
+ static default = Number.NaN;
137
+ }
138
+ $.$mol_schema_float = $mol_schema_float;
139
+ })($ || ($ = {}));
140
+
141
+ ;
142
+ "use strict";
143
+ var $;
144
+ (function ($) {
145
+ class $mol_schema_integer extends $mol_schema_float {
146
+ $mol_schema_integer = true;
147
+ static guard(value) {
148
+ const val = super.guard(value);
149
+ if (!Number.isFinite(val))
150
+ return $mol_fail(new TypeError('Non finite', { cause: { value, schema: this } }));
151
+ if (Math.trunc(val) !== val)
152
+ return $mol_fail(new TypeError('Non integer', { cause: { value, schema: this } }));
153
+ return val;
154
+ }
155
+ static default = 0;
156
+ }
157
+ $.$mol_schema_integer = $mol_schema_integer;
158
+ })($ || ($ = {}));
159
+
160
+ ;
161
+ "use strict";
162
+ var $;
163
+ (function ($) {
164
+ class $mol_schema_bigint extends $mol_schema_any {
165
+ static guard(value) {
166
+ if (typeof value === 'bigint')
167
+ return value;
168
+ return $mol_fail(new TypeError('Wrong type', { cause: { value, schema: this } }));
169
+ }
170
+ static cast(value) {
171
+ if (typeof value === 'number')
172
+ return BigInt($mol_schema_integer.cast(value));
173
+ return super.cast(value);
174
+ }
175
+ static default = 0n;
176
+ }
177
+ $.$mol_schema_bigint = $mol_schema_bigint;
178
+ })($ || ($ = {}));
179
+
123
180
  ;
124
181
  "use strict";
125
182
  var $;
@@ -153,6 +210,241 @@ var $;
153
210
  $.$mol_schema_string = $mol_schema_string;
154
211
  })($ || ($ = {}));
155
212
 
213
+ ;
214
+ "use strict";
215
+ var $;
216
+ (function ($) {
217
+ $.$mol_ambient_ref = Symbol('$mol_ambient_ref');
218
+ function $mol_ambient(overrides) {
219
+ return Object.setPrototypeOf(overrides, this || $);
220
+ }
221
+ $.$mol_ambient = $mol_ambient;
222
+ })($ || ($ = {}));
223
+
224
+ ;
225
+ "use strict";
226
+ var $;
227
+ (function ($) {
228
+ const instances = new WeakSet();
229
+ /**
230
+ * Proxy that delegates all to lazy returned target.
231
+ *
232
+ * $mol_delegate( Array.prototype , ()=> fetch_array() )
233
+ */
234
+ function $mol_delegate(proto, target) {
235
+ const proxy = new Proxy(proto, {
236
+ get: (_, field) => {
237
+ const obj = target();
238
+ let val = Reflect.get(obj, field);
239
+ if (typeof val === 'function') {
240
+ val = val.bind(obj);
241
+ }
242
+ return val;
243
+ },
244
+ has: (_, field) => Reflect.has(target(), field),
245
+ set: (_, field, value) => Reflect.set(target(), field, value),
246
+ getOwnPropertyDescriptor: (_, field) => Reflect.getOwnPropertyDescriptor(target(), field),
247
+ ownKeys: () => Reflect.ownKeys(target()),
248
+ getPrototypeOf: () => Reflect.getPrototypeOf(target()),
249
+ setPrototypeOf: (_, donor) => Reflect.setPrototypeOf(target(), donor),
250
+ isExtensible: () => Reflect.isExtensible(target()),
251
+ preventExtensions: () => Reflect.preventExtensions(target()),
252
+ apply: (_, self, args) => Reflect.apply(target(), self, args),
253
+ construct: (_, args, retarget) => Reflect.construct(target(), args, retarget),
254
+ defineProperty: (_, field, descr) => Reflect.defineProperty(target(), field, descr),
255
+ deleteProperty: (_, field) => Reflect.deleteProperty(target(), field),
256
+ });
257
+ instances.add(proxy);
258
+ return proxy;
259
+ }
260
+ $.$mol_delegate = $mol_delegate;
261
+ Reflect.defineProperty($mol_delegate, Symbol.hasInstance, {
262
+ value: (obj) => instances.has(obj),
263
+ });
264
+ })($ || ($ = {}));
265
+
266
+ ;
267
+ "use strict";
268
+ var $;
269
+ (function ($) {
270
+ $.$mol_owning_map = new WeakMap();
271
+ function $mol_owning_allow(having) {
272
+ try {
273
+ if (!having)
274
+ return false;
275
+ if (typeof having !== 'object' && typeof having !== 'function')
276
+ return false;
277
+ if (having instanceof $mol_delegate)
278
+ return false;
279
+ if (typeof having['destructor'] !== 'function')
280
+ return false;
281
+ return true;
282
+ }
283
+ catch {
284
+ return false;
285
+ }
286
+ }
287
+ $.$mol_owning_allow = $mol_owning_allow;
288
+ function $mol_owning_get(having, Owner) {
289
+ if (!$mol_owning_allow(having))
290
+ return null;
291
+ while (true) {
292
+ const owner = $.$mol_owning_map.get(having);
293
+ if (!owner)
294
+ return owner;
295
+ if (!Owner)
296
+ return owner;
297
+ if (owner instanceof Owner)
298
+ return owner;
299
+ having = owner;
300
+ }
301
+ }
302
+ $.$mol_owning_get = $mol_owning_get;
303
+ function $mol_owning_check(owner, having) {
304
+ if (!$mol_owning_allow(having))
305
+ return false;
306
+ if ($.$mol_owning_map.get(having) !== owner)
307
+ return false;
308
+ return true;
309
+ }
310
+ $.$mol_owning_check = $mol_owning_check;
311
+ function $mol_owning_catch(owner, having) {
312
+ if (!$mol_owning_allow(having))
313
+ return false;
314
+ if ($.$mol_owning_map.get(having))
315
+ return false;
316
+ $.$mol_owning_map.set(having, owner);
317
+ return true;
318
+ }
319
+ $.$mol_owning_catch = $mol_owning_catch;
320
+ })($ || ($ = {}));
321
+
322
+ ;
323
+ "use strict";
324
+ var $;
325
+ (function ($) {
326
+ function $mol_fail_hidden(error) {
327
+ throw error; /// Use 'Never Pause Here' breakpoint in DevTools or simply blackbox this script
328
+ }
329
+ $.$mol_fail_hidden = $mol_fail_hidden;
330
+ })($ || ($ = {}));
331
+
332
+ ;
333
+ "use strict";
334
+
335
+ ;
336
+ "use strict";
337
+ var $;
338
+ (function ($) {
339
+ if (!Symbol.dispose)
340
+ Symbol.dispose = Symbol('Symbol.dispose');
341
+ class $mol_object2 {
342
+ static $ = $;
343
+ [Symbol.toStringTag];
344
+ [$mol_ambient_ref] = null;
345
+ get $() {
346
+ if (this[$mol_ambient_ref])
347
+ return this[$mol_ambient_ref];
348
+ const owner = $mol_owning_get(this);
349
+ return this[$mol_ambient_ref] = owner?.$ || this.constructor.$ || $mol_object2.$;
350
+ }
351
+ set $(next) {
352
+ if (this[$mol_ambient_ref])
353
+ $mol_fail_hidden(new Error('Context already defined'));
354
+ this[$mol_ambient_ref] = next;
355
+ }
356
+ static create(init) {
357
+ const obj = new this;
358
+ if (init)
359
+ init(obj);
360
+ return obj;
361
+ }
362
+ static [Symbol.toPrimitive]() {
363
+ return this.toString();
364
+ }
365
+ static toString() {
366
+ return this[Symbol.toStringTag] || this.$.$mol_func_name(this);
367
+ }
368
+ static toJSON() {
369
+ return this.toString();
370
+ }
371
+ static [$mol_key_handle]() {
372
+ return this.toString();
373
+ }
374
+ destructor() { }
375
+ static destructor() { }
376
+ [Symbol.dispose]() {
377
+ this.destructor();
378
+ }
379
+ //[ Symbol.toPrimitive ]( hint: string ) {
380
+ // return hint === 'number' ? this.valueOf() : this.toString()
381
+ //}
382
+ toString() {
383
+ return this[Symbol.toStringTag] || this.constructor.name + '<>';
384
+ }
385
+ }
386
+ $.$mol_object2 = $mol_object2;
387
+ })($ || ($ = {}));
388
+
389
+ ;
390
+ "use strict";
391
+ var $;
392
+ (function ($) {
393
+ class $mol_wrapper extends $mol_object2 {
394
+ static wrap;
395
+ static run(task) {
396
+ return this.func(task)();
397
+ }
398
+ static func(func) {
399
+ return this.wrap(func);
400
+ }
401
+ static get class() {
402
+ return (Class) => {
403
+ const construct = (target, args) => new Class(...args);
404
+ const handler = {
405
+ construct: this.func(construct)
406
+ };
407
+ handler[Symbol.toStringTag] = Class.name + '#';
408
+ return new Proxy(Class, handler);
409
+ };
410
+ }
411
+ static get method() {
412
+ return (obj, name, descr = Reflect.getOwnPropertyDescriptor(obj, name)) => {
413
+ descr.value = this.func(descr.value);
414
+ return descr;
415
+ };
416
+ }
417
+ static get field() {
418
+ return (obj, name, descr = Reflect.getOwnPropertyDescriptor(obj, name)) => {
419
+ descr.get = descr.set = this.func(descr.get);
420
+ return descr;
421
+ };
422
+ }
423
+ }
424
+ $.$mol_wrapper = $mol_wrapper;
425
+ })($ || ($ = {}));
426
+
427
+ ;
428
+ "use strict";
429
+ var $;
430
+ (function ($) {
431
+ class $mol_memo extends $mol_wrapper {
432
+ static wrap(task) {
433
+ const store = new WeakMap();
434
+ const fun = function (next) {
435
+ if (next === undefined && store.has(this ?? fun))
436
+ return store.get(this ?? fun);
437
+ const val = task.call(this, next) ?? next;
438
+ store.set(this ?? fun, val);
439
+ return val;
440
+ };
441
+ Reflect.defineProperty(fun, 'name', { value: task.name + ' ' });
442
+ return fun;
443
+ }
444
+ }
445
+ $.$mol_memo = $mol_memo;
446
+ })($ || ($ = {}));
447
+
156
448
  ;
157
449
  "use strict";
158
450
  var $;
@@ -226,7 +518,32 @@ var $;
226
518
  "use strict";
227
519
  var $;
228
520
  (function ($) {
229
- function $mol_schema_pattern(Pattern) {
521
+ class $mol_memo_key extends $mol_wrapper {
522
+ static wrap(task) {
523
+ const store = new WeakMap();
524
+ const fun = function (key, next) {
525
+ let store2 = store.get(this ?? fun);
526
+ if (!store2)
527
+ store.set(this ?? fun, store2 = new Map);
528
+ const key_str = $mol_key(key);
529
+ if (next === undefined && store2.has(key_str))
530
+ return store2.get(key_str);
531
+ const val = task.call(this, key, next) ?? next;
532
+ store2.set(key_str, val);
533
+ return val;
534
+ };
535
+ Reflect.defineProperty(fun, 'name', { value: task.name + ' ' });
536
+ return fun;
537
+ }
538
+ }
539
+ $.$mol_memo_key = $mol_memo_key;
540
+ })($ || ($ = {}));
541
+
542
+ ;
543
+ "use strict";
544
+ var $;
545
+ (function ($) {
546
+ $.$mol_schema_pattern = $mol_memo_key.func(function $mol_schema_pattern(Pattern) {
230
547
  return class $mol_schema_pattern_ extends $mol_schema_string {
231
548
  static Pattern = Pattern;
232
549
  static toString() {
@@ -244,42 +561,32 @@ var $;
244
561
  }
245
562
  static default = '';
246
563
  };
247
- }
248
- $.$mol_schema_pattern = $mol_schema_pattern;
564
+ });
249
565
  })($ || ($ = {}));
250
566
 
251
567
  ;
252
568
  "use strict";
253
569
  var $;
254
570
  (function ($) {
255
- class $mol_schema_float extends $mol_schema_any {
256
- static guard(value) {
257
- if (typeof value === 'number')
258
- return value;
259
- return $mol_fail(new TypeError('Wrong type', { cause: { value, schema: this } }));
260
- }
261
- static default = Number.NaN;
262
- }
263
- $.$mol_schema_float = $mol_schema_float;
264
- })($ || ($ = {}));
265
-
266
- ;
267
- "use strict";
268
- var $;
269
- (function ($) {
270
- class $mol_schema_integer extends $mol_schema_float {
271
- $mol_schema_integer = true;
272
- static guard(value) {
273
- const val = super.guard(value);
274
- if (!Number.isFinite(val))
275
- return $mol_fail(new TypeError('Non finite', { cause: { value, schema: this } }));
276
- if (Math.trunc(val) !== val)
277
- return $mol_fail(new TypeError('Non integer', { cause: { value, schema: this } }));
278
- return val;
571
+ $.$mol_schema_instance = $mol_memo_key.func(function $mol_schema_instance(Class, ...args) {
572
+ class $mol_schema_instance_ extends $mol_schema_any {
573
+ static Class = Class;
574
+ static toString() {
575
+ if (this !== $mol_schema_instance_)
576
+ return super.toString();
577
+ return '$mol_schema_instance<' + $$.$mol_func_name(Class) + '>';
578
+ }
579
+ static guard(value) {
580
+ if (value != null && Object(value) instanceof Class)
581
+ return value;
582
+ return $mol_fail(new TypeError('Wrong class', { cause: { value, schema: this } }));
583
+ }
584
+ static default = new Class(...args);
279
585
  }
280
- static default = 0;
281
- }
282
- $.$mol_schema_integer = $mol_schema_integer;
586
+ return ((Class[Symbol.hasInstance] === $mol_schema_any[Symbol.hasInstance])
587
+ ? Class
588
+ : $mol_schema_instance_);
589
+ });
283
590
  })($ || ($ = {}));
284
591
 
285
592
  ;
@@ -289,7 +596,7 @@ var $;
289
596
  "use strict";
290
597
  var $;
291
598
  (function ($) {
292
- function $mol_schema_every(Schemas) {
599
+ $.$mol_schema_every = $mol_memo_key.func(function $mol_schema_every(Schemas) {
293
600
  return class $mol_schema_every_ extends $mol_schema_any {
294
601
  static Schemas = Schemas;
295
602
  static toString() {
@@ -310,50 +617,47 @@ var $;
310
617
  }
311
618
  static default = Schemas.find(Scheme => this.check(Scheme.default));
312
619
  };
313
- }
314
- $.$mol_schema_every = $mol_schema_every;
620
+ });
315
621
  })($ || ($ = {}));
316
622
 
317
623
  ;
318
624
  "use strict";
319
625
  var $;
320
626
  (function ($) {
321
- function $mol_schema_range(Min, Max) {
627
+ $.$mol_schema_range = $mol_memo_key.func(function $mol_schema_range(Range) {
322
628
  return class $mol_schema_range_ extends $mol_schema_any {
323
- static Min = Min;
324
- static Max = Max;
629
+ static Range = Range;
325
630
  static toString() {
326
631
  if (this !== $mol_schema_range_)
327
632
  return super.toString();
328
- return '$mol_schema_range<' + $mol_key(Min) + ',' + $mol_key(Max) + '>';
633
+ return '$mol_schema_range<' + $mol_key(Range) + '>';
329
634
  }
330
635
  static guard(value) {
331
636
  if (typeof value !== 'number' && typeof value !== 'bigint')
332
637
  return $mol_fail(new TypeError('Uncomparable type', { cause: { value, schema: this } }));
333
- if (!(value <= Max))
638
+ if (!(value <= Range[1]))
334
639
  return $mol_fail(new TypeError('Too large', { cause: { value, schema: this } }));
335
- if (!(value >= Min))
640
+ if (!(value >= Range[0]))
336
641
  return $mol_fail(new TypeError('Too small', { cause: { value, schema: this } }));
337
642
  return value;
338
643
  }
339
644
  static cast(val) {
340
- if (val > Max)
341
- return Max;
342
- if (val >= Min)
645
+ if (val > Range[1])
646
+ return Range[1];
647
+ if (val >= Range[0])
343
648
  return val;
344
- return Min;
649
+ return Range[0];
345
650
  }
346
- static default = Min;
651
+ static default = Range[0];
347
652
  };
348
- }
349
- $.$mol_schema_range = $mol_schema_range;
653
+ });
350
654
  })($ || ($ = {}));
351
655
 
352
656
  ;
353
657
  "use strict";
354
658
  var $;
355
659
  (function ($) {
356
- class $mol_schema_positive extends $mol_schema_range(0, Number.POSITIVE_INFINITY) {
660
+ class $mol_schema_positive extends $mol_schema_range([0, Number.POSITIVE_INFINITY]) {
357
661
  }
358
662
  $.$mol_schema_positive = $mol_schema_positive;
359
663
  })($ || ($ = {}));
@@ -374,7 +678,7 @@ var $;
374
678
  "use strict";
375
679
  var $;
376
680
  (function ($) {
377
- function $mol_schema_enum(Options) {
681
+ $.$mol_schema_enum = $mol_memo_key.func(function $mol_schema_enum(Options) {
378
682
  return class $mol_schema_enum_ extends $mol_schema_any {
379
683
  static Options = Options;
380
684
  static toString() {
@@ -394,15 +698,14 @@ var $;
394
698
  }
395
699
  static default = Options[0];
396
700
  };
397
- }
398
- $.$mol_schema_enum = $mol_schema_enum;
701
+ });
399
702
  })($ || ($ = {}));
400
703
 
401
704
  ;
402
705
  "use strict";
403
706
  var $;
404
707
  (function ($) {
405
- class $mol_schema_negative extends $mol_schema_range(Number.NEGATIVE_INFINITY, 0) {
708
+ class $mol_schema_negative extends $mol_schema_range([Number.NEGATIVE_INFINITY, 0]) {
406
709
  }
407
710
  $.$mol_schema_negative = $mol_schema_negative;
408
711
  })($ || ($ = {}));
@@ -411,7 +714,7 @@ var $;
411
714
  "use strict";
412
715
  var $;
413
716
  (function ($) {
414
- function $mol_schema_some(Variants) {
717
+ $.$mol_schema_some = $mol_memo_key.func(function $mol_schema_some(Variants) {
415
718
  return class $mol_schema_some_ extends $mol_schema_any {
416
719
  static Variants = Variants;
417
720
  static toString() {
@@ -441,15 +744,14 @@ var $;
441
744
  }
442
745
  static default = Variants[0].default;
443
746
  };
444
- }
445
- $.$mol_schema_some = $mol_schema_some;
747
+ });
446
748
  })($ || ($ = {}));
447
749
 
448
750
  ;
449
751
  "use strict";
450
752
  var $;
451
753
  (function ($) {
452
- function $mol_schema_maybe(Some) {
754
+ $.$mol_schema_maybe = $mol_memo_key.func(function $mol_schema_maybe(Some) {
453
755
  return class $mol_schema_maybe_ extends $mol_schema_some([$mol_schema_enum([undefined, null]), Some]) {
454
756
  static Some = Some;
455
757
  static toString() {
@@ -458,15 +760,14 @@ var $;
458
760
  return '$mol_schema_maybe<' + $mol_key(Some) + '>';
459
761
  }
460
762
  };
461
- }
462
- $.$mol_schema_maybe = $mol_schema_maybe;
763
+ });
463
764
  })($ || ($ = {}));
464
765
 
465
766
  ;
466
767
  "use strict";
467
768
  var $;
468
769
  (function ($) {
469
- function $mol_schema_list(Item) {
770
+ $.$mol_schema_list = $mol_memo_key.func(function $mol_schema_list(Item) {
470
771
  return class $mol_schema_list_ extends $mol_schema_any {
471
772
  static Item = Item;
472
773
  static toString() {
@@ -494,22 +795,20 @@ var $;
494
795
  }
495
796
  static default = [];
496
797
  };
497
- }
498
- $.$mol_schema_list = $mol_schema_list;
798
+ });
499
799
  })($ || ($ = {}));
500
800
 
501
801
  ;
502
802
  "use strict";
503
803
  var $;
504
804
  (function ($) {
505
- function $mol_schema_dict(Key, Val) {
805
+ $.$mol_schema_dict = $mol_memo_key.func(function $mol_schema_dict(Pair) {
506
806
  return class $mol_schema_dict_ extends $mol_schema_any {
507
- static Key = Key;
508
- static Val = Val;
807
+ static Pair = Pair;
509
808
  static toString() {
510
809
  if (this !== $mol_schema_dict_)
511
810
  return super.toString();
512
- return '$mol_schema_dict<' + $mol_key(Key) + ',' + $mol_key(Key) + '>';
811
+ return '$mol_schema_dict<' + $mol_key(Pair) + '>';
513
812
  }
514
813
  static guard(value) {
515
814
  if (Object.getPrototypeOf(Object.getPrototypeOf(value))) {
@@ -517,13 +816,13 @@ var $;
517
816
  }
518
817
  for (const key in value) {
519
818
  try {
520
- Key.guard(key);
819
+ Pair[0].guard(key);
521
820
  }
522
821
  catch (error) {
523
822
  return $mol_fail(new TypeError('Wrong key', { cause: { key, error, value, schema: this } }));
524
823
  }
525
824
  try {
526
- Val.guard(value[key]);
825
+ Pair[1].guard(value[key]);
527
826
  }
528
827
  catch (error) {
529
828
  return $mol_fail(new TypeError('Wrong val', { cause: { key, error, value, schema: this } }));
@@ -536,23 +835,22 @@ var $;
536
835
  return this.default;
537
836
  const res = {};
538
837
  for (const key in value) {
539
- if (!Key.check(key))
838
+ if (!Pair[0].check(key))
540
839
  continue;
541
- res[key] = Val.cast(value[key]);
840
+ res[key] = Pair[1].cast(value[key]);
542
841
  }
543
842
  return res;
544
843
  }
545
844
  static default = {};
546
845
  };
547
- }
548
- $.$mol_schema_dict = $mol_schema_dict;
846
+ });
549
847
  })($ || ($ = {}));
550
848
 
551
849
  ;
552
850
  "use strict";
553
851
  var $;
554
852
  (function ($) {
555
- function $mol_schema_record(Fields) {
853
+ $.$mol_schema_record = $mol_memo_key.func(function $mol_schema_record(Fields) {
556
854
  return class $mol_schema_record_ extends $mol_schema_any {
557
855
  static Fields = Fields;
558
856
  static toString() {
@@ -584,8 +882,7 @@ var $;
584
882
  }
585
883
  static default = Object.fromEntries(Object.entries(Fields).map(([field, Field]) => [field, Field.default]));
586
884
  };
587
- }
588
- $.$mol_schema_record = $mol_schema_record;
885
+ });
589
886
  })($ || ($ = {}));
590
887
 
591
888
  ;