@bemoje/composition 2.1.3 → 3.0.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.
package/lib/index.js CHANGED
@@ -1,14 +1,809 @@
1
- export * from './IView';
2
- export * from './OptionsConfigurator';
3
- export * from './View';
4
- export * from './inheritProxifiedPrototype';
5
- export * from './inheritProxifiedPrototypeProperty';
6
- export * from './inspector/Inspector';
7
- export * from './inspector/defaults/ignoreValuesDefaults';
8
- export * from './inspector/defaults/ignoreValuesFilterDefaults';
9
- export * from './inspector/defaults/inspectDefaults';
10
- export * from './inspector/defaults/inspectorDefaults';
11
- export * from './inspector/types';
12
- export * from './parenting/ParentRelationTypes';
13
- export * from './parenting/Parenting';
14
- export * from './parenting/types';
1
+ import { KindGuard, Type } from '@sinclair/typebox';
2
+ import { Value } from '@sinclair/typebox/value';
3
+ import { ValueErrorIterator } from '@sinclair/typebox/compiler';
4
+ import { cloneDeep } from 'es-toolkit/object';
5
+ import colors2 from 'ansi-colors';
6
+ import { inspect } from 'util';
7
+ import { isObjectLike, omit } from 'es-toolkit/compat';
8
+ import { mapValues } from 'es-toolkit';
9
+ import { DefaultMap, MultiSet } from 'mnemonist';
10
+
11
+ var __defProp = Object.defineProperty;
12
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
13
+ var SchemaValidationError = class extends Error {
14
+ static {
15
+ __name(this, "SchemaValidationError");
16
+ }
17
+ value;
18
+ errors;
19
+ constructor(errors, value, message) {
20
+ super(message);
21
+ this.name = "SchemaValidationError";
22
+ this.value = value;
23
+ this.errors = Array.isArray(errors) ? errors : errors instanceof ValueErrorIterator ? Array.from(errors) : Array.from(Value.Errors(errors, value));
24
+ }
25
+ };
26
+ function assertValidSchema(schema, data, message) {
27
+ const isValid = Value.Check(schema, data);
28
+ if (!isValid) {
29
+ throw new SchemaValidationError(Value.Errors(schema, data), data, message);
30
+ }
31
+ }
32
+ __name(assertValidSchema, "assertValidSchema");
33
+
34
+ // ../object/src/defineGetter.ts
35
+ function defineGetter(obj, key, get, des = {}) {
36
+ return Object.defineProperty(obj, key, {
37
+ configurable: true,
38
+ enumerable: false,
39
+ get,
40
+ ...des
41
+ });
42
+ }
43
+ __name(defineGetter, "defineGetter");
44
+
45
+ // ../object/src/defineValue.ts
46
+ function defineValue(obj, key, value, des = {}) {
47
+ return Object.defineProperty(obj, key, {
48
+ configurable: true,
49
+ value,
50
+ enumerable: true,
51
+ ...des
52
+ });
53
+ }
54
+ __name(defineValue, "defineValue");
55
+
56
+ // ../object/src/defineLazyProperty.ts
57
+ function defineLazyProperty(obj, key, get, des = {}) {
58
+ function getter() {
59
+ const value = get.call(this);
60
+ defineValue(this, key, value);
61
+ return value;
62
+ }
63
+ __name(getter, "getter");
64
+ return defineGetter(obj, key, getter, des);
65
+ }
66
+ __name(defineLazyProperty, "defineLazyProperty");
67
+
68
+ // ../object/src/defineMethod.ts
69
+ function defineMethod(obj, key, value, des = {}) {
70
+ return Object.defineProperty(obj, key, {
71
+ value,
72
+ configurable: true,
73
+ writable: true,
74
+ enumerable: false,
75
+ ...des
76
+ });
77
+ }
78
+ __name(defineMethod, "defineMethod");
79
+
80
+ // ../object/src/entriesOf.ts
81
+ function entriesOf(obj) {
82
+ return Object.entries(obj);
83
+ }
84
+ __name(entriesOf, "entriesOf");
85
+
86
+ // ../object/src/keysOf.ts
87
+ function keysOf(obj) {
88
+ return Object.keys(obj);
89
+ }
90
+ __name(keysOf, "keysOf");
91
+
92
+ // ../object/src/getClassChain.ts
93
+ function getClassChain(target, options) {
94
+ const ctor = typeof target === "function" ? target : target.constructor;
95
+ const result = [];
96
+ if (options?.includeSelf) {
97
+ result.push(ctor);
98
+ }
99
+ let current = ctor;
100
+ while (current = Reflect.getPrototypeOf(current)) {
101
+ if (current === Function.prototype || current === Object.prototype) {
102
+ result.push(Object);
103
+ break;
104
+ }
105
+ result.push(current);
106
+ }
107
+ return result;
108
+ }
109
+ __name(getClassChain, "getClassChain");
110
+
111
+ // ../object/src/hasOwnProperty.ts
112
+ function hasOwnProperty(object, key) {
113
+ return Object.prototype.hasOwnProperty.call(object, key);
114
+ }
115
+ __name(hasOwnProperty, "hasOwnProperty");
116
+
117
+ // ../object/src/hasPrototypeChainProperty.ts
118
+ function hasPrototypeChainProperty(object, key) {
119
+ return getClassChain(object).some((ctor) => {
120
+ return hasOwnProperty(ctor.prototype, key);
121
+ });
122
+ }
123
+ __name(hasPrototypeChainProperty, "hasPrototypeChainProperty");
124
+
125
+ // ../object/src/hasProperty.ts
126
+ function hasProperty(object, key) {
127
+ return hasOwnProperty(object, key) || hasPrototypeChainProperty(object, key);
128
+ }
129
+ __name(hasProperty, "hasProperty");
130
+
131
+ // src/OptionsConfigurator.ts
132
+ function OptionsConfigurator(schemaOrProps, defaultKeys) {
133
+ const props = KindGuard.IsObject(schemaOrProps) ? schemaOrProps.properties : schemaOrProps;
134
+ const schema = KindGuard.IsObject(schemaOrProps) ? schemaOrProps : Type.Object(props);
135
+ const defaults = entriesOf(props).reduce((acc, [key, prop]) => {
136
+ if ("default" in prop) {
137
+ Reflect.set(acc, key, typeof prop.default === "function" ? prop.default : () => {
138
+ return prop.default;
139
+ });
140
+ }
141
+ return acc;
142
+ }, {});
143
+ const requiredKeys = schema.required;
144
+ const optionalKeys = keysOf(props).filter((k) => {
145
+ return !requiredKeys.includes(k);
146
+ });
147
+ const create = createConfigurator({
148
+ defaults,
149
+ requiredKeys,
150
+ optionalKeys
151
+ });
152
+ const build = /* @__PURE__ */ __name((callback) => {
153
+ const builder = create();
154
+ const options = callback(builder);
155
+ return options;
156
+ }, "build");
157
+ build.getSchemaProps = () => {
158
+ return props;
159
+ };
160
+ build.getSchema = () => {
161
+ return schema;
162
+ };
163
+ build.getDefaults = () => {
164
+ return defaults;
165
+ };
166
+ build.getDefaultKeys = () => {
167
+ return defaultKeys;
168
+ };
169
+ build.cast = (v) => {
170
+ return v;
171
+ };
172
+ build.getCreate = () => {
173
+ return create;
174
+ };
175
+ build.getBuild = () => {
176
+ return build;
177
+ };
178
+ build.createFunction = (func) => {
179
+ function fn(arg0) {
180
+ const options = typeof arg0 === "function" ? build(arg0) : Value.Default(schema, arg0);
181
+ assertValidSchema(schema, options, "Invalid options");
182
+ return func(options);
183
+ }
184
+ __name(fn, "fn");
185
+ if (func.name) {
186
+ Object.defineProperty(fn, "name", {
187
+ value: func.name,
188
+ configurable: true
189
+ });
190
+ }
191
+ Object.defineProperty(fn, "OptionsConfigurator", {
192
+ get() {
193
+ return build;
194
+ },
195
+ enumerable: true
196
+ });
197
+ return fn;
198
+ };
199
+ build.createBaseClass = () => {
200
+ return class Base {
201
+ static {
202
+ __name(this, "Base");
203
+ }
204
+ static get OptionsConfigurator() {
205
+ return build;
206
+ }
207
+ options;
208
+ constructor(arg0) {
209
+ this.options = typeof arg0 === "function" ? build(arg0) : Value.Default(schema, arg0);
210
+ this.initialize();
211
+ }
212
+ /**
213
+ * Called by the constructor
214
+ */
215
+ initialize() {
216
+ this.assertValidOptions();
217
+ }
218
+ isValidOptions() {
219
+ return Value.Check(schema, this.options);
220
+ }
221
+ assertValidOptions() {
222
+ assertValidSchema(schema, this.options, "Invalid options");
223
+ }
224
+ };
225
+ };
226
+ return build;
227
+ }
228
+ __name(OptionsConfigurator, "OptionsConfigurator");
229
+ function createConfigurator(options) {
230
+ let CFG = class CFG {
231
+ static {
232
+ __name(this, "CFG");
233
+ }
234
+ $data;
235
+ constructor($data) {
236
+ this.$data = $data;
237
+ }
238
+ /**
239
+ * Applies defaults for missing values and then returns data.
240
+ */
241
+ done() {
242
+ this.applyDefaults();
243
+ return this.$data;
244
+ }
245
+ /**
246
+ * Set default values for missing keys.
247
+ */
248
+ applyDefaults() {
249
+ entriesOf(options.defaults).forEach(([key, dfValue]) => {
250
+ if (this.$data[key] === void 0) {
251
+ const df = dfValue?.();
252
+ if (df !== void 0) {
253
+ this.$data[key] = df;
254
+ }
255
+ }
256
+ });
257
+ return this;
258
+ }
259
+ };
260
+ const keys = /* @__PURE__ */ new Set([
261
+ ...options.requiredKeys,
262
+ ...keysOf(options.defaults),
263
+ ...options.optionalKeys
264
+ ]);
265
+ for (const key of keys) {
266
+ Object.defineProperty(CFG.prototype, key, {
267
+ value: /* @__PURE__ */ __name(function(value) {
268
+ if (value === void 0) {
269
+ if (key in options.defaults) {
270
+ this.$data[key] = options.defaults[key]();
271
+ } else {
272
+ delete this.$data[key];
273
+ }
274
+ } else {
275
+ this.$data[key] = value;
276
+ }
277
+ return new CFG(this.$data);
278
+ }, "value")
279
+ });
280
+ }
281
+ return /* @__PURE__ */ __name(function configurator() {
282
+ const ins = new CFG({});
283
+ return ins;
284
+ }, "configurator");
285
+ }
286
+ __name(createConfigurator, "createConfigurator");
287
+
288
+ // src/View.ts
289
+ var TARGET_SYMBOL = /* @__PURE__ */ Symbol("target");
290
+ var View = class {
291
+ static {
292
+ __name(this, "View");
293
+ }
294
+ [TARGET_SYMBOL];
295
+ constructor(target) {
296
+ this[TARGET_SYMBOL] = target;
297
+ }
298
+ get [Symbol.toStringTag]() {
299
+ return this.constructor.name;
300
+ }
301
+ get target() {
302
+ return this[TARGET_SYMBOL];
303
+ }
304
+ };
305
+
306
+ // src/inheritProxifiedPrototypeProperty.ts
307
+ function inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, key) {
308
+ const des = Reflect.getOwnPropertyDescriptor(TargetClass.prototype, key);
309
+ if (!des) {
310
+ return;
311
+ }
312
+ if (des.value && typeof des.value === "function") {
313
+ return Object.defineProperty(ViewerClass.prototype, key, {
314
+ value(...args) {
315
+ return this.target[key](...args);
316
+ },
317
+ writable: true,
318
+ configurable: true,
319
+ enumerable: false
320
+ });
321
+ } else if (des && !des.get && !des.set) {
322
+ return Object.defineProperty(ViewerClass.prototype, key, {
323
+ get() {
324
+ return this.target[key];
325
+ },
326
+ set: des.writable ? /* @__PURE__ */ __name(function set(value) {
327
+ this.target[key] = value;
328
+ }, "set") : void 0,
329
+ configurable: true,
330
+ enumerable: false
331
+ });
332
+ } else {
333
+ Object.defineProperty(ViewerClass.prototype, key, {
334
+ get: des.get ? function() {
335
+ return this.target[key];
336
+ } : void 0,
337
+ set: des.set ? function(value) {
338
+ this.target[key] = value;
339
+ } : void 0,
340
+ configurable: true,
341
+ enumerable: false
342
+ });
343
+ }
344
+ }
345
+ __name(inheritProxifiedPrototypeProperty, "inheritProxifiedPrototypeProperty");
346
+
347
+ // src/inheritProxifiedPrototype.ts
348
+ function inheritProxifiedPrototype(ViewerClass, TargetClass, omitKeys) {
349
+ Reflect.ownKeys(TargetClass.prototype).filter((k) => {
350
+ return k !== "constructor" && k !== Symbol.toStringTag;
351
+ }).filter((k) => {
352
+ return omitKeys ? !omitKeys.includes(k) : true;
353
+ }).forEach((k) => {
354
+ return inheritProxifiedPrototypeProperty(ViewerClass, TargetClass, k);
355
+ });
356
+ return ViewerClass;
357
+ }
358
+ __name(inheritProxifiedPrototype, "inheritProxifiedPrototype");
359
+
360
+ // src/inspector/defaults/ignoreValuesFilterDefaults.ts
361
+ var ignoreValuesFilterDefaults = {
362
+ noFalse: /* @__PURE__ */ __name((value) => {
363
+ return value !== false;
364
+ }, "noFalse"),
365
+ noNull: /* @__PURE__ */ __name((value) => {
366
+ return value !== null;
367
+ }, "noNull"),
368
+ noUndefined: /* @__PURE__ */ __name((value) => {
369
+ return value !== void 0;
370
+ }, "noUndefined"),
371
+ noEmptyArray: /* @__PURE__ */ __name((value) => {
372
+ return !Array.isArray(value) || !!value.length;
373
+ }, "noEmptyArray"),
374
+ noEmptyObject: /* @__PURE__ */ __name((value) => {
375
+ return !value || typeof value !== "object" || Array.isArray(value) || !!Object.keys(value).length;
376
+ }, "noEmptyObject")
377
+ };
378
+
379
+ // src/inspector/defaults/ignoreValuesDefaults.ts
380
+ var ignoreValuesDefaults = {
381
+ noEmptyArray: true,
382
+ noEmptyObject: true,
383
+ noNull: true,
384
+ noUndefined: true,
385
+ noFalse: false
386
+ };
387
+ var inspectDefaults = {
388
+ ...inspect.defaultOptions,
389
+ customInspect: true,
390
+ maxStringLength: 500,
391
+ maxArrayLength: 200,
392
+ breakLength: 100,
393
+ colors: true,
394
+ depth: 100,
395
+ compact: false,
396
+ numericSeparator: true
397
+ };
398
+
399
+ // src/inspector/defaults/inspectorDefaults.ts
400
+ var inspectorDefaults = {
401
+ inspect: inspectDefaults,
402
+ ignoreValues: ignoreValuesDefaults,
403
+ keys: [],
404
+ autoAddBooleanKeys: false,
405
+ ignoreKeys: [],
406
+ filters: []
407
+ };
408
+ var Inspector = class _Inspector extends View {
409
+ static {
410
+ __name(this, "Inspector");
411
+ }
412
+ /**
413
+ * Composes a class with Inspector capabilities by adding inspection methods to its prototype.
414
+ * This follows the Composition pattern to extend classes without inheritance.
415
+ */
416
+ static compose(cls, options = {}) {
417
+ if (!hasProperty(cls.prototype, inspect.custom)) {
418
+ defineMethod(cls.prototype, inspect.custom, function(depth, options2) {
419
+ return this.inspector.inspect(depth, options2);
420
+ });
421
+ }
422
+ if (!hasProperty(cls.prototype, "inspector")) {
423
+ defineLazyProperty(cls.prototype, "inspector", /* @__PURE__ */ __name(function get() {
424
+ return new _Inspector(this);
425
+ }, "get"));
426
+ }
427
+ if (!hasProperty(cls.prototype, "toJSON")) {
428
+ defineMethod(cls.prototype, "toJSON", function() {
429
+ return this.inspector.toObject();
430
+ });
431
+ }
432
+ return options;
433
+ }
434
+ /**
435
+ * Compile the target object and return the output as an object.
436
+ */
437
+ toObject(depth = 0) {
438
+ const result = mapValues(this.compile(depth).output, (value) => {
439
+ if (!value) {
440
+ return value;
441
+ }
442
+ if (typeof value !== "object") {
443
+ return value;
444
+ }
445
+ if (Array.isArray(value)) {
446
+ return value.map((v) => {
447
+ if (v.inspector && v.inspector instanceof _Inspector) {
448
+ return v.inspector.toObject(depth + 1);
449
+ }
450
+ return v;
451
+ });
452
+ }
453
+ if (value.inspector && value.inspector instanceof _Inspector) {
454
+ return value.inspector.toObject(depth + 1);
455
+ }
456
+ return value;
457
+ });
458
+ return {
459
+ T: this.target.constructor.name,
460
+ ...result
461
+ };
462
+ }
463
+ /**
464
+ * Inspect the target object.
465
+ */
466
+ inspect(depth = 0, options = {}) {
467
+ const { output, inspectOptions } = this.compile(depth);
468
+ Object.assign(inspectOptions, options);
469
+ const result = inspect(output, options);
470
+ return result.replaceAll(/Object \[\w+] {/gm, (match) => {
471
+ match = match.substring(8, match.length - 3);
472
+ return `[${inspectOptions.colors ? colors2.magenta(match) : match}] {`;
473
+ });
474
+ }
475
+ /**
476
+ * Compiles the output object and inspect options.
477
+ * Merges prototype chain options and applies filters to determine which properties to include.
478
+ */
479
+ compile(depth = 0) {
480
+ const output = Object.defineProperty({}, Symbol.toStringTag, {
481
+ value: this.target.constructor.name,
482
+ configurable: true
483
+ });
484
+ const options = this.mergePrototypeOptions();
485
+ const keys = new Set(options.keys);
486
+ if (options.autoAddBooleanKeys) {
487
+ const insBooleanValueKeys = Object.entries(this.target).filter(([_key, value]) => {
488
+ return typeof value === "boolean";
489
+ }).map(([key]) => {
490
+ return key;
491
+ });
492
+ const protoGetterKeys = getClassChain(this.target, {
493
+ includeSelf: false
494
+ }).map((cls) => {
495
+ return Object.getOwnPropertyNames(cls.prototype).filter((key) => {
496
+ return hasOwnProperty(Reflect.getOwnPropertyDescriptor(cls.prototype, key), "get");
497
+ });
498
+ }).flat(2);
499
+ const booleanKeys = insBooleanValueKeys.concat(protoGetterKeys).filter((key) => {
500
+ return /^(is|was|has|should)[A-Z]/.test(key);
501
+ });
502
+ booleanKeys.forEach((key) => {
503
+ return keys.add(key);
504
+ });
505
+ }
506
+ const ignoreFilters = Object.entries(options.ignoreValues).filter(([_name, enabled]) => {
507
+ return enabled;
508
+ }).map(([name]) => {
509
+ return ignoreValuesFilterDefaults[name];
510
+ });
511
+ const objectValues = [];
512
+ const arrayValues = [];
513
+ for (const key of keys) {
514
+ const value = this.target[key];
515
+ if (!ignoreFilters.every((filter) => {
516
+ return filter(value);
517
+ })) {
518
+ continue;
519
+ }
520
+ if (!options.filters.every((filter) => {
521
+ return filter.call(this.target, value, key, depth, output);
522
+ })) {
523
+ continue;
524
+ }
525
+ if (Array.isArray(value)) {
526
+ arrayValues.push(() => {
527
+ return defineValue(output, key, value);
528
+ });
529
+ } else if (isObjectLike(value)) {
530
+ objectValues.push(() => {
531
+ return defineValue(output, key, value);
532
+ });
533
+ } else {
534
+ defineValue(output, key, value);
535
+ }
536
+ }
537
+ objectValues.forEach((fn) => {
538
+ return fn();
539
+ });
540
+ arrayValues.forEach((fn) => {
541
+ return fn();
542
+ });
543
+ return {
544
+ output,
545
+ inspectOptions: options.inspect
546
+ };
547
+ }
548
+ /**
549
+ * Walks the prototype chain and merges Inspector options from all classes.
550
+ * Subclass options override superclass options following inheritance semantics.
551
+ */
552
+ mergePrototypeOptions() {
553
+ const defaults = this.getDefaults();
554
+ let inspectOptions = defaults.inspect;
555
+ const keys = new Set(defaults.keys);
556
+ let autoAddBooleanKeys = defaults.autoAddBooleanKeys;
557
+ const ignoreKeys = new Set(defaults.ignoreKeys);
558
+ const ignoreValues = defaults.ignoreValues;
559
+ const customFilters = defaults.filters;
560
+ const prototypeChainOptions = getClassChain(this.target.constructor, {
561
+ includeSelf: true
562
+ }).reverse().map((cls) => {
563
+ return cls.inspector ?? {};
564
+ });
565
+ for (const options of prototypeChainOptions) {
566
+ if (options.inspect) {
567
+ inspectOptions = {
568
+ ...inspectOptions,
569
+ ...options.inspect
570
+ };
571
+ }
572
+ options.keys?.forEach((elem) => {
573
+ return keys.add(elem);
574
+ });
575
+ if (options.autoAddBooleanKeys != null) {
576
+ autoAddBooleanKeys = options.autoAddBooleanKeys;
577
+ }
578
+ keys.forEach((elem) => {
579
+ return ignoreKeys.delete(elem);
580
+ });
581
+ options.ignoreKeys?.forEach((elem) => {
582
+ return ignoreKeys.add(elem);
583
+ });
584
+ ignoreKeys.forEach((elem) => {
585
+ return keys.delete(elem);
586
+ });
587
+ if (options.ignoreValues) {
588
+ Object.assign(ignoreValues, options.ignoreValues);
589
+ }
590
+ options.filters?.forEach((filter) => {
591
+ return customFilters.push(filter);
592
+ });
593
+ }
594
+ return {
595
+ inspect: inspectOptions,
596
+ ignoreValues,
597
+ autoAddBooleanKeys,
598
+ keys: Array.from(keys),
599
+ ignoreKeys: Array.from(ignoreKeys),
600
+ filters: customFilters
601
+ };
602
+ }
603
+ /**
604
+ * Returns a deep clone of the default Inspector options.
605
+ */
606
+ getDefaults() {
607
+ const filters = inspectorDefaults.filters.slice();
608
+ const rest = cloneDeep(omit(inspectorDefaults, "filters"));
609
+ return {
610
+ ...rest,
611
+ filters
612
+ };
613
+ }
614
+ };
615
+ var ParentRelationTypes = class _ParentRelationTypes extends View {
616
+ static {
617
+ __name(this, "ParentRelationTypes");
618
+ }
619
+ registerParent(parentType) {
620
+ _ParentRelationTypes.parentTypesStats.get(this.target).add(parentType);
621
+ _ParentRelationTypes.childTypesStats.get(parentType).add(this.target);
622
+ }
623
+ get hasParent() {
624
+ return _ParentRelationTypes.parentTypesStats.has(this.target);
625
+ }
626
+ get getParentTypes() {
627
+ return _ParentRelationTypes.parentTypesStats.get(this.target);
628
+ }
629
+ get hasChildren() {
630
+ return _ParentRelationTypes.childTypesStats.has(this.target);
631
+ }
632
+ get getChildTypes() {
633
+ return _ParentRelationTypes.childTypesStats.get(this.target);
634
+ }
635
+ get isRoot() {
636
+ return !this.hasParent && this.hasChildren;
637
+ }
638
+ get isLeaf() {
639
+ return this.hasParent && !this.hasChildren;
640
+ }
641
+ get isIsolated() {
642
+ return !this.hasParent && !this.hasChildren;
643
+ }
644
+ static getAllChildTypesStats() {
645
+ const res = {};
646
+ Array.from(this.childTypesStats.entries()).map(([cls, multimap]) => {
647
+ multimap.forEachMultiplicity((count, key) => {
648
+ if (!res[cls.name]) {
649
+ res[cls.name] = {};
650
+ }
651
+ if (!res[cls.name][key.name]) {
652
+ res[cls.name][key.name] = count;
653
+ }
654
+ });
655
+ });
656
+ return res;
657
+ }
658
+ static getAllParentTypesStats() {
659
+ const res = {};
660
+ Array.from(this.parentTypesStats.entries()).map(([cls, multimap]) => {
661
+ multimap.forEachMultiplicity((count, key) => {
662
+ if (!res[cls.name]) {
663
+ res[cls.name] = {};
664
+ }
665
+ if (!res[cls.name][key.name]) {
666
+ res[cls.name][key.name] = count;
667
+ }
668
+ });
669
+ });
670
+ return res;
671
+ }
672
+ static getAllStats() {
673
+ const classes = /* @__PURE__ */ new Set([
674
+ ...Array.from(this.childTypesStats.keys()),
675
+ ...Array.from(this.parentTypesStats.keys())
676
+ ]);
677
+ return Array.from(classes).map((cls) => {
678
+ const parents = Array.from(this.parentTypesStats.get(cls).multiplicities()).map(([cls2, count]) => {
679
+ return [
680
+ cls2.name,
681
+ count
682
+ ];
683
+ });
684
+ const children = Array.from(this.childTypesStats.get(cls).multiplicities()).map(([cls2, count]) => {
685
+ return [
686
+ cls2.name,
687
+ count
688
+ ];
689
+ });
690
+ const result = {
691
+ class: cls.name
692
+ };
693
+ if (parents.length) {
694
+ result.parents = Object.fromEntries(parents);
695
+ }
696
+ if (children.length) {
697
+ result.children = Object.fromEntries(children);
698
+ }
699
+ return result;
700
+ });
701
+ }
702
+ static printAllStats() {
703
+ console.log();
704
+ console.log(colors2.bold.yellow("Parent-Child Relation Stats"));
705
+ for (const stats of this.getAllStats()) {
706
+ console.log(stats.class);
707
+ if (stats.parents) {
708
+ let prefix = ` ${colors2.dim("parents : ")}`;
709
+ for (const [parent, count] of Object.entries(stats.parents)) {
710
+ console.log(`${prefix}${colors2.blue(parent)}: ${colors2.yellow(String(count))}`);
711
+ prefix = " ".repeat(12);
712
+ }
713
+ }
714
+ if (stats.children) {
715
+ let prefix = ` ${colors2.dim("children : ")}`;
716
+ for (const [child, count] of Object.entries(stats.children)) {
717
+ console.log(`${prefix}${colors2.green(child)}: ${colors2.yellow(String(count))}`);
718
+ prefix = " ".repeat(12);
719
+ }
720
+ }
721
+ console.log();
722
+ }
723
+ }
724
+ static childTypesStats = new DefaultMap(() => {
725
+ return new MultiSet();
726
+ });
727
+ static parentTypesStats = new DefaultMap(() => {
728
+ return new MultiSet();
729
+ });
730
+ };
731
+ var Parenting = class _Parenting extends View {
732
+ static {
733
+ __name(this, "Parenting");
734
+ }
735
+ /**
736
+ * Class decorator to register and correctly initialize a class as a 'parentingTarget'.
737
+ */
738
+ static compose(cls) {
739
+ if (!hasProperty(cls, "parenting")) {
740
+ defineLazyProperty(cls, "parenting", function() {
741
+ return new ParentRelationTypes(this);
742
+ });
743
+ }
744
+ if (!hasProperty(cls.prototype, "parenting")) {
745
+ defineLazyProperty(cls.prototype, "parenting", function() {
746
+ return new _Parenting(this);
747
+ });
748
+ }
749
+ }
750
+ /**
751
+ * Store a weak refences to the target object's parent.
752
+ */
753
+ onInstance(parent) {
754
+ if (!parent) {
755
+ return;
756
+ }
757
+ const targetClass = this.parentingTargetConstructor;
758
+ _Parenting.parentWeakRefMap.get(targetClass).set(this.target, parent);
759
+ targetClass.parenting?.registerParent(parent.constructor);
760
+ }
761
+ /**
762
+ * Retrieve the parent object of 'target' object.
763
+ */
764
+ getParent() {
765
+ const map = _Parenting.parentWeakRefMap.get(this.parentingTargetConstructor);
766
+ const parent = map.get(this.target);
767
+ return parent;
768
+ }
769
+ /**
770
+ * Recursively get parent until no parent is found or a circular reference is detected.
771
+ */
772
+ *iterateAncestors() {
773
+ let node = this.target.parenting.getParent();
774
+ if (!node) {
775
+ return;
776
+ }
777
+ const seen = /* @__PURE__ */ new Set();
778
+ while (node) {
779
+ if (seen.has(node)) {
780
+ return;
781
+ }
782
+ seen.add(node);
783
+ yield node;
784
+ node = node?.parenting?.getParent();
785
+ }
786
+ }
787
+ /**
788
+ * The depth of the object in the parent-child hierarchy.
789
+ */
790
+ get depth() {
791
+ return [
792
+ ...this.iterateAncestors()
793
+ ].length;
794
+ }
795
+ /**
796
+ * The class constructor of the target object.
797
+ */
798
+ get parentingTargetConstructor() {
799
+ return this.target.constructor;
800
+ }
801
+ /**
802
+ * A WeakMap for parent references.
803
+ */
804
+ static parentWeakRefMap = new DefaultMap(() => {
805
+ return /* @__PURE__ */ new WeakMap();
806
+ });
807
+ };
808
+
809
+ export { Inspector, OptionsConfigurator, ParentRelationTypes, Parenting, View, ignoreValuesDefaults, ignoreValuesFilterDefaults, inheritProxifiedPrototype, inheritProxifiedPrototypeProperty, inspectDefaults, inspectorDefaults };