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.
Files changed (89) hide show
  1. package/.ai/ONBOARDING.md +1 -1
  2. package/.ai/ask/AGENTS.md +15 -9
  3. package/.ai/rules-skill/contributing.md +1 -1
  4. package/.ai/rules-skill/define-patterns.md +8 -2
  5. package/.ai/rules-skill/ecosystem.md +4 -4
  6. package/.ai/rules-skill/instance-methods.md +48 -15
  7. package/.ai/rules-skill/type-system.md +12 -2
  8. package/AGENTS.md +6 -2
  9. package/CONTRIBUTING.md +7 -1
  10. package/FOR_HUMANS.md +143 -199
  11. package/README.md +74 -39
  12. package/build/api/errors/exceptionConstructor.js +20 -12
  13. package/build/api/errors/index.js +9 -5
  14. package/build/api/errors/throwModificationError.js +15 -9
  15. package/build/api/index.js +35 -2
  16. package/build/api/types/InstanceCreator.js +5 -2
  17. package/build/api/types/Mnemosyne.d.ts +6 -6
  18. package/build/api/types/Mnemosyne.js +43 -120
  19. package/build/api/types/Props.js +5 -3
  20. package/build/api/types/TypeProxy.js +13 -9
  21. package/build/api/types/compileNewModificatorFunctionBody.js +14 -8
  22. package/build/api/types/index.js +56 -13
  23. package/build/api/utils/index.js +21 -12
  24. package/build/constants/index.js +11 -8
  25. package/build/descriptors/types/index.js +79 -26
  26. package/build/index.d.ts +4 -4
  27. package/build/index.js +62 -15
  28. package/build/types/index.d.ts +52 -31
  29. package/build/types/index.js +1 -1
  30. package/build/utils/clone.d.ts +1 -0
  31. package/build/utils/clone.js +11 -0
  32. package/build/utils/collectConstructors.js +5 -3
  33. package/build/utils/exception.d.ts +1 -0
  34. package/build/utils/exception.js +14 -0
  35. package/build/utils/extract.d.ts +2 -3
  36. package/build/utils/extract.js +3 -2
  37. package/build/utils/fork.d.ts +1 -0
  38. package/build/utils/fork.js +33 -0
  39. package/build/utils/index.d.ts +2 -3
  40. package/build/utils/index.js +21 -7
  41. package/build/utils/merge.d.ts +2 -1
  42. package/build/utils/merge.js +10 -6
  43. package/build/utils/parent.d.ts +1 -1
  44. package/build/utils/parent.js +3 -2
  45. package/build/utils/parse.d.ts +2 -12
  46. package/build/utils/parse.js +1 -1
  47. package/build/utils/pick.d.ts +4 -3
  48. package/build/utils/pick.js +4 -5
  49. package/build/utils/sibling.d.ts +1 -0
  50. package/build/utils/sibling.js +25 -0
  51. package/build/utils/toJSON.d.ts +1 -1
  52. package/build/utils/toJSON.js +3 -2
  53. package/docs/UTILS.md +184 -0
  54. package/docs/ai-learning-trajectory.md +1 -1
  55. package/docs/async-constructors.md +3 -1
  56. package/docs/empathy-in-ai.md +170 -0
  57. package/docs/hott-primer.md +47 -0
  58. package/docs/performance-vs-security.md +2 -2
  59. package/docs/purpose.md +38 -7
  60. package/docs/tactica-pattern.md +10 -10
  61. package/docs/theory-of-operations.md +224 -0
  62. package/docs/typed-lookup.md +12 -12
  63. package/package.json +12 -7
  64. package/src/api/errors/exceptionConstructor.ts +14 -9
  65. package/src/api/errors/index.ts +8 -4
  66. package/src/api/errors/throwModificationError.ts +10 -9
  67. package/src/api/types/InstanceCreator.ts +5 -8
  68. package/src/api/types/Mnemosyne.ts +72 -231
  69. package/src/api/types/Props.ts +4 -2
  70. package/src/api/types/TypeProxy.ts +7 -11
  71. package/src/api/types/compileNewModificatorFunctionBody.ts +13 -7
  72. package/src/api/types/index.ts +15 -8
  73. package/src/api/utils/index.ts +16 -14
  74. package/src/constants/index.ts +6 -9
  75. package/src/descriptors/types/index.ts +44 -24
  76. package/src/index.ts +28 -21
  77. package/src/types/index.ts +101 -69
  78. package/src/utils/clone.ts +11 -0
  79. package/src/utils/collectConstructors.ts +4 -2
  80. package/src/utils/exception.ts +16 -0
  81. package/src/utils/extract.ts +5 -2
  82. package/src/utils/fork.ts +57 -0
  83. package/src/utils/index.ts +32 -13
  84. package/src/utils/merge.ts +18 -6
  85. package/src/utils/parent.ts +4 -6
  86. package/src/utils/parse.ts +5 -4
  87. package/src/utils/pick.ts +10 -2
  88. package/src/utils/sibling.ts +33 -0
  89. package/src/utils/toJSON.ts +3 -2
