mnemonica 1.0.1 → 1.0.6
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/.ai/ONBOARDING.md +1 -1
- package/.ai/ask/AGENTS.md +15 -9
- package/.ai/rules-skill/contributing.md +1 -1
- package/.ai/rules-skill/define-patterns.md +8 -2
- package/.ai/rules-skill/ecosystem.md +4 -4
- package/.ai/rules-skill/instance-methods.md +48 -15
- package/.ai/rules-skill/type-system.md +12 -2
- package/AGENTS.md +6 -2
- package/CONTRIBUTING.md +7 -1
- package/FOR_HUMANS.md +143 -199
- package/README.md +74 -39
- package/build/api/errors/exceptionConstructor.js +20 -12
- package/build/api/errors/index.js +9 -5
- package/build/api/errors/throwModificationError.js +15 -9
- package/build/api/index.js +35 -2
- package/build/api/types/InstanceCreator.js +5 -2
- package/build/api/types/Mnemosyne.d.ts +6 -6
- package/build/api/types/Mnemosyne.js +43 -120
- package/build/api/types/Props.js +5 -3
- package/build/api/types/TypeProxy.js +13 -9
- package/build/api/types/compileNewModificatorFunctionBody.js +14 -8
- package/build/api/types/index.js +56 -13
- package/build/api/utils/index.js +21 -12
- package/build/constants/index.js +11 -8
- package/build/descriptors/types/index.js +79 -26
- package/build/index.d.ts +4 -4
- package/build/index.js +62 -15
- package/build/types/index.d.ts +52 -31
- package/build/types/index.js +1 -1
- package/build/utils/clone.d.ts +1 -0
- package/build/utils/clone.js +11 -0
- package/build/utils/collectConstructors.js +5 -3
- package/build/utils/exception.d.ts +1 -0
- package/build/utils/exception.js +14 -0
- package/build/utils/extract.d.ts +2 -3
- package/build/utils/extract.js +3 -2
- package/build/utils/fork.d.ts +1 -0
- package/build/utils/fork.js +33 -0
- package/build/utils/index.d.ts +2 -3
- package/build/utils/index.js +21 -7
- package/build/utils/merge.d.ts +2 -1
- package/build/utils/merge.js +10 -6
- package/build/utils/parent.d.ts +1 -1
- package/build/utils/parent.js +3 -2
- package/build/utils/parse.d.ts +2 -12
- package/build/utils/parse.js +1 -1
- package/build/utils/pick.d.ts +4 -3
- package/build/utils/pick.js +4 -5
- package/build/utils/sibling.d.ts +1 -0
- package/build/utils/sibling.js +25 -0
- package/build/utils/toJSON.d.ts +1 -1
- package/build/utils/toJSON.js +3 -2
- package/docs/UTILS.md +184 -0
- package/docs/ai-learning-trajectory.md +1 -1
- package/docs/async-constructors.md +3 -1
- package/docs/empathy-in-ai.md +170 -0
- package/docs/hott-primer.md +47 -0
- package/docs/performance-vs-security.md +2 -2
- package/docs/purpose.md +38 -7
- package/docs/tactica-pattern.md +10 -10
- package/docs/theory-of-operations.md +224 -0
- package/docs/typed-lookup.md +12 -12
- package/package.json +12 -7
- package/src/api/errors/exceptionConstructor.ts +14 -9
- package/src/api/errors/index.ts +8 -4
- package/src/api/errors/throwModificationError.ts +10 -9
- package/src/api/types/InstanceCreator.ts +5 -8
- package/src/api/types/Mnemosyne.ts +72 -231
- package/src/api/types/Props.ts +4 -2
- package/src/api/types/TypeProxy.ts +7 -11
- package/src/api/types/compileNewModificatorFunctionBody.ts +13 -7
- package/src/api/types/index.ts +15 -8
- package/src/api/utils/index.ts +16 -14
- package/src/constants/index.ts +6 -9
- package/src/descriptors/types/index.ts +44 -24
- package/src/index.ts +28 -21
- package/src/types/index.ts +101 -69
- package/src/utils/clone.ts +11 -0
- package/src/utils/collectConstructors.ts +4 -2
- package/src/utils/exception.ts +16 -0
- package/src/utils/extract.ts +5 -2
- package/src/utils/fork.ts +57 -0
- package/src/utils/index.ts +32 -13
- package/src/utils/merge.ts +18 -6
- package/src/utils/parent.ts +4 -6
- package/src/utils/parse.ts +5 -4
- package/src/utils/pick.ts +10 -2
- package/src/utils/sibling.ts +33 -0
- package/src/utils/toJSON.ts +3 -2
package/FOR_HUMANS.md
CHANGED
|
@@ -323,17 +323,14 @@ console.log(subInstance.some); // 'arguments' (inherited)
|
|
|
323
323
|
console.log(subInstance.other); // 'data needed' (own)
|
|
324
324
|
```
|
|
325
325
|
|
|
326
|
-
### The
|
|
326
|
+
### The `utils.extract()` Utility
|
|
327
327
|
|
|
328
328
|
Extract all inherited properties into a flat object:
|
|
329
329
|
|
|
330
330
|
```js
|
|
331
|
-
const extracted = subInstance.extract();
|
|
332
|
-
// Result: { data, description, other, some }
|
|
333
|
-
|
|
334
|
-
// Or use the standalone utility
|
|
335
331
|
const { utils: { extract } } = require('mnemonica');
|
|
336
|
-
const
|
|
332
|
+
const extracted = extract(subInstance);
|
|
333
|
+
// Result: { data, description, other, some }
|
|
337
334
|
```
|
|
338
335
|
|
|
339
336
|
---
|
|
@@ -444,54 +441,21 @@ const subInstance = call(someInstance, SomeSubType, 'arg1', 'arg2');
|
|
|
444
441
|
|
|
445
442
|
### Exported Type Definitions
|
|
446
443
|
|
|
447
|
-
The
|
|
444
|
+
The most commonly used types for TypeScript projects:
|
|
448
445
|
|
|
449
446
|
```typescript
|
|
450
447
|
import {
|
|
451
|
-
//
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
//
|
|
457
|
-
MnemonicaInstance, // Instance methods interface (extract, pick, parent, fork, etc.)
|
|
458
|
-
Props, // Internal instance properties (__type__, __args__, __parent__, etc.)
|
|
459
|
-
SiblingAccessor, // Sibling type accessor type
|
|
460
|
-
|
|
461
|
-
// Type definition types
|
|
462
|
-
TypeClass, // Base type constructor returned by define()
|
|
463
|
-
IDefinitorInstance, // Definitor instance with define/lookup methods and subtypes
|
|
464
|
-
DecoratedClass, // Type for @decorate decorated classes
|
|
465
|
-
TypeDef, // Type definition object structure
|
|
466
|
-
|
|
467
|
-
// Configuration types
|
|
468
|
-
constructorOptions, // Type config options (strictChain, blockErrors, etc.)
|
|
469
|
-
hooksTypes, // 'preCreation' | 'postCreation' | 'creationError'
|
|
470
|
-
hook, // Hook callback type
|
|
471
|
-
hooksOpts, // Hook options passed to callbacks
|
|
472
|
-
CollectionDef, // Types collection definition
|
|
473
|
-
|
|
474
|
-
// Utility function types
|
|
475
|
-
ApplyFunction, // apply(entity, Ctor, args) => S
|
|
476
|
-
CallFunction, // call(entity, Ctor, ...args) => S
|
|
477
|
-
BindFunction, // bind(entity, Ctor) => (...args) => S
|
|
478
|
-
|
|
479
|
-
// createTypesCollection types — use only when you need isolation
|
|
480
|
-
CreateTypesCollectionFunction, // (config?: constructorOptions) => TypesCollection
|
|
481
|
-
TypesCollection, // Interface returned by createTypesCollection
|
|
482
|
-
|
|
483
|
-
// Configuration helper types
|
|
484
|
-
HideInstanceMethodsOptions, // constructorOptions & { exposeInstanceMethods: true }
|
|
485
|
-
IsHidingMethods<Config>, // Conditional type for exposeInstanceMethods: false detection
|
|
486
|
-
|
|
487
|
-
// Utility types
|
|
488
|
-
Proto<P, T>, // Merges parent P and child T types
|
|
489
|
-
SN, // String-Name map for nested constructors
|
|
490
|
-
SubtypesMap, // Map<string, TypeClass>
|
|
491
|
-
TypeAbsorber, // Main define() function interface with overloads
|
|
448
|
+
IDEF, // Base constructor function type
|
|
449
|
+
MnemonicaInstance, // Instance methods interface (extract, pick, parent, fork, etc.)
|
|
450
|
+
constructorOptions, // Type config options (strictChain, blockErrors, etc.)
|
|
451
|
+
hooksTypes, // 'preCreation' | 'postCreation' | 'creationError'
|
|
452
|
+
hook, // Hook callback type
|
|
453
|
+
TypeClass, // Base type constructor returned by define()
|
|
492
454
|
} from 'mnemonica';
|
|
493
455
|
```
|
|
494
456
|
|
|
457
|
+
Additional types available: `Constructor`, `Props`, `SiblingAccessor`, `IDefinitorInstance`, `DecoratedClass`, `TypeDef`, `CollectionDef`, `hooksOpts`, `ApplyFunction`, `CallFunction`, `BindFunction`, `CreateTypesCollectionFunction`, `TypesCollection`, `HideInstanceMethodsOptions`, `IsHidingMethods`, `Proto`, `SubtypesMap`, `TypeAbsorber`. See [`src/types/index.ts`](./src/types/index.ts) for full definitions.
|
|
458
|
+
|
|
495
459
|
### Generic Type Patterns
|
|
496
460
|
|
|
497
461
|
Define types with proper generic constraints for full type safety:
|
|
@@ -538,6 +502,19 @@ const AsyncTypeNoReturn = define('AsyncType', async function () {
|
|
|
538
502
|
|
|
539
503
|
## API Reference
|
|
540
504
|
|
|
505
|
+
**Quick reference:**
|
|
506
|
+
|
|
507
|
+
| I want to... | Use |
|
|
508
|
+
|---|---|
|
|
509
|
+
| Define a type | `define('Name', ctor)` |
|
|
510
|
+
| Create from instance | `new instance.SubType(args)` |
|
|
511
|
+
| Look up a type | `lookup('Name')` or `lookupTyped('Name')` |
|
|
512
|
+
| Read construction history | `getProps(instance)` |
|
|
513
|
+
| Get parent instance | `utils.parent(instance)` or `utils.parent(instance, 'TypeName')` |
|
|
514
|
+
| Flatten to plain object | `utils.extract(instance)` |
|
|
515
|
+
| Add lifecycle hooks | `type.registerHook('postCreation', cb)` |
|
|
516
|
+
| Use classes | `@decorate()` decorator |
|
|
517
|
+
|
|
541
518
|
### Core Functions
|
|
542
519
|
|
|
543
520
|
#### `define(typeName, constructHandler, config?)`
|
|
@@ -623,24 +600,16 @@ const subInstance = createSub('arg1', 'arg2');
|
|
|
623
600
|
|
|
624
601
|
TypeScript decorator for class-based definitions.
|
|
625
602
|
|
|
626
|
-
**Usage Patterns:**
|
|
627
|
-
|
|
628
603
|
```typescript
|
|
629
604
|
import { decorate } from 'mnemonica';
|
|
630
605
|
|
|
631
|
-
//
|
|
606
|
+
// Basic decoration
|
|
632
607
|
@decorate()
|
|
633
608
|
class MyClass {
|
|
634
609
|
field: number = 123;
|
|
635
610
|
}
|
|
636
611
|
|
|
637
|
-
//
|
|
638
|
-
@decorate({ strictChain: false, blockErrors: true })
|
|
639
|
-
class ConfiguredClass {
|
|
640
|
-
field: number = 123;
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
// 3. Nested decoration (define as subtype)
|
|
612
|
+
// Nested decoration (define as subtype)
|
|
644
613
|
@decorate()
|
|
645
614
|
class ParentClass {
|
|
646
615
|
parentField: string = 'parent';
|
|
@@ -654,30 +623,23 @@ class ChildClass {
|
|
|
654
623
|
// Create parent instance, then child from it
|
|
655
624
|
const parent = new ParentClass();
|
|
656
625
|
const child = new parent.ChildClass();
|
|
626
|
+
```
|
|
657
627
|
|
|
658
|
-
|
|
659
|
-
@decorate(ParentClass, { strictChain: false })
|
|
660
|
-
class ConfiguredChildClass {
|
|
661
|
-
field: number = 123;
|
|
662
|
-
}
|
|
628
|
+
**With configuration:**
|
|
663
629
|
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
class BaseDecorator {
|
|
669
|
-
baseField: number = 100;
|
|
630
|
+
```typescript
|
|
631
|
+
@decorate({ strictChain: false, blockErrors: true })
|
|
632
|
+
class ConfiguredClass {
|
|
633
|
+
field: number = 123;
|
|
670
634
|
}
|
|
671
635
|
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
class ExtendedClass {
|
|
676
|
-
extField: number = 200;
|
|
636
|
+
@decorate(ParentClass, { strictChain: false })
|
|
637
|
+
class ConfiguredChildClass {
|
|
638
|
+
field: number = 123;
|
|
677
639
|
}
|
|
678
640
|
```
|
|
679
641
|
|
|
680
|
-
**Note:**
|
|
642
|
+
**Note:** After a class is decorated with `@decorate()`, it can be used as a decorator for nested types (advanced pattern; may require `@ts-ignore` due to TypeScript limitations with callable class types).
|
|
681
643
|
|
|
682
644
|
#### `registerHook(Constructor, hookType, callback)`
|
|
683
645
|
|
|
@@ -700,7 +662,7 @@ For advanced TypeScript usage, the following types are exported from `mnemonica`
|
|
|
700
662
|
| Type | Description | Usage |
|
|
701
663
|
|------|-------------|-------|
|
|
702
664
|
| `IDEF<T>` | Base constructor function type | `define('Name', fn: IDEF<MyType>)` |
|
|
703
|
-
| `MnemonicaInstance` |
|
|
665
|
+
| `MnemonicaInstance` | Optional helper interface | Can be used when attaching the legacy instance methods to your own prototype |
|
|
704
666
|
| `TypeClass` | Base type constructor | `const MyType: TypeClass = define(...)` |
|
|
705
667
|
| `DecoratedClass<T>` | Decorated class type | `@decorate() class MyClass {}` |
|
|
706
668
|
| `IDefinitorInstance<N, S>` | Constructor with subtypes | Returned by `define()` with `.define()` method |
|
|
@@ -759,12 +721,6 @@ const MyType = myCollection.define('MyType', function () {});
|
|
|
759
721
|
const FoundType = myCollection.lookup('MyType');
|
|
760
722
|
```
|
|
761
723
|
|
|
762
|
-
```js
|
|
763
|
-
const { createTypesCollection } = require('mnemonica');
|
|
764
|
-
const myCollection = createTypesCollection();
|
|
765
|
-
const MyType = myCollection.define('MyType', function () {});
|
|
766
|
-
```
|
|
767
|
-
|
|
768
724
|
**`TypesCollection` Interface:**
|
|
769
725
|
|
|
770
726
|
`createTypesCollection()` returns a `TypesCollection` object with the following interface:
|
|
@@ -802,8 +758,7 @@ const { createTypesCollection } = require('mnemonica');
|
|
|
802
758
|
// Create isolated collection with custom config
|
|
803
759
|
const myCollection = createTypesCollection({
|
|
804
760
|
strictChain: false,
|
|
805
|
-
blockErrors: false
|
|
806
|
-
exposeInstanceMethods: false // Hide instance methods for cleaner API
|
|
761
|
+
blockErrors: false
|
|
807
762
|
});
|
|
808
763
|
|
|
809
764
|
// Define types in isolation
|
|
@@ -838,80 +793,106 @@ setProps(instance, { __timestamp__: Date.now() });
|
|
|
838
793
|
|
|
839
794
|
### Instance Methods
|
|
840
795
|
|
|
841
|
-
|
|
796
|
+
Starting from v1.0.6 these convenience methods are **no longer auto-injected** on instances. Use the standalone utilities from `utils`:
|
|
842
797
|
|
|
843
|
-
|
|
798
|
+
```js
|
|
799
|
+
const { utils } = require('mnemonica');
|
|
844
800
|
|
|
845
|
-
|
|
801
|
+
utils.extract(instance);
|
|
802
|
+
utils.pick(instance, 'key');
|
|
803
|
+
utils.parent(instance, 'ParentType');
|
|
804
|
+
utils.fork(instance)(newArgs);
|
|
805
|
+
utils.exception(instance, error);
|
|
806
|
+
utils.sibling(instance);
|
|
807
|
+
utils.clone(instance);
|
|
808
|
+
```
|
|
846
809
|
|
|
847
|
-
|
|
810
|
+
To restore the old instance-method style for a specific root constructor, attach the methods to its prototype **before** passing it to `define()`. See `test/instance-methods-helper.js` for a complete reference pattern.
|
|
848
811
|
|
|
849
|
-
|
|
850
|
-
|
|
812
|
+
#### `utils.extract(instance)`
|
|
813
|
+
|
|
814
|
+
Extracts all enumerable user properties into a single flat object. TypeScript
|
|
815
|
+
returns `Extracted<T>` — legacy instance-method names are filtered out and
|
|
816
|
+
optionality is preserved from the source type.
|
|
817
|
+
|
|
818
|
+
```ts
|
|
819
|
+
const extracted = utils.extract(instance);
|
|
820
|
+
// hover: { name: string; email: string; age: number; }
|
|
851
821
|
```
|
|
852
822
|
|
|
853
|
-
####
|
|
823
|
+
#### `utils.pick(instance, ...keys)` / `utils.pick(instance, [keys])`
|
|
854
824
|
|
|
855
|
-
Picks specific properties from the instance and its inheritance chain.
|
|
825
|
+
Picks specific properties from the instance and its inheritance chain. Literal
|
|
826
|
+
keys produce a typed subset; dynamic or unknown keys fall back to
|
|
827
|
+
`Record<string, unknown>`.
|
|
856
828
|
|
|
857
|
-
```
|
|
858
|
-
const picked =
|
|
859
|
-
//
|
|
860
|
-
|
|
829
|
+
```ts
|
|
830
|
+
const picked = utils.pick(instance, 'email', 'password');
|
|
831
|
+
// hover: { email: string; password: string; }
|
|
832
|
+
|
|
833
|
+
const pickedArray = utils.pick(instance, ['email', 'password']);
|
|
861
834
|
```
|
|
862
835
|
|
|
863
|
-
####
|
|
836
|
+
#### `utils.parent(instance, constructorName?)`
|
|
864
837
|
|
|
865
838
|
Gets the parent instance. If `constructorName` is provided, walks up the chain.
|
|
839
|
+
The structural return type is `object | undefined`; a specific nominal parent
|
|
840
|
+
type requires a `TypeRegistry` augmentation (see [`docs/typed-lookup.md`](./docs/typed-lookup.md)).
|
|
866
841
|
|
|
867
|
-
```
|
|
868
|
-
const immediateParent =
|
|
869
|
-
const specificParent =
|
|
842
|
+
```ts
|
|
843
|
+
const immediateParent = utils.parent(instance);
|
|
844
|
+
const specificParent = utils.parent(instance, 'UserType'); // object | undefined
|
|
870
845
|
```
|
|
871
846
|
|
|
872
|
-
####
|
|
847
|
+
#### `utils.clone(instance)`
|
|
873
848
|
|
|
874
|
-
|
|
849
|
+
Returns a cloned instance (same parent, same args). The return type is the
|
|
850
|
+
instance type.
|
|
875
851
|
|
|
876
|
-
```
|
|
877
|
-
const cloned =
|
|
878
|
-
// Note: For async constructors, use: await
|
|
852
|
+
```ts
|
|
853
|
+
const cloned = utils.clone(instance);
|
|
854
|
+
// Note: For async constructors, use: await utils.clone(instance)
|
|
879
855
|
```
|
|
880
856
|
|
|
881
|
-
####
|
|
857
|
+
#### `utils.fork(instance)`
|
|
882
858
|
|
|
883
|
-
|
|
859
|
+
Returns a fork constructor for the instance. Call it with optional new
|
|
860
|
+
arguments to create a forked instance from the same parent.
|
|
884
861
|
|
|
885
|
-
```
|
|
886
|
-
const
|
|
887
|
-
const
|
|
888
|
-
|
|
862
|
+
```ts
|
|
863
|
+
const forkFn = utils.fork(instance);
|
|
864
|
+
const forked = forkFn(); // same args
|
|
865
|
+
const forkedWithNewArgs = forkFn('new', 'args');
|
|
866
|
+
// Note: For async constructors, use: await forkFn(...)
|
|
889
867
|
```
|
|
890
868
|
|
|
891
|
-
####
|
|
869
|
+
#### `utils.fork(instance).call(thisArg, ...args)` / `.apply(thisArg, args)`
|
|
892
870
|
|
|
893
871
|
Forks with a different `this` context (useful for Directed Acyclic Graphs).
|
|
894
872
|
|
|
895
|
-
```
|
|
896
|
-
const
|
|
873
|
+
```ts
|
|
874
|
+
const forkFn = utils.fork(instanceA);
|
|
875
|
+
const dagInstance = forkFn.call(instanceB, 'args');
|
|
897
876
|
```
|
|
898
877
|
|
|
899
|
-
####
|
|
878
|
+
#### `utils.exception(instance, error, ...args)`
|
|
900
879
|
|
|
901
|
-
Creates an exception instance from the
|
|
880
|
+
Creates an exception instance from the given instance.
|
|
902
881
|
|
|
903
|
-
```
|
|
904
|
-
const error =
|
|
882
|
+
```ts
|
|
883
|
+
const error = utils.exception(someInstance, new Error('Something went wrong'));
|
|
905
884
|
throw error;
|
|
906
885
|
```
|
|
907
886
|
|
|
908
|
-
####
|
|
887
|
+
#### `utils.sibling(instance)`
|
|
909
888
|
|
|
910
|
-
Access sibling types from the same collection.
|
|
889
|
+
Access sibling types from the same collection. Returns a callable/proxy object
|
|
890
|
+
that resolves to `TypeClass | undefined`.
|
|
911
891
|
|
|
912
|
-
```
|
|
913
|
-
const
|
|
914
|
-
const
|
|
892
|
+
```ts
|
|
893
|
+
const siblingAccessor = utils.sibling(instance);
|
|
894
|
+
const siblingType = siblingAccessor('OtherType');
|
|
895
|
+
const sibling = siblingAccessor.OtherType;
|
|
915
896
|
```
|
|
916
897
|
|
|
917
898
|
---
|
|
@@ -930,7 +911,7 @@ All instances have non-enumerable internal properties:
|
|
|
930
911
|
| `.__stack__` | `string` | Stack trace (if `submitStack: true` in config) |
|
|
931
912
|
| `.__creator__` | `InstanceCreatorContext` | Instance creator reference |
|
|
932
913
|
| `.__timestamp__` | `number` | Creation timestamp (ms since epoch) |
|
|
933
|
-
| `.__self__` | `object` | Self reference to the instance
|
|
914
|
+
| `.__self__` | `object` | Self reference to the instance |
|
|
934
915
|
|
|
935
916
|
---
|
|
936
917
|
|
|
@@ -938,54 +919,28 @@ All instances have non-enumerable internal properties:
|
|
|
938
919
|
|
|
939
920
|
Access via `utils` export:
|
|
940
921
|
|
|
941
|
-
```
|
|
922
|
+
```ts
|
|
942
923
|
const { utils } = require('mnemonica');
|
|
943
924
|
```
|
|
944
925
|
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
#### `utils.pick(instance, ...keys)`
|
|
949
|
-
Standalone pick function.
|
|
950
|
-
|
|
951
|
-
#### `utils.parent(instance, constructorName?)`
|
|
952
|
-
Standalone parent function.
|
|
953
|
-
|
|
954
|
-
#### `utils.parse(instance)`
|
|
955
|
-
Parses an instance structure, returning:
|
|
956
|
-
- `name`: constructor name
|
|
957
|
-
- `props`: extracted properties
|
|
958
|
-
- `self`: the instance itself
|
|
959
|
-
- `proto`: prototype object
|
|
960
|
-
- `joint`: prototype properties
|
|
961
|
-
- `parent`: parent prototype
|
|
926
|
+
All utilities infer their type parameter from the instance argument; no
|
|
927
|
+
explicit `<T>` cast is required for ordinary use.
|
|
962
928
|
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
```js
|
|
980
|
-
const json = utils.toJSON(instance);
|
|
981
|
-
```
|
|
982
|
-
|
|
983
|
-
#### `utils.collectConstructors(instance, flat?)`
|
|
984
|
-
Collects all constructors in the instance's prototype chain.
|
|
985
|
-
|
|
986
|
-
```js
|
|
987
|
-
const constructors = utils.collectConstructors(instance, true);
|
|
988
|
-
```
|
|
929
|
+
| Utility | Inferred return type |
|
|
930
|
+
|---|---|
|
|
931
|
+
| `utils.extract(instance)` | `Extracted<T>` — plain user-field object |
|
|
932
|
+
| `utils.pick(instance, 'a', 'b')` | `{ a: T['a'], b: T['b'] }` |
|
|
933
|
+
| `utils.clone(instance)` | `T` |
|
|
934
|
+
| `utils.fork(instance)` | `(this: object, ...args: unknown[]) => T` |
|
|
935
|
+
| `utils.parent(instance, path?)` | `object \| undefined` |
|
|
936
|
+
| `utils.sibling(instance)` | `SiblingAccessor` |
|
|
937
|
+
| `utils.merge(A, B, ...args)` | `InstanceResult<Merge<B, A>, constructorOptions>` |
|
|
938
|
+
| `utils.parse(instance)` | `Parsed<T>` |
|
|
939
|
+
| `utils.toJSON(instance)` | `string` |
|
|
940
|
+
| `utils.collectConstructors(instance, flat?)` | `(CallableFunction \| string)[]` |
|
|
941
|
+
|
|
942
|
+
For the full type-level explanation, helper types, and examples, see
|
|
943
|
+
[`docs/UTILS.md`](./docs/UTILS.md).
|
|
989
944
|
|
|
990
945
|
---
|
|
991
946
|
|
|
@@ -1079,10 +1034,11 @@ errors.WRONG_STACK_CLEANER
|
|
|
1079
1034
|
|
|
1080
1035
|
#### Exception Instances
|
|
1081
1036
|
|
|
1082
|
-
When creating exceptions using `
|
|
1037
|
+
When creating exceptions using `utils.exception()`:
|
|
1083
1038
|
|
|
1084
1039
|
```js
|
|
1085
|
-
const
|
|
1040
|
+
const { utils } = require('mnemonica');
|
|
1041
|
+
const error = utils.exception(instance, new Error('Original error'));
|
|
1086
1042
|
|
|
1087
1043
|
// Properties:
|
|
1088
1044
|
error.originalError // The original error
|
|
@@ -1129,8 +1085,7 @@ define('SomeType', function () {}, {
|
|
|
1129
1085
|
submitStack: false, // Collect stack trace as __stack__ property
|
|
1130
1086
|
awaitReturn: true, // Ensure await new Constructor() returns value
|
|
1131
1087
|
ModificationConstructor: fn, // Custom modification constructor
|
|
1132
|
-
asClass: false
|
|
1133
|
-
exposeInstanceMethods: true // Expose instance methods (default: true for backward compatibility)
|
|
1088
|
+
asClass: false // Force class mode (auto-detected by default)
|
|
1134
1089
|
});
|
|
1135
1090
|
```
|
|
1136
1091
|
|
|
@@ -1143,39 +1098,27 @@ define('SomeType', function () {}, {
|
|
|
1143
1098
|
| `submitStack` | `boolean` | `false` | If `true`, collects stack trace and stores as `__stack__` property on instances. |
|
|
1144
1099
|
| `awaitReturn` | `boolean` | `true` | For async constructors, ensures `await new Constructor()` returns the instance. |
|
|
1145
1100
|
| `asClass` | `boolean` | `auto` | Force class mode detection. Usually auto-detected from constructor syntax. |
|
|
1146
|
-
| `exposeInstanceMethods` | `boolean` | `true` | Expose instance methods on the instance. Set to `false` to hide from TypeScript types. See details below. |
|
|
1147
1101
|
| `ModificationConstructor` | `Function` | - | Custom constructor function for internal instance modification. |
|
|
1148
1102
|
|
|
1149
|
-
###
|
|
1103
|
+
### Instance Method Opt-In
|
|
1150
1104
|
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
| Value | Behavior |
|
|
1154
|
-
|-------|----------|
|
|
1155
|
-
| `true` (default) | All instance methods are available directly on instances |
|
|
1156
|
-
| `false` | Methods are hidden from TypeScript types but still accessible via prototype chain |
|
|
1157
|
-
|
|
1158
|
-
**Use Case:** Set to `false` when you want a cleaner public API and don't want internal mnemonica methods cluttering autocomplete/IntelliSense.
|
|
1105
|
+
Starting from v1.0.6 instance methods are **not** exposed on instances by default. Use the standalone `utils` export or attach the methods to your constructor's prototype before calling `define()`:
|
|
1159
1106
|
|
|
1160
1107
|
```typescript
|
|
1161
|
-
import { define,
|
|
1162
|
-
|
|
1163
|
-
// With exposeInstanceMethods: false
|
|
1164
|
-
const CleanType = define('CleanType', function (data) {
|
|
1165
|
-
Object.assign(this, data);
|
|
1166
|
-
}, { exposeInstanceMethods: false });
|
|
1167
|
-
|
|
1168
|
-
const instance = new CleanType({ value: 42 });
|
|
1108
|
+
import { define, utils } from 'mnemonica';
|
|
1169
1109
|
|
|
1170
|
-
//
|
|
1171
|
-
|
|
1110
|
+
// Preferred: standalone utilities
|
|
1111
|
+
const instance = new MyType({ value: 42 });
|
|
1112
|
+
utils.extract(instance);
|
|
1172
1113
|
|
|
1173
|
-
//
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
utils.extract(
|
|
1114
|
+
// Legacy opt-in: attach methods to the constructor prototype before define()
|
|
1115
|
+
function MyType(data) {
|
|
1116
|
+
Object.assign(this, data);
|
|
1117
|
+
}
|
|
1118
|
+
Object.defineProperty(MyType.prototype, 'extract', {
|
|
1119
|
+
get() { return () => utils.extract(this); }
|
|
1120
|
+
});
|
|
1121
|
+
const MyTypeWithMethods = define('MyType', MyType);
|
|
1179
1122
|
```
|
|
1180
1123
|
|
|
1181
1124
|
### Override Default Config for Collection
|
|
@@ -1299,7 +1242,8 @@ const usingReactAsProto = ReactDOOMed.call(ReactDOM);
|
|
|
1299
1242
|
|
|
1300
1243
|
```js
|
|
1301
1244
|
// Fork from different parent
|
|
1302
|
-
const
|
|
1245
|
+
const { utils: { fork } } = require('mnemonica');
|
|
1246
|
+
const dagInstance = fork(instanceA).call(instanceB, 'args');
|
|
1303
1247
|
|
|
1304
1248
|
// Or use merge utility
|
|
1305
1249
|
const { utils: { merge } } = require('mnemonica');
|