mnemonica 1.4.8 → 1.5.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.
Files changed (41) hide show
  1. package/FOR_HUMANS.md +95 -31
  2. package/README.md +10 -9
  3. package/SKILL.md +4 -4
  4. package/build/api/hooks/hookTypes.d.ts +2 -0
  5. package/build/api/hooks/hookTypes.js +25 -0
  6. package/build/api/hooks/index.d.ts +2 -1
  7. package/build/api/hooks/index.js +6 -2
  8. package/build/api/hooks/registerHook.d.ts +1 -1
  9. package/build/api/hooks/registerHook.js +4 -15
  10. package/build/api/hooks/unregisterHook.d.ts +2 -0
  11. package/build/api/hooks/unregisterHook.js +18 -0
  12. package/build/api/index.d.ts +4 -3
  13. package/build/api/index.js +2 -1
  14. package/build/api/types/compileNewModificatorFunctionBody.d.ts +4 -2
  15. package/build/api/types/compileNewModificatorFunctionBody.js +16 -7
  16. package/build/api/types/index.d.ts +2 -2
  17. package/build/api/types/index.js +21 -18
  18. package/build/api/utils/index.d.ts +1 -2
  19. package/build/api/utils/index.js +37 -13
  20. package/build/descriptors/types/index.js +19 -12
  21. package/build/index.d.ts +25 -17
  22. package/build/index.js +35 -50
  23. package/build/types/index.d.ts +242 -225
  24. package/build/types/index.js +1 -1
  25. package/docs/UTILS.md +4 -5
  26. package/docs/async-constructors.md +32 -13
  27. package/docs/hott-correspondence.md +1 -1
  28. package/docs/typed-lookup.md +31 -37
  29. package/module/index.js +2 -0
  30. package/package.json +2 -2
  31. package/src/api/hooks/hookTypes.ts +37 -0
  32. package/src/api/hooks/index.ts +8 -1
  33. package/src/api/hooks/registerHook.ts +6 -19
  34. package/src/api/hooks/unregisterHook.ts +36 -0
  35. package/src/api/index.ts +2 -0
  36. package/src/api/types/compileNewModificatorFunctionBody.ts +79 -18
  37. package/src/api/types/index.ts +64 -54
  38. package/src/api/utils/index.ts +5 -51
  39. package/src/descriptors/types/index.ts +31 -22
  40. package/src/index.ts +271 -161
  41. package/src/types/index.ts +812 -590
@@ -22,12 +22,12 @@
22
22
  */
23
23
 