@@ -9,17 +9,19 @@ const {
9
9
  } = constants;
10
10
 
11
11
  const getAdditor = (constructors: string[] | { [index: string]: boolean }) => {
12
- return Array.isArray(constructors) ?
12
+ const result = Array.isArray(constructors) ?
13
13
  (name: string) => {
14
14
  constructors.push(name);
15
15
  } : (name: string) => {
16
16
  constructors[ name ] = true;
17
17
  };
18
+ return result;
18
19
 
19
20
  };
20
21
 
21
22
  const getAccumulator = (asSequence: boolean) => {
22
- return asSequence ? [] : {};
23
+ const result = asSequence ? [] : {};
24
+ return result;
23
25
  };
24
26
 
25
27
  export const collectConstructors = (self: object, asSequence = false) => {
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ import exceptionConstructor from '../api/errors/exceptionConstructor';
4
+
5
+ export const exception = function (instance: object, error: Error, ...args: unknown[]) {
6
+
7
+ const target = new.target;
8
+ const exceptionResult = exceptionConstructor.call(
9
+ instance,
10
+ target,
11
+ error,
12
+ ...args
13
+ );
14
+ return exceptionResult;
15
+
16
+ };
@@ -3,9 +3,11 @@
3
3
  import { ErrorsTypes } from '../descriptors/errors';
4
4
  const { WRONG_INSTANCE_INVOCATION } = ErrorsTypes;
5
5
 
6
+ import type { Extracted } from '../types';
7
+
6
8
  // import { hop } from './hop';
7
9
 
8
- export const extract = ( instance: object ) => {
10
+ export const extract = <T extends object>( instance: T ): Extracted<T> => {
9
11
 
10
12
  // at this situation this check is enough
11
13
  if ( instance !== Object( instance ) ) {
@@ -25,6 +27,7 @@ export const extract = ( instance: object ) => {
25
27
  extracted[ name ] = ( instance as Record<string, unknown> )[ name ];
26
28
  }
27
29
 
28
- return extracted;
30
+ const result = extracted as Extracted<T>;
31
+ return result;
29
32
 
30
33
  };
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ import {
4
+ getProps, Props
5
+ } from '../api/types/Props';
6
+ import { InstanceCreator } from '../api/types/InstanceCreator';
7
+ import TypesUtils from '../api/utils/index';
8
+
9
+ const { reflectPrimitiveWrappers } = TypesUtils;
10
+
11
+ export const fork = <T extends object>(instance: T): (this: object, ...forkArgs: unknown[]) => T => {
12
+
13
+ const props = getProps(instance) as Props;
14
+
15
+ const {
16
+ __type__: type,
17
+ __collection__: collection,
18
+ __parent__: existentInstance,
19
+ __args__,
20
+ __self__,
21
+ } = props;
22
+
23
+ const {
24
+ isSubType,
25
+ TypeName
26
+ } = type;
27
+
28
+ const result = function (this: object, ...forkArgs: unknown[]) {
29
+
30
+ let forked;
31
+ const Constructor = isSubType ?
32
+ existentInstance :
33
+ collection;
34
+
35
+ const args = forkArgs.length ? forkArgs : __args__;
36
+
37
+
38
+ if (this === __self__) {
39
+
40
+ // @ts-expect-error this is definitely a constructor
41
+ forked = new (Constructor[ TypeName ])(...args);
42
+ } else {
43
+ // fork.call ? let's do it !
44
+ forked = new InstanceCreator(
45
+ type,
46
+ reflectPrimitiveWrappers(this),
47
+ args
48
+ );
49
+ }
50
+
51
+ const forkResult = forked as T;
52
+ return forkResult;
53
+
54
+ };
55
+ return result;
56
+
57
+ };
@@ -1,11 +1,18 @@
1
1
  'use strict';
2
2
 
3
- import type { WrappableMethod } from '../types';
3
+ import type {
4
+ WrappableMethod,
5
+ UtilsCollection
6
+ } from '../types';
4
7
 
5
8
  import { collectConstructors } from './collectConstructors';
6
9
  import { extract } from './extract';
7
10
  import { parent } from './parent';
8
11
  import { pick } from './pick';
12
+ import { sibling } from './sibling';
13
+ import { exception } from './exception';
14
+ import { fork } from './fork';
15
+ import { clone } from './clone';
9
16
  import { toJSON } from './toJSON';
10
17
  import { parse } from './parse';
11
18
  import { merge } from './merge';
@@ -16,29 +23,41 @@ const utilsUnWrapped = {
16
23
  pick,
17
24
 
18
25
  parent,
26
+ sibling,
27
+ exception,
28
+ fork,
29
+ clone,
19
30
 
20
31
  toJSON,
21
32
 
22
33
  parse,
23
34
  merge,
24
35
 
25
- get collectConstructors () {
26
- return collectConstructors;
27
- },
36
+ collectConstructors,
28
37
 
29
38
  };
30
39
 
31
40
  const wrapThis = ( method: WrappableMethod ) => {
32
- return function ( this: object, instance: object | undefined, ...args: unknown[] ) {
33
- const result = method(
34
- instance !== undefined ? instance : this,
35
- ...args
36
- );
37
- return result;
41
+ const result = function ( this: object, instance: object | undefined, ...args: unknown[] ) {
42
+ const instanceContext = instance !== undefined ? instance : this;
43
+ let wrapResult: unknown;
44
+ if ( new.target ) {
45
+ wrapResult = new (method as unknown as new (...a: unknown[]) => unknown)(
46
+ instanceContext,
47
+ ...args
48
+ );
49
+ } else {
50
+ wrapResult = method(
51
+ instanceContext,
52
+ ...args
53
+ );
54
+ }
55
+ return wrapResult;
38
56
  };
57
+ return result;
39
58
  };
40
59
 
41
- export const utils: { [ index: string ]: CallableFunction } = {
60
+ export const utils = {
42
61
 
43
62
  ...Object.entries( utilsUnWrapped )
44
63
  .reduce(
@@ -47,9 +66,9 @@ export const utils: { [ index: string ]: CallableFunction } = {
47
66
  methods[ name ] = wrapThis( fn );
48
67
  return methods;
49
68
  },
50
- {}
69
+ {}
51
70
  ),
52
71
 
53
- };
72
+ } as UtilsCollection;
54
73
 
55
74
  export { defineStackCleaner } from './defineStackCleaner';
@@ -2,8 +2,18 @@
2
2
 
3
3
  import { ErrorsTypes } from '../descriptors/errors';
4
4
  const { WRONG_ARGUMENTS_USED } = ErrorsTypes;
5
+ import { fork } from './fork';
6
+ import { getProps } from '../api/types/Props';
7
+ import type {
8
+ Merge,
9
+ InstanceResult,
10
+ } from '../types';
5
11
 
6
- export const merge = ( a: object, b: object, ...args: unknown[] ): object => {
12
+ export const merge = <A extends object, B extends object>(
13
+ a: A,
14
+ b: B,
15
+ ...args: unknown[]
16
+ ): InstanceResult<Merge<B, A>> => {
7
17
 
8
18
  // at this situation this check is enough
9
19
  if ( a !== Object( a ) ) {
@@ -15,15 +25,17 @@ export const merge = ( a: object, b: object, ...args: unknown[] ): object => {
15
25
  throw new WRONG_ARGUMENTS_USED( 'B should be an object' );
16
26
  }
17
27
 
18
- const { fork } = a as { fork?: (this: object, ...forkArgs: unknown[]) => unknown };
19
- if ( typeof fork !== 'function' ) {
20
- throw new WRONG_ARGUMENTS_USED( 'A should have A.fork()' );
28
+ const props = getProps(a);
29
+ if (props === undefined) {
30
+ throw new WRONG_ARGUMENTS_USED('A should be a mnemonica instance');
21
31
  }
22
32
 
23
- const aa = fork.call(
33
+ const forked = fork(a);
34
+ const aa = forked.call(
24
35
  b,
25
36
  ...args
26
37
  );
27
- return aa as object;
38
+ const result = aa as InstanceResult<Merge<B, A>>;
39
+ return result;
28
40
 
29
41
  };
@@ -10,7 +10,7 @@ import {
10
10
  // seek for firts parent instance
11
11
  // of instance prototype chain
12
12
  // with constructors of path
13
- export const parent = ( instance: object, path?: string ): unknown => {
13
+ export const parent = <T extends object>( instance: T, path?: string ): object | undefined => {
14
14
 
15
15
  // at this situation this check is enough
16
16
  if ( instance !== Object( instance ) ) {
@@ -33,10 +33,8 @@ export const parent = ( instance: object, path?: string ): unknown => {
33
33
 
34
34
  // seek throuh parent instances
35
35
  // about the fist constructor with this name
36
- return name === path ?
37
- p : parent(
38
- p as object,
39
- path
40
- );
36
+ const result = name === path ?
37
+ p : parent( p as object, path );
38
+ return result;
41
39
 
42
40
  };
@@ -14,8 +14,9 @@ const {
14
14
  // } = constants;
15
15
 
16
16
  import { extract } from './extract';
17
+ import type { Parsed } from '../types';
17
18
 
18
- export const parse = ( self: object ) => {
19
+ export const parse = <T extends object>( self: T ): Parsed<T> => {
19
20
 
20
21
  if ( !self || !( self as { constructor?: CallableFunction } ).constructor ) {
21
22
  throw new WRONG_MODIFICATION_PATTERN;
@@ -47,7 +48,7 @@ export const parse = ( self: object ) => {
47
48
 
48
49
  const { name } = protoConstructor;
49
50
 
50
- const props = extract( { ...self } as Record<string, unknown> );
51
+ const props = extract( { ...self } as T );
51
52
  // props.constructor = undefined;
52
53
  delete ( props as { constructor?: unknown } ).constructor;
53
54
 
@@ -57,7 +58,7 @@ export const parse = ( self: object ) => {
57
58
  ) as Record<string, unknown> );
58
59
  delete ( joint as { constructor?: unknown } ).constructor;
59
60
 
60
- const parent = protoProto;
61
+ const parent = protoProto as object | undefined;
61
62
  // TODO: deep parse
62
63
  // let parent;
63
64
  // if ( protoProto[ SymbolConstructorName ] === MNEMONICA ) {
@@ -66,7 +67,7 @@ export const parse = ( self: object ) => {
66
67
  // parent = Reflect.getPrototypeOf( protoProto );
67
68
  // }
68
69
 
69
- const result = {
70
+ const result: Parsed<T> = {
70
71
 
71
72
  name,
72
73
 
package/src/utils/pick.ts CHANGED
@@ -3,7 +3,15 @@
3
3
  import { ErrorsTypes } from '../descriptors/errors';
4
4
  const { WRONG_INSTANCE_INVOCATION } = ErrorsTypes;
5
5
 
6
- export const pick = ( instance: object, ...args: (string | string[])[] ) => {
6
+ export function pick <T extends object, K extends keyof T> (
7
+ instance: T,
8
+ ...args: (K | K[])[]
9
+ ): { [P in K]: T[P] } & {};
10
+ export function pick <T extends object> (
11
+ instance: T,
12
+ ...args: (string | string[])[]
13
+ ): Record<string, unknown>;
14
+ export function pick ( instance: object, ...args: (string | string[])[] ) {
7
15
 
8
16
  // at this situation this check is enough
9
17
  if ( instance !== Object( instance ) ) {
@@ -33,4 +41,4 @@ export const pick = ( instance: object, ...args: (string | string[])[] ) => {
33
41
 
34
42
  return picked;
35
43
 
36
- };
44
+ }
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ import {
4
+ getProps,
5
+ Props
6
+ } from '../api/types/Props';
7
+
8
+ export const sibling = (instance: object): unknown => {
9
+
10
+ const siblings = (SiblingTypeName: string) => {
11
+ const props = getProps(instance) as Props;
12
+ const { __collection__: collection, } = props;
13
+ const answer = collection[ SiblingTypeName ];
14
+ return answer;
15
+ };
16
+
17
+ const result = new Proxy(
18
+ siblings,
19
+ {
20
+ get (_, prop: string) {
21
+ const proxyResult = siblings(prop);
22
+ return proxyResult;
23
+ },
24
+ apply (_, __, args,) {
25
+ const proxyResult = siblings(args[ 0 ]);
26
+ return proxyResult;
27
+ }
28
+ }
29
+ );
30
+
31
+ return result;
32
+
33
+ };
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
3
  import { extract } from './extract';
4
- export const toJSON = ( instance: object ) => {
4
+ export const toJSON = <T extends object>( instance: T ) => {
5
5
 
6
6
  const extracted = extract( instance );
7
- return Object.entries( extracted ).reduce(
7
+ const result = Object.entries( extracted ).reduce(
8
8
  ( o: string, entry: [ string, unknown ] ) => {
9
9
 
10
10
  const [ name, _value ] = entry;
@@ -40,5 +40,6 @@ export const toJSON = ( instance: object ) => {
40
40
  /,$/,
41
41
  '}'
42
42
  );
43
+ return result;
43
44
 
44
45
  };