mnemonica 1.3.5 → 1.3.7
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/FOR_HUMANS.md +6 -1
- package/README.md +1 -1
- package/SKILL.md +11 -1
- package/build/api/types/InstanceCreator.js +21 -3
- package/build/api/types/index.js +1 -1
- package/build/index.d.ts +4 -4
- package/build/index.js +1 -1
- package/build/types/index.d.ts +30 -16
- package/build/types/index.js +1 -1
- package/build/utils/deepParse.d.ts +2 -0
- package/build/utils/deepParse.js +21 -0
- package/build/utils/index.js +3 -1
- package/build/utils/parent.d.ts +1 -1
- package/build/utils/parent.js +9 -4
- package/build/utils/parse.d.ts +3 -2
- package/build/utils/parse.js +19 -6
- package/build/utils/toJSON.js +21 -18
- package/docs/UTILS.md +46 -8
- package/docs/typed-lookup.md +101 -0
- package/package.json +135 -134
- package/src/api/types/InstanceCreator.ts +25 -2
- package/src/api/types/index.ts +3 -3
- package/src/index.ts +16 -4
- package/src/types/index.ts +75 -20
- package/src/utils/deepParse.ts +28 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/parent.ts +20 -5
- package/src/utils/parse.ts +42 -6
- package/src/utils/toJSON.ts +35 -36
package/src/index.ts
CHANGED
|
@@ -19,9 +19,9 @@ import type {
|
|
|
19
19
|
InstanceResult,
|
|
20
20
|
Merge,
|
|
21
21
|
TypeLookup,
|
|
22
|
-
LookupResult,
|
|
23
22
|
RegistryHolderBase,
|
|
24
|
-
|
|
23
|
+
RegistryEntry,
|
|
24
|
+
LookedUpConstructor
|
|
25
25
|
} from './types';
|
|
26
26
|
|
|
27
27
|
import TypesUtils from './api/utils/index';
|
|
@@ -55,6 +55,12 @@ export type {
|
|
|
55
55
|
LookupResult,
|
|
56
56
|
RegistryOf,
|
|
57
57
|
MnemonicaModule,
|
|
58
|
+
// Deliberate public vocabulary for builder-mode consumers (TS 6
|
|
59
|
+
// declaration emit names ONLY entry exports; these three stand in
|
|
60
|
+
// for the internal registry/lookup machinery):
|
|
61
|
+
RegistryEntry,
|
|
62
|
+
LookedUpConstructor,
|
|
63
|
+
LookedUpInstance,
|
|
58
64
|
} from './types';
|
|
59
65
|
|
|
60
66
|
/**
|
|
@@ -75,6 +81,12 @@ export type {
|
|
|
75
81
|
* Once augmented, `lookup('MyType')` and `utils.parent(instance, 'MyType')`
|
|
76
82
|
* resolve keys against this registry. Unaugmented keys fall back to the broad
|
|
77
83
|
* `TypeClass | undefined` return type.
|
|
84
|
+
*
|
|
85
|
+
* Internal on purpose: if you are here from a TS2883 error
|
|
86
|
+
* ("cannot be named without a reference to 'GlobalRegistry'"), the project
|
|
87
|
+
* exports free define() results without merging them into this registry.
|
|
88
|
+
* The error is the guard rail, not a missing export — see
|
|
89
|
+
* docs/typed-lookup.md, "Declaration emit on TypeScript 6".
|
|
78
90
|
*/
|
|
79
91
|
export interface TypeRegistry {
|
|
80
92
|
// Intentionally empty. Augment this interface via declaration merging
|
|
@@ -136,7 +148,7 @@ export function define <
|
|
|
136
148
|
): IDefinitorInstance<
|
|
137
149
|
F,
|
|
138
150
|
InstanceResult<F>,
|
|
139
|
-
Reg & Record<ChildPath,
|
|
151
|
+
Reg & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
140
152
|
ChildPath
|
|
141
153
|
>;
|
|
142
154
|
export function define <
|
|
@@ -264,7 +276,7 @@ export function lookup<Reg extends object, const K extends keyof Reg & string>(
|
|
|
264
276
|
// resolves against the registry carried by a builder/collection value
|
|
265
277
|
source: { lookup: TypeLookup<Reg> },
|
|
266
278
|
TypeNestedPath: K
|
|
267
|
-
):
|
|
279
|
+
): LookedUpConstructor<Reg, K>;
|
|
268
280
|
export function lookup(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
269
281
|
export function lookup(
|
|
270
282
|
source: { lookup: (path: string) => TypeClass | undefined },
|
package/src/types/index.ts
CHANGED
|
@@ -280,10 +280,18 @@ export type TypeDef = {
|
|
|
280
280
|
[Symbol.hasInstance]: (instance: object) => boolean;
|
|
281
281
|
};
|
|
282
282
|
|
|
283
|
+
// Runtime lookup contract of a collection: a dotted path in, a registered
|
|
284
|
+
// TypeClass (or undefined) out. Internal code uses this, not the public
|
|
285
|
+
// TypeLookup — with the default GlobalRegistry (a string index) a plain
|
|
286
|
+
// `string` path would match TypeLookup's typed overload and need a cast back.
|
|
287
|
+
export interface CollectionLookup extends CallableFunction {
|
|
288
|
+
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
289
|
+
}
|
|
290
|
+
|
|
283
291
|
// Collection definition
|
|
284
292
|
export type CollectionDef = Hookable & {
|
|
285
293
|
define: TypeAbsorber;
|
|
286
|
-
lookup:
|
|
294
|
+
lookup: CollectionLookup;
|
|
287
295
|
subtypes: SubtypesMap;
|
|
288
296
|
[key: string]: unknown;
|
|
289
297
|
};
|
|
@@ -292,6 +300,10 @@ export type CollectionDef = Hookable & {
|
|
|
292
300
|
// Global registry shape used as the default for TypeLookup. It combines any
|
|
293
301
|
// augmented TypeRegistry entries with a broad string index signature so the
|
|
294
302
|
// generic TypeLookup constraint is satisfied.
|
|
303
|
+
// Internal on purpose: if you are here from a TS2883 error ("cannot be
|
|
304
|
+
// named without a reference to 'GlobalRegistry'"), the consumer exports free
|
|
305
|
+
// define() results without a TypeRegistry merge — see docs/typed-lookup.md,
|
|
306
|
+
// "Declaration emit on TypeScript 6". Do not re-export this.
|
|
295
307
|
export type GlobalRegistry = TypeRegistry & Record<string, TypeConstructorBase>;
|
|
296
308
|
|
|
297
309
|
// Extract the instance type returned by a mnemonica-compatible constructor.
|
|
@@ -311,9 +323,7 @@ export type SubTypeConstructors<
|
|
|
311
323
|
Path extends string
|
|
312
324
|
> = {
|
|
313
325
|
[K in keyof Registry as K extends `${Path}.${infer Child}` ? Child : never]:
|
|
314
|
-
|
|
315
|
-
lookup: NestedTypeLookup<Registry, K & string>;
|
|
316
|
-
};
|
|
326
|
+
LookedUpConstructor<Registry, K & string, true>;
|
|
317
327
|
};
|
|
318
328
|
|
|
319
329
|
// Reconstruct a constructor with a new instance type while preserving every
|
|
@@ -369,23 +379,45 @@ export type AugmentedConstructor<
|
|
|
369
379
|
CallConstructs extends boolean = false
|
|
370
380
|
> = ReplaceConstructorInstance<
|
|
371
381
|
Registry[Path],
|
|
372
|
-
|
|
382
|
+
LookedUpInstance<Registry, Path>,
|
|
373
383
|
CallConstructs
|
|
374
384
|
>;
|
|
375
385
|
|
|
386
|
+
// Instance type produced by a typed lookup (public vocabulary): the
|
|
387
|
+
// constructor's own instance shape plus child-constructor properties for
|
|
388
|
+
// every registered subtype. Referencing this name — instead of the internal
|
|
389
|
+
// WithSubTypes alias — keeps consumer declaration emit portable (TS names
|
|
390
|
+
// alias instantiations in emit; only entry-exported names are nameable).
|
|
391
|
+
export type LookedUpInstance<
|
|
392
|
+
Registry extends object,
|
|
393
|
+
Path extends string
|
|
394
|
+
> = WithSubTypes<ExtractConstructorInstance<Registry[Path & keyof Registry]>, Registry, Path>;
|
|
395
|
+
|
|
396
|
+
// Lookup result type (public vocabulary): the augmented constructor plus a
|
|
397
|
+
// lookup method scoped to the constructor's own type path. Non-conditional
|
|
398
|
+
// on purpose: a conditional alias resolves during inference and its
|
|
399
|
+
// expansion (internal helpers) would leak into consumer declaration emit.
|
|
400
|
+
// CallConstructs mirrors the SubTypeProxy distinction (true drops the this
|
|
401
|
+
// demand for instance-carried child constructors).
|
|
402
|
+
export type LookedUpConstructor<
|
|
403
|
+
Registry extends object,
|
|
404
|
+
Path extends keyof Registry & string,
|
|
405
|
+
CallConstructs extends boolean = false
|
|
406
|
+
> = AugmentedConstructor<Registry, Path, CallConstructs> & {
|
|
407
|
+
lookup: NestedTypeLookup<Registry, Path>;
|
|
408
|
+
};
|
|
409
|
+
|
|
376
410
|
// Result of a typed lookup: the augmented constructor plus a `lookup` method
|
|
377
411
|
// scoped to the constructor's own type path for relative subtype lookups.
|
|
378
412
|
export type LookupResult<
|
|
379
413
|
Registry extends object,
|
|
380
414
|
Path extends keyof Registry & string
|
|
381
415
|
> = Registry[Path] extends AnyConstructor
|
|
382
|
-
?
|
|
383
|
-
lookup: NestedTypeLookup<Registry, Path>;
|
|
384
|
-
}
|
|
416
|
+
? LookedUpConstructor<Registry, Path>
|
|
385
417
|
: never;
|
|
386
418
|
|
|
387
419
|
export interface TypeLookup<T extends object = GlobalRegistry> extends CallableFunction {
|
|
388
|
-
<const K extends keyof T & string>(this: unknown, TypeNestedPath: K):
|
|
420
|
+
<const K extends keyof T & string>(this: unknown, TypeNestedPath: K): LookedUpConstructor<T, K>;
|
|
389
421
|
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
390
422
|
}
|
|
391
423
|
|
|
@@ -429,13 +461,13 @@ export type NestedTypeLookup<
|
|
|
429
461
|
Path extends string = ''
|
|
430
462
|
> = (Path extends ''
|
|
431
463
|
? {
|
|
432
|
-
<const K extends keyof Registry & string>(this: unknown, TypeNestedPath: K):
|
|
464
|
+
<const K extends keyof Registry & string>(this: unknown, TypeNestedPath: K): LookedUpConstructor<Registry, K>;
|
|
433
465
|
}
|
|
434
466
|
: {
|
|
435
467
|
<const K extends RelativeKeys<Registry, Path> & string>(
|
|
436
468
|
this: unknown,
|
|
437
469
|
TypeNestedPath: K
|
|
438
|
-
):
|
|
470
|
+
): LookedUpConstructor<Registry, Extract<keyof Registry & string, `${Path}.${K}`>>;
|
|
439
471
|
}) & {
|
|
440
472
|
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
441
473
|
};
|
|
@@ -526,16 +558,35 @@ export type Extracted<T extends object> = {
|
|
|
526
558
|
// It is a one-level snapshot of the instance's prototype chain.
|
|
527
559
|
// `props` contains the instance's enumerable user properties;
|
|
528
560
|
// `joint` contains enumerable properties copied from the immediate prototype;
|
|
529
|
-
// `parent` is the
|
|
561
|
+
// `parent` is the parent INSTANCE — the same object utils.parent(instance)
|
|
562
|
+
// returns; null for no parent. Not recursively parsed — walking the whole
|
|
563
|
+
// lineage level by level is what utils.deepParse does — this function stays
|
|
564
|
+
// the one-level primitive.
|
|
565
|
+
// The three "no parent" readings, stated once:
|
|
566
|
+
// parent == null — no parent at all (covers both below);
|
|
567
|
+
// parent === null — the end of a REAL chain (a root instance);
|
|
568
|
+
// parent === undefined — nothing was given (utils.parse(null)).
|
|
530
569
|
export type Parsed<T extends object> = {
|
|
531
570
|
name: string;
|
|
532
571
|
props: Extracted<T>;
|
|
533
572
|
self: T;
|
|
534
573
|
proto: object;
|
|
535
574
|
joint: Record<string, unknown>;
|
|
536
|
-
parent: object |
|
|
575
|
+
parent: object | null;
|
|
537
576
|
};
|
|
538
577
|
|
|
578
|
+
// EmptyParsed: the shape utils.parse(null) returns — the one non-object
|
|
579
|
+
// input parse accepts. "Nothing was given": every field is its empty value,
|
|
580
|
+
// and parent is undefined (see the readings above).
|
|
581
|
+
export interface EmptyParsed {
|
|
582
|
+
name: undefined;
|
|
583
|
+
props: {};
|
|
584
|
+
self: null;
|
|
585
|
+
proto: undefined;
|
|
586
|
+
joint: {};
|
|
587
|
+
parent: undefined;
|
|
588
|
+
}
|
|
589
|
+
|
|
539
590
|
// Sibling type accessor
|
|
540
591
|
export interface SiblingAccessor {
|
|
541
592
|
(SiblingTypeName: string): TypeClass | undefined;
|
|
@@ -591,14 +642,16 @@ export type InstanceResult<
|
|
|
591
642
|
N extends object,
|
|
592
643
|
> = { [K in keyof N]: N[K] };
|
|
593
644
|
|
|
594
|
-
//
|
|
645
|
+
// The registry VALUE as public vocabulary: what a builder chain stores per
|
|
646
|
+
// defined path and what typed lookups read back. Interface (not alias) so
|
|
647
|
+
// consumer declaration emit names it instead of expanding the intersection.
|
|
595
648
|
// It preserves the dotted `Path` so that `.define()` on a looked-up constructor
|
|
596
649
|
// computes the correct child path, but it omits the full `GlobalRegistry`
|
|
597
650
|
// generic to keep hover tooltips readable.
|
|
598
|
-
export
|
|
651
|
+
export interface RegistryEntry<
|
|
599
652
|
F extends object,
|
|
600
653
|
Path extends string = ''
|
|
601
|
-
>
|
|
654
|
+
> extends _Internal_TC_<F>, RegistryHolderBase<{}, F, Path> {}
|
|
602
655
|
|
|
603
656
|
// Registry holder base - provides the accumulating .define() method.
|
|
604
657
|
// `Parent` tracks the instance type of the last defined constructor, so chained
|
|
@@ -632,7 +685,7 @@ export interface RegistryHolderBase<
|
|
|
632
685
|
): IDefinitorInstance<
|
|
633
686
|
F,
|
|
634
687
|
InstanceResult<F>,
|
|
635
|
-
T & Record<ChildPath,
|
|
688
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
636
689
|
ChildPath
|
|
637
690
|
>;
|
|
638
691
|
|
|
@@ -657,7 +710,7 @@ export interface RegistryHolderBase<
|
|
|
657
710
|
): IDefinitorInstance<
|
|
658
711
|
F,
|
|
659
712
|
InstanceResult<F>,
|
|
660
|
-
T & Record<ChildPath,
|
|
713
|
+
T & Record<ChildPath, RegistryEntry<F, ChildPath>>,
|
|
661
714
|
ChildPath
|
|
662
715
|
>;
|
|
663
716
|
}
|
|
@@ -790,7 +843,7 @@ export interface TypesCollection<
|
|
|
790
843
|
config?: constructorOptions
|
|
791
844
|
): <U extends Constructor<object>>(cstr: U) => DecoratedClass<
|
|
792
845
|
U,
|
|
793
|
-
T & Record<ConstructorName<U>,
|
|
846
|
+
T & Record<ConstructorName<U>, RegistryEntry<InstanceType<U>, ConstructorName<U>>>
|
|
794
847
|
>;
|
|
795
848
|
|
|
796
849
|
[key: string]: unknown;
|
|
@@ -935,7 +988,9 @@ export interface UtilsCollection {
|
|
|
935
988
|
...args: unknown[]
|
|
936
989
|
): InstanceResult<Merge<B, A>>;
|
|
937
990
|
parse<T extends object>(self: T): Parsed<T>;
|
|
938
|
-
|
|
991
|
+
parse(self: null): EmptyParsed;
|
|
992
|
+
deepParse<T extends object>(instance: T): Array<Parsed<T>>;
|
|
993
|
+
parent<T extends object>(instance: T): object | null;
|
|
939
994
|
parent<T extends object, K extends ParentPathOfInstance<T> & string>(
|
|
940
995
|
instance: T,
|
|
941
996
|
path: K
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import { parse } from './parse';
|
|
4
|
+
import type { Parsed } from '../types';
|
|
5
|
+
|
|
6
|
+
// the lineage walker (lineage-export plan, layer 2): from the instance to
|
|
7
|
+
// its root through the REAL parent instances — parse().parent hops directly
|
|
8
|
+
// from instance to instance, so the per-type prototype layers between them
|
|
9
|
+
// never enter the picture. The result is the ordered list of parsed levels,
|
|
10
|
+
// index 0 being the instance itself, the last level its root (parent null).
|
|
11
|
+
export const deepParse = <T extends object>( instance: T ): Array<Parsed<T>> => {
|
|
12
|
+
|
|
13
|
+
const levels: Array<Parsed<object>> = [];
|
|
14
|
+
let current: object = instance;
|
|
15
|
+
for ( ;; ) {
|
|
16
|
+
const parsed: Parsed<object> = parse( current );
|
|
17
|
+
levels.push( parsed );
|
|
18
|
+
const next: object | null = parsed.parent;
|
|
19
|
+
if ( next === null ) {
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
current = next;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const result = levels as Array<Parsed<T>>;
|
|
26
|
+
return result;
|
|
27
|
+
|
|
28
|
+
};
|
package/src/utils/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ import { fork } from './fork';
|
|
|
15
15
|
import { clone } from './clone';
|
|
16
16
|
import { toJSON } from './toJSON';
|
|
17
17
|
import { parse } from './parse';
|
|
18
|
+
import { deepParse } from './deepParse';
|
|
18
19
|
import { merge } from './merge';
|
|
19
20
|
|
|
20
21
|
const utilsUnWrapped = {
|
|
@@ -31,6 +32,7 @@ const utilsUnWrapped = {
|
|
|
31
32
|
toJSON,
|
|
32
33
|
|
|
33
34
|
parse,
|
|
35
|
+
deepParse,
|
|
34
36
|
merge,
|
|
35
37
|
|
|
36
38
|
collectConstructors,
|
package/src/utils/parent.ts
CHANGED
|
@@ -15,13 +15,13 @@ import {
|
|
|
15
15
|
// seek for firts parent instance
|
|
16
16
|
// of instance prototype chain
|
|
17
17
|
// with constructors of path
|
|
18
|
-
export function parent <T extends object> (instance: T): object |
|
|
18
|
+
export function parent <T extends object> (instance: T): object | null;
|
|
19
19
|
export function parent <T extends object, K extends ParentPathOfInstance<T> & string> (
|
|
20
20
|
instance: T,
|
|
21
21
|
path: K
|
|
22
22
|
): InstanceOfTypeRegistry<K> | undefined;
|
|
23
23
|
export function parent (instance: object, path: string): object | undefined;
|
|
24
|
-
export function parent <T extends object> (instance: T, path?: string): object | undefined {
|
|
24
|
+
export function parent <T extends object> (instance: T, path?: string): object | null | undefined {
|
|
25
25
|
|
|
26
26
|
// at this situation this check is enough
|
|
27
27
|
if ( instance !== Object( instance ) ) {
|
|
@@ -30,14 +30,29 @@ export function parent <T extends object> (instance: T, path?: string): object |
|
|
|
30
30
|
|
|
31
31
|
const props = _getProps(instance) as Props;
|
|
32
32
|
|
|
33
|
+
// no construction props at all — nothing to seek from. NO PARENT IS
|
|
34
|
+
// null (object-typed, the end of a chain — like Object.getPrototypeOf
|
|
35
|
+
// at the top); undefined is reserved for the path form's "searched,
|
|
36
|
+
// not found" — and a props-less object cannot match any path either
|
|
33
37
|
if ( !props ) {
|
|
34
|
-
|
|
38
|
+
const result = path ? undefined : null;
|
|
39
|
+
return result;
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
const { __parent__: p } = props;
|
|
38
43
|
|
|
44
|
+
// a root instance's __parent__ points at mnemonica's internal root
|
|
45
|
+
// sentinel (an object with no construction props of its own) — that
|
|
46
|
+
// means there is no parent: null, not the sentinel itself
|
|
47
|
+
const parentInstance: object | null = p && _getProps( p ) ? p : null;
|
|
48
|
+
|
|
39
49
|
if ( !path ) {
|
|
40
|
-
return
|
|
50
|
+
return parentInstance;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if ( !parentInstance ) {
|
|
54
|
+
// searched (the chain exists) — nothing found
|
|
55
|
+
return;
|
|
41
56
|
}
|
|
42
57
|
|
|
43
58
|
const segments = path.split('.');
|
|
@@ -49,7 +64,7 @@ export function parent <T extends object> (instance: T, path?: string): object |
|
|
|
49
64
|
// each leading segment must be the direct parent
|
|
50
65
|
// of the instance matched by the next one,
|
|
51
66
|
// and the instance itself is never a candidate
|
|
52
|
-
let current =
|
|
67
|
+
let current = parentInstance;
|
|
53
68
|
for ( ;; ) {
|
|
54
69
|
|
|
55
70
|
const { constructor: { name } } = current as { constructor: { name: string } };
|
package/src/utils/parse.ts
CHANGED
|
@@ -14,9 +14,32 @@ const {
|
|
|
14
14
|
// } = constants;
|
|
15
15
|
|
|
16
16
|
import { extract } from './extract';
|
|
17
|
-
import type {
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
import type {
|
|
18
|
+
EmptyParsed, Parsed
|
|
19
|
+
} from '../types';
|
|
20
|
+
|
|
21
|
+
import {
|
|
22
|
+
_getProps, Props
|
|
23
|
+
} from '../api/types/Props';
|
|
24
|
+
|
|
25
|
+
export function parse<T extends object>( self: T ): Parsed<T>;
|
|
26
|
+
export function parse( self: null ): EmptyParsed;
|
|
27
|
+
export function parse<T extends object> ( self: T | null ): Parsed<T> | EmptyParsed {
|
|
28
|
+
|
|
29
|
+
if ( self === null ) {
|
|
30
|
+
// null is the one non-object input parse accepts: the empty shape —
|
|
31
|
+
// nothing was given. undefined and every other non-object keep
|
|
32
|
+
// throwing WRONG_MODIFICATION_PATTERN
|
|
33
|
+
const nullResult: EmptyParsed = {
|
|
34
|
+
name : undefined,
|
|
35
|
+
props : {},
|
|
36
|
+
self : null,
|
|
37
|
+
proto : undefined,
|
|
38
|
+
joint : {},
|
|
39
|
+
parent : undefined
|
|
40
|
+
};
|
|
41
|
+
return nullResult;
|
|
42
|
+
}
|
|
20
43
|
|
|
21
44
|
if ( !self || !( self as { constructor?: CallableFunction } ).constructor ) {
|
|
22
45
|
throw new WRONG_MODIFICATION_PATTERN;
|
|
@@ -58,14 +81,27 @@ export const parse = <T extends object>( self: T ): Parsed<T> => {
|
|
|
58
81
|
) as Record<string, unknown> );
|
|
59
82
|
delete ( joint as { constructor?: unknown } ).constructor;
|
|
60
83
|
|
|
61
|
-
|
|
62
|
-
//
|
|
84
|
+
// parent is the PARENT INSTANCE — the same object utils.parent(self)
|
|
85
|
+
// returns (the __parent__ record of the construction props), not a
|
|
86
|
+
// prototype layer. A root instance's __parent__ points at mnemonica's
|
|
87
|
+
// internal root sentinel (an object with no construction props of its
|
|
88
|
+
// own) — parse reports NO parent for it, and NO PARENT IS null
|
|
89
|
+
// (object-typed, the end of a chain — like Object.getPrototypeOf at the
|
|
90
|
+
// top). Walking the whole lineage level by level is what
|
|
91
|
+
// utils.deepParse does — this function stays the one-level primitive.
|
|
92
|
+
|
|
93
|
+
// The explored recursive-parse alternative, kept as the record of what
|
|
94
|
+
// was considered before deepParse became the plan's next step:
|
|
63
95
|
// let parent;
|
|
64
96
|
// if ( protoProto[ SymbolConstructorName ] === MNEMONICA ) {
|
|
65
97
|
// parent = parse( protoProto );
|
|
66
98
|
// } else {
|
|
67
99
|
// parent = Reflect.getPrototypeOf( protoProto );
|
|
68
100
|
// }
|
|
101
|
+
const selfProps = _getProps( self ) as Props | undefined;
|
|
102
|
+
const parentRecord = selfProps ? selfProps.__parent__ : undefined;
|
|
103
|
+
const parent: object | null =
|
|
104
|
+
parentRecord && _getProps( parentRecord ) ? parentRecord : null;
|
|
69
105
|
|
|
70
106
|
const result: Parsed<T> = {
|
|
71
107
|
|
|
@@ -83,4 +119,4 @@ export const parse = <T extends object>( self: T ): Parsed<T> => {
|
|
|
83
119
|
|
|
84
120
|
};
|
|
85
121
|
return result;
|
|
86
|
-
}
|
|
122
|
+
}
|
package/src/utils/toJSON.ts
CHANGED
|
@@ -1,45 +1,44 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
import { extract } from './extract';
|
|
4
|
-
export const toJSON = <T extends object>( instance: T ) => {
|
|
5
|
-
|
|
6
|
-
const extracted = extract( instance );
|
|
7
|
-
const result = Object.entries( extracted ).reduce(
|
|
8
|
-
( o: string, entry: [ string, unknown ] ) => {
|
|
9
|
-
|
|
10
|
-
const [ name, _value ] = entry;
|
|
11
|
-
if ( [ null, undefined ].includes( _value as null | undefined ) ) {
|
|
12
|
-
return o;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
let value: string;
|
|
16
4
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
message
|
|
30
|
-
} );
|
|
31
|
-
}
|
|
5
|
+
const descriptionOf = ( error: unknown ) => {
|
|
6
|
+
const {
|
|
7
|
+
stack,
|
|
8
|
+
message
|
|
9
|
+
} = error as Error;
|
|
10
|
+
const description = {
|
|
11
|
+
description : 'This value type is not supported by JSON.stringify',
|
|
12
|
+
stack,
|
|
13
|
+
message
|
|
14
|
+
};
|
|
15
|
+
return description;
|
|
16
|
+
};
|
|
32
17
|
|
|
33
|
-
|
|
34
|
-
return o;
|
|
18
|
+
export const toJSON = <T extends object>( instance: T ) => {
|
|
35
19
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
)
|
|
20
|
+
const extracted = extract( instance );
|
|
21
|
+
// collect the fields into a plain object and stringify ONCE: the output
|
|
22
|
+
// is always valid JSON — keys escaped by the serializer itself, empty
|
|
23
|
+
// field sets become '{}', null/undefined fields omitted
|
|
24
|
+
const collected: Record<string, unknown> = {};
|
|
25
|
+
Object.entries( extracted ).forEach( ( [ name, value ] ) => {
|
|
26
|
+
if ( value === null || value === undefined ) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
let checked: unknown;
|
|
30
|
+
try {
|
|
31
|
+
const probe = JSON.stringify( value );
|
|
32
|
+
// stringify returns undefined (no throw) for values JSON has no
|
|
33
|
+
// representation of — functions, symbols: unstringifiable too
|
|
34
|
+
checked = probe === undefined ? descriptionOf( value ) : value;
|
|
35
|
+
} catch ( error: unknown ) {
|
|
36
|
+
checked = descriptionOf( error );
|
|
37
|
+
}
|
|
38
|
+
collected[ name ] = checked;
|
|
39
|
+
} );
|
|
40
|
+
|
|
41
|
+
const result = JSON.stringify( collected );
|
|
43
42
|
return result;
|
|
44
43
|
|
|
45
44
|
};
|