24
24
  import {
25
- _Internal_TC_,
26
25
  TypeDescriptorInstance,
27
26
  TypeClass,
28
27
  TypeDef,
29
28
  CollectionDef,
30
29
  constructorOptions,
30
+ TypeConfig,
31
31
  TypeAbsorber,
32
32
  ModificationConstructorFactory,
33
33
  MnemonicaConstructorFactory,
@@ -197,10 +197,7 @@ const installLazyDefinition = function (
197
197
  return shimResult;
198
198
  }
199
199
  const { handler } = cell;
200
- const modificatorBody = compileNewModificatorFunctionBody(
201
- TypeName,
202
- cell.asClass
203
- );
200
+ const modificatorBody = compileNewModificatorFunctionBody(TypeName);
204
201
  const wrappedBody = modificatorBody(
205
202
  handler,
206
203
  CreationHandler,
@@ -375,13 +372,22 @@ const typeDescriptorCreator = function (
375
372
 
376
373
  };
377
374
 
378
- // the creator is only ever invoked with `new` (the createFrom* call sites
379
- // below); a function expression carries no construct signature, so it is
380
- // named once as the constructor interface it is used through — the
381
- // prototype contract is attached immediately below
382
- // The registered result is a TypeProxy, not the descriptor's declared
383
- // TypeDescriptorInstance shape.
384
- const TypeDescriptor = typeDescriptorCreator as _Internal_TC_<TypeDescriptorInstance>;
375
+ // the creator, as it is used: invoked with `new` (the createFrom* call
376
+ // sites below), it returns the TypeProxy it registers in the subtypes map
377
+ // — a Proxy materializing the TypeClass surface dynamically — while its
378
+ // prototype is the descriptor's TypeDescriptorInstance contract
379
+ type TypeDescriptorArgs = Parameters<typeof typeDescriptorCreator>;
380
+ interface TypeDescriptorConstructor extends NewableFunction {
381
+ new (...args: TypeDescriptorArgs): TypeClass;
382
+ (this: TypeDescriptorInstance, ...args: TypeDescriptorArgs): ReturnType<typeof typeDescriptorCreator>;
383
+ readonly prototype: TypeDescriptorInstance;
384
+ }
385
+
386
+ // the creator is only ever invoked with `new`; a function expression
387
+ // carries no construct signature, so it is named once as the constructor
388
+ // interface it is used through — the prototype contract is attached
389
+ // immediately below
390
+ const TypeDescriptor = typeDescriptorCreator as TypeDescriptorConstructor;
385
391
 
386
392
  Object.assign(
387
393
  TypeDescriptor.prototype,
@@ -391,8 +397,8 @@ Object.assign(
391
397
  TypeDescriptor.prototype.define = function (
392
398
  this: TypeDescriptorInstance,
393
399
  TypeOrTypeName: string | DefineNewableOrCallable,
394
- constructHandlerOrConfig?: DefineNewableOrCallable | object,
395
- config?: object
400
+ constructHandlerOrConfig?: DefineNewableOrCallable | object | boolean,
401
+ config?: object | boolean
396
402
  ) {
397
403
  const result = define.call(
398
404
  define,
@@ -408,7 +414,7 @@ TypeDescriptor.prototype.lazy = function (
408
414
  this: TypeDescriptorInstance,
409
415
  TypeName: string,
410
416
  getter?: LazyDef<object>,
411
- config?: object
417
+ config?: object | boolean
412
418
  ) {
413
419
  const result = lazy.call(
414
420
  this,
@@ -452,7 +458,7 @@ odp(
452
458
  'decorate', {
453
459
  get (this: TypeDescriptorInstance) {
454
460
  const self = this;
455
- const result = function (options?: object) {
461
+ const result = function (options?: object | boolean) {
456
462
  const decorator = function (cstr: DefineNewable) {
457
463
  const { name } = cstr;
458
464
  // define() returns the registered constructor itself —
@@ -546,12 +552,30 @@ const checkDuplicate = function (target: TypesMap, name: string): void {
546
552
  // Both receive the resolved context and return TypeDescriptor.
547
553
  // ============================================================
548
554
 
555
+ // what is given in the config place overlaps defaultOptions (see constants):
556
+ // an object its own keys, a boolean strictChain alone, and a function
557
+ // ModificationConstructor alone. Anything else overlaps nothing
558
+ const makeOwnConfig = function (config: unknown): constructorOptions {
559
+ if (typeof config === 'boolean') {
560
+ const strictChainResult = { strictChain : config };
561
+ return strictChainResult;
562
+ }
563
+ if (typeof config === 'function') {
564
+ const modificatorResult = {
565
+ ModificationConstructor : config as ModificationConstructorFactory
566
+ };
567
+ return modificatorResult;
568
+ }
569
+ const result = typeof config === 'object' && config !== null ? config : {};
570
+ return result;
571
+ };
572
+
549
573
  const createFromDirectHandler = function (
550
574
  defineOrigin: TypeAbsorber,
551
575
  target: TypesMap,
552
576
  name: string,
553
- handler?: ConstructHandler,
554
- config?: constructorOptions,
577
+ handler: ConstructHandler | undefined,
578
+ config: unknown,
555
579
  lazyGetter?: LazyDef<object>
556
580
  ) {
557
581
  // for a lazy type the handler stays the empty shim: the real constructor
@@ -559,10 +583,7 @@ const createFromDirectHandler = function (
559
583
  // installLazyDefinition above and lazyDefinition.ts)
560
584
  handler = handler || function () { };
561
585
  const asClass = isClass(handler);
562
- const modificatorBody = compileNewModificatorFunctionBody(
563
- name,
564
- asClass
565
- );
586
+ const modificatorBody = compileNewModificatorFunctionBody(name);
566
587
 
567
588
  const makeConstructHandler = modificatorBody(
568
589
  handler as ConstructHandler,
@@ -570,13 +591,13 @@ const createFromDirectHandler = function (
570
591
  SymbolConstructorName
571
592
  );
572
593
 
573
- if (config instanceof Function) {
574
- config = {
575
- ModificationConstructor : config as ModificationConstructorFactory
576
- };
577
- }
578
-
579
- config!.asClass = asClass;
594
+ // the detected mode goes into the type's own copy, never into the
595
+ // caller's options object
596
+ const typeConfig: TypeConfig = Object.assign(
597
+ {},
598
+ makeOwnConfig(config),
599
+ { asClass }
600
+ );
580
601
 
581
602
  const proto = (
582
603
  hop(
@@ -604,14 +625,9 @@ const createFromDirectHandler = function (
604
625
  name,
605
626
  makeConstructHandler,
606
627
  proto,
607
- config,
628
+ typeConfig,
608
629
  lazyGetter
609
- // new TypeDescriptor() returns the TypeProxy it registers in the
610
- // subtypes map — a Proxy materializing the TypeClass surface
611
- // dynamically — so the public constructor contract is named here
612
- // The declared descriptor result and dynamic proxy surface differ;
613
- // changing the producing signature breaks descriptor prototype methods.
614
- ) as unknown as TypeClass;
630
+ );
615
631
  return result;
616
632
  };
617
633
 
@@ -626,7 +642,7 @@ const createFromLazyDefinition = function (
626
642
  target: TypesMap,
627
643
  name: string,
628
644
  getter: LazyDef<object>,
629
- config: constructorOptions
645
+ config: unknown
630
646
  ) {
631
647
 
632
648
  const result = createFromDirectHandler(
@@ -651,15 +667,13 @@ export const define = function (
651
667
  this: unknown,
652
668
  subtypes: TypesMap,
653
669
  TypeOrTypeName: string | DefineNewableOrCallable,
654
- constructHandlerOrConfig?: DefineNewableOrCallable | object,
655
- config?: object
670
+ constructHandlerOrConfig?: DefineNewableOrCallable | object | boolean,
671
+ config?: object | boolean
656
672
  ): TypeClass {
657
673
 
658
- if (!config || (typeof config !== 'object' && typeof config !== 'function')) {
659
- config = {};
660
- }
661
-
662
- // --- Branch: function passed as first arg ---
674
+ // --- Branch: function or class passed as first arg ---
675
+ // its own name is the TypeName, so the arguments shift left by one:
676
+ // the second is the config, and nothing after the config is read
663
677
  if (typeof TypeOrTypeName === 'function') {
664
678
  const fn = TypeOrTypeName;
665
679
  if (fn.name) {
@@ -672,7 +686,7 @@ export const define = function (
672
686
  subtypes,
673
687
  fn.name,
674
688
  fn,
675
- config
689
+ constructHandlerOrConfig
676
690
  );
677
691
  return defineResult;
678
692
  }
@@ -690,8 +704,8 @@ export const define = function (
690
704
  let handler: ConstructHandler | undefined;
691
705
  if (typeof constructHandlerOrConfig === 'function') {
692
706
  handler = constructHandlerOrConfig as ConstructHandler;
693
- } else if (typeof constructHandlerOrConfig === 'object') {
694
- config = constructHandlerOrConfig as constructorOptions;
707
+ } else if (typeof constructHandlerOrConfig === 'object' || typeof constructHandlerOrConfig === 'boolean') {
708
+ config = constructHandlerOrConfig;
695
709
  }
696
710
 
697
711
  const {
@@ -711,7 +725,7 @@ export const define = function (
711
725
  target,
712
726
  name,
713
727
  handler,
714
- config as constructorOptions
728
+ config
715
729
  );
716
730
  return result;
717
731
  };
@@ -721,7 +735,7 @@ export const lazy = function (
721
735
  subtypes: TypesMap,
722
736
  TypeName: string,
723
737
  getter?: LazyDef<object>,
724
- config?: object
738
+ config?: object | boolean
725
739
  ): TypeClass {
726
740
 
727
741
  // the unnamed lazy(() => Constructor) form is dropped: the callback is
@@ -749,10 +763,6 @@ export const lazy = function (
749
763
  throw new WRONG_MODIFICATION_PATTERN( msg );
750
764
  }
751
765
 
752
- if (!config || (typeof config !== 'object' && typeof config !== 'function')) {
753
- config = {};
754
- }
755
-
756
766
  // the same dotted-path resolution as define's string branch:
757
767
  // lazy('Parent.Child', ...) lands under Parent, and a taken name fails
758
768
  // with the descriptor's ALREADY_DECLARED, exactly like define
@@ -768,7 +778,7 @@ export const lazy = function (
768
778
  target,
769
779
  name,
770
780
  getter,
771
- config as constructorOptions
781
+ config
772
782
  );
773
783
  return lazyResult;
774
784
  };
@@ -22,7 +22,7 @@ import type {
22
22
  TypeClass
23
23
  } from '../../types';
24
24
 
25
- import compileNewModificatorFunctionBody, { ConstructHandler } from '../types/compileNewModificatorFunctionBody';
25
+ import compileNewModificatorFunctionBody, { isClass } from '../types/compileNewModificatorFunctionBody';
26
26
 
27
27
  // CreationHandler - handles constructor return values
28
28
  // Moved to api/types/index.ts as per refactoring plan
@@ -213,54 +213,9 @@ const findSubTypeFromParent = (instance: parentSub | object | undefined, subType
213
213
  return subtype;
214
214
  };
215
215
 
216
- // const isClass = ( functionPointer: CallableFunction ) => {
217
- // const value = Function.prototype.toString.call( functionPointer );
218
- // return /^\s*class\s+/.test( value.trim() );
219
- // };
220
-
221
- // accordingly to the gist from here:
222
- // https://gist.github.com/wentout/ea3afe9c822a6b6ef32f9e4f3e98b1ba
223
- const isClass = (fn: ConstructHandler) => {
224
-
225
- /*
226
- const str = String(fn);
227
- const result = str.indexOf('class ') === 0;
228
- return result;
229
-
230
-
231
- // not necessary to check fn for typeof
232
- // because of other checks made before
233
- // if (typeof fn !== 'function') {
234
- // return false;
235
- // }
236
-
237
- // seemingly
238
- // class prototype is always object
239
- if (!(fn.prototype instanceof Object)) {
240
- return false;
241
- }
242
- // but only if not augmented
243
-
244
- // class prototype.constructor cannot be re-defined (usually)
245
- // but for functions user may re-assign it easily
246
- // just assigning the way .prototype = {}
247
- // and they 99.9% do this, cause nobody cares
248
- // but we are checking if this is class, so it works ...
249
- if (fn.prototype.constructor !== fn) {
250
- return false;
251
- }
252
-
253
- // and the most unknown thing is that,
254
- // cause for functions it it writeable )))
255
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
256
- return Reflect.getOwnPropertyDescriptor(fn, 'prototype')!.writable === false;
257
-
258
- */
259
-
260
- const protoDesc = Reflect.getOwnPropertyDescriptor(fn, 'prototype');
261
- const result = protoDesc !== undefined && protoDesc.writable === false;
262
- return result;
263
- };
216
+ // isClass lives beside the construct-handler shapes it narrows, in
217
+ // compileNewModificatorFunctionBody.ts (the compiler picks the class
218
+ // branch with it); re-exported here as part of TypesUtils
264
219
 
265
220
  const makeErrorModificatorType = (
266
221
  TypeName: string,
@@ -277,8 +232,7 @@ const makeErrorModificatorType = (
277
232
  );
278
233
 
279
234
  const result = modificatorType();
280
- const constructorResult = result as MnemonicaConstructor;
281
- return constructorResult;
235
+ return result;
282
236
 
283
237
  };
284
238
 
@@ -35,6 +35,7 @@ import * as hooksAPI from '../../api/hooks';
35
35
 
36
36
  const {
37
37
  registerHook,
38
+ unregisterHook,
38
39
  invokeHook,
39
40
  registerFlowChecker,
40
41
  } = hooksAPI;
@@ -216,7 +217,7 @@ odp(
216
217
  {
217
218
  get (this: TypesCollection) {
218
219
  const self = this;
219
- const result = function (config?: object) {
220
+ const result = function (config?: object | boolean) {
220
221
  const decorator = function (cstr: DefineNewable) {
221
222
  const { name } = cstr;
222
223
  const defineResult = self.define(
@@ -258,27 +259,35 @@ odp(
258
259
  }
259
260
  );
260
261
 
261
- odp(
262
- TypesCollection.prototype,
263
- 'registerHook',
264
- {
265
- get (this: TypesCollection) {
266
-
267
- const self = this;
268
- const result = function (hookName: string, hookCallback: hook) {
269
- // return proto.registerHook.call( typesCollections.get( self ), hookName, hookCallback );
270
- const hookResult = registerHook.call(
271
- self,
272
- hookName,
273
- hookCallback
274
- );
275
- return hookResult;
276
- }.bind(this);
277
- return result;
278
- },
279
- enumerable : true
280
- }
281
- );
262
+ // on / off are the same two functions under short names
263
+ Object.entries({
264
+ registerHook,
265
+ on : registerHook,
266
+ unregisterHook,
267
+ off : unregisterHook,
268
+ }).forEach(([ methodName, hookMethod ]) => {
269
+ odp(
270
+ TypesCollection.prototype,
271
+ methodName,
272
+ {
273
+ get (this: TypesCollection) {
274
+
275
+ const self = this;
276
+ const result = function (hookName: string, hookCallback: hook) {
277
+ // return proto.registerHook.call( typesCollections.get( self ), hookName, hookCallback );
278
+ const hookResult = hookMethod.call(
279
+ self,
280
+ hookName,
281
+ hookCallback
282
+ );
283
+ return hookResult;
284
+ }.bind(this);
285
+ return result;
286
+ },
287
+ enumerable : true
288
+ }
289
+ );
290
+ });
282
291
 
283
292
  odp(
284
293
  TypesCollection.prototype,