@bemoje/composition 2.0.0 → 2.1.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 (64) hide show
  1. package/README.md +150 -0
  2. package/lib/IView.cjs +2 -0
  3. package/lib/IView.d.cts +12 -0
  4. package/lib/IView.d.ts +12 -0
  5. package/lib/IView.js +1 -0
  6. package/lib/OptionsConfigurator.cjs +166 -0
  7. package/lib/OptionsConfigurator.d.cts +2229 -0
  8. package/lib/OptionsConfigurator.d.ts +2229 -0
  9. package/lib/OptionsConfigurator.js +163 -0
  10. package/lib/View.cjs +22 -0
  11. package/lib/View.d.cts +14 -0
  12. package/lib/View.d.ts +14 -0
  13. package/lib/View.js +20 -0
  14. package/lib/index.cjs +103 -0
  15. package/lib/index.d.cts +16 -0
  16. package/lib/index.d.ts +16 -0
  17. package/lib/index.js +14 -0
  18. package/lib/inheritProxifiedPrototype.cjs +19 -0
  19. package/lib/inheritProxifiedPrototype.d.cts +11 -0
  20. package/lib/inheritProxifiedPrototype.d.ts +11 -0
  21. package/lib/inheritProxifiedPrototype.js +16 -0
  22. package/lib/inheritProxifiedPrototypeProperty.cjs +45 -0
  23. package/lib/inheritProxifiedPrototypeProperty.d.cts +9 -0
  24. package/lib/inheritProxifiedPrototypeProperty.d.ts +9 -0
  25. package/lib/inheritProxifiedPrototypeProperty.js +43 -0
  26. package/lib/inspector/Inspector.cjs +227 -0
  27. package/lib/inspector/Inspector.d.cts +4 -0
  28. package/lib/inspector/Inspector.d.ts +4 -0
  29. package/lib/inspector/Inspector.js +220 -0
  30. package/lib/inspector/defaults/ignoreValuesDefaults.cjs +11 -0
  31. package/lib/inspector/defaults/ignoreValuesDefaults.d.cts +11 -0
  32. package/lib/inspector/defaults/ignoreValuesDefaults.d.ts +11 -0
  33. package/lib/inspector/defaults/ignoreValuesDefaults.js +11 -0
  34. package/lib/inspector/defaults/ignoreValuesFilterDefaults.cjs +23 -0
  35. package/lib/inspector/defaults/ignoreValuesFilterDefaults.d.cts +11 -0
  36. package/lib/inspector/defaults/ignoreValuesFilterDefaults.d.ts +11 -0
  37. package/lib/inspector/defaults/ignoreValuesFilterDefaults.js +21 -0
  38. package/lib/inspector/defaults/inspectDefaults.cjs +17 -0
  39. package/lib/inspector/defaults/inspectDefaults.d.cts +8 -0
  40. package/lib/inspector/defaults/inspectDefaults.d.ts +8 -0
  41. package/lib/inspector/defaults/inspectDefaults.js +16 -0
  42. package/lib/inspector/defaults/inspectorDefaults.cjs +15 -0
  43. package/lib/inspector/defaults/inspectorDefaults.d.cts +11 -0
  44. package/lib/inspector/defaults/inspectorDefaults.d.ts +11 -0
  45. package/lib/inspector/defaults/inspectorDefaults.js +14 -0
  46. package/lib/inspector/types.cjs +2 -0
  47. package/lib/inspector/types.d.cts +4 -0
  48. package/lib/inspector/types.d.ts +4 -0
  49. package/lib/inspector/types.js +1 -0
  50. package/lib/parenting/ParentRelationTypes.cjs +130 -0
  51. package/lib/parenting/ParentRelationTypes.d.cts +4 -0
  52. package/lib/parenting/ParentRelationTypes.d.ts +4 -0
  53. package/lib/parenting/ParentRelationTypes.js +123 -0
  54. package/lib/parenting/Parenting.cjs +88 -0
  55. package/lib/parenting/Parenting.d.cts +4 -0
  56. package/lib/parenting/Parenting.d.ts +4 -0
  57. package/lib/parenting/Parenting.js +85 -0
  58. package/lib/parenting/types.cjs +2 -0
  59. package/lib/parenting/types.d.cts +4 -0
  60. package/lib/parenting/types.d.ts +4 -0
  61. package/lib/parenting/types.js +1 -0
  62. package/package.json +239 -21
  63. package/index.d.ts +0 -193
  64. package/index.mjs +0 -628
package/README.md ADDED
@@ -0,0 +1,150 @@
1
+ # @bemoje/composition
2
+
3
+ Class composition utilities for views, proxy-based inheritance, object inspection, and parent-child relationships.
4
+
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5-blue)](https://www.typescriptlang.org/)
6
+ [![Module](https://img.shields.io/badge/Module-ESM-yellow)](https://nodejs.org/api/esm.html)
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ npm install @bemoje/composition
12
+ ```
13
+
14
+ ## Features
15
+
16
+ - **View pattern** - Compose objects through a target wrapper abstraction
17
+ - **Proxy inheritance** - Inherit prototype members via proxy redirection
18
+ - **Inspector** - Deep object inspection with customizable formatting and serialization
19
+ - **Parenting** - Manage parent-child relationships between objects with weak references
20
+
21
+ ## Usage
22
+
23
+ ### View
24
+
25
+ The `View` class wraps a target object, enabling the composition pattern:
26
+
27
+ ```ts
28
+ import { View } from '@bemoje/composition'
29
+
30
+ class UserView extends View<{ name: string; email: string }> {
31
+ get displayName() {
32
+ return this.target.name.toUpperCase()
33
+ }
34
+ }
35
+
36
+ const view = new UserView({ name: 'Alice', email: 'alice@example.com' })
37
+ view.displayName // => 'ALICE'
38
+ view.target.email // => 'alice@example.com'
39
+ ```
40
+
41
+ ### Proxy-Based Prototype Inheritance
42
+
43
+ Redirect prototype method calls through a proxy to the view's target:
44
+
45
+ ```ts
46
+ import { View, inheritProxifiedPrototype } from '@bemoje/composition'
47
+
48
+ class DataStore {
49
+ items: string[] = []
50
+ add(item: string) {
51
+ this.items.push(item)
52
+ }
53
+ count() {
54
+ return this.items.length
55
+ }
56
+ }
57
+
58
+ class DataView extends View<DataStore> {}
59
+
60
+ // Inherit DataStore methods on DataView, proxied through .target
61
+ inheritProxifiedPrototype(DataView, DataStore, ['items'])
62
+
63
+ const store = new DataStore()
64
+ const view = new DataView(store) as DataView & DataStore
65
+ view.add('hello') // calls store.add('hello')
66
+ view.count() // => 1
67
+ ```
68
+
69
+ ### Inspector
70
+
71
+ Compose classes with customizable inspection for `console.log`, `util.inspect`, and `JSON.stringify`:
72
+
73
+ ```ts
74
+ import { Inspector } from '@bemoje/composition'
75
+
76
+ class Config {
77
+ host = 'localhost'
78
+ port = 3000
79
+ secret = 'my-secret'
80
+ }
81
+
82
+ Inspector.compose(Config, {
83
+ keys: ['host', 'port'], // only show these in output
84
+ ignoreValues: { undefined: true }, // hide undefined values
85
+ })
86
+
87
+ const config = new Config()
88
+ console.log(config)
89
+ // Config { host: 'localhost', port: 3000 }
90
+
91
+ JSON.stringify(config)
92
+ // '{"host":"localhost","port":3000}'
93
+ ```
94
+
95
+ ### Parenting
96
+
97
+ Manage parent-child object relationships with automatic weak reference tracking:
98
+
99
+ ```ts
100
+ import { Parenting } from '@bemoje/composition'
101
+
102
+ @Parenting.compose
103
+ class TreeNode {
104
+ name: string
105
+ declare parenting: Parenting
106
+
107
+ constructor(name: string, parent?: TreeNode) {
108
+ this.name = name
109
+ this.parenting.onInstance(parent ?? null)
110
+ }
111
+ }
112
+
113
+ const root = new TreeNode('root')
114
+ const child = new TreeNode('child', root)
115
+
116
+ child.parenting.getParent() // => root
117
+ ```
118
+
119
+ ## API Reference
120
+
121
+ ### Views
122
+
123
+ | Export | Description |
124
+ | ------- | --------------------------------------------------- |
125
+ | `View` | Base class wrapping a target object for composition |
126
+ | `IView` | Interface for the View pattern |
127
+
128
+ ### Prototype Inheritance
129
+
130
+ | Function | Description |
131
+ | ----------------------------------- | --------------------------------------- |
132
+ | `inheritProxifiedPrototype` | Inherit all prototype members via proxy |
133
+ | `inheritProxifiedPrototypeProperty` | Inherit a single property via proxy |
134
+
135
+ ### Inspector
136
+
137
+ | Export | Description |
138
+ | ---------------------------- | ----------------------------------------------------------- |
139
+ | `Inspector` | Deep inspection with configurable display and serialization |
140
+ | `inspectorDefaults` | Default inspector configuration |
141
+ | `inspectDefaults` | Default inspect behavior |
142
+ | `ignoreValuesDefaults` | Default ignored value patterns |
143
+ | `ignoreValuesFilterDefaults` | Default filter for ignored values |
144
+
145
+ ### Parenting
146
+
147
+ | Export | Description |
148
+ | --------------------- | ---------------------------------------------- |
149
+ | `Parenting` | Mixin for parent-child relationship management |
150
+ | `ParentRelationTypes` | Registry of parent relation types |
package/lib/IView.cjs ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Interface for a View that wraps a target object.
3
+ * View methods are redirected to the wrapped object which acts as
4
+ * a surrogate 'this'.
5
+ *
6
+ * To create a method that redirects to 'target', @see {@link defineProxifiedPrototypeProperty}
7
+ */
8
+ interface IView<T extends object> {
9
+ readonly target: T;
10
+ }
11
+
12
+ export type { IView };
package/lib/IView.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Interface for a View that wraps a target object.
3
+ * View methods are redirected to the wrapped object which acts as
4
+ * a surrogate 'this'.
5
+ *
6
+ * To create a method that redirects to 'target', @see {@link defineProxifiedPrototypeProperty}
7
+ */
8
+ interface IView<T extends object> {
9
+ readonly target: T;
10
+ }
11
+
12
+ export type { IView };
package/lib/IView.js ADDED
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,166 @@
1
+ 'use strict';
2
+
3
+ var typebox = require('@sinclair/typebox');
4
+ var value = require('@sinclair/typebox/value');
5
+ var tschema = require('@mono/tschema');
6
+ var object = require('@mono/object');
7
+
8
+ var __defProp = Object.defineProperty;
9
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
10
+ function OptionsConfigurator(schemaOrProps, defaultKeys) {
11
+ const props = typebox.KindGuard.IsObject(schemaOrProps) ? schemaOrProps.properties : schemaOrProps;
12
+ const schema = typebox.KindGuard.IsObject(schemaOrProps) ? schemaOrProps : typebox.Type.Object(props);
13
+ const defaults = object.entriesOf(props).reduce((acc, [key, prop]) => {
14
+ if ("default" in prop) {
15
+ Reflect.set(acc, key, typeof prop.default === "function" ? prop.default : () => {
16
+ return prop.default;
17
+ });
18
+ }
19
+ return acc;
20
+ }, {});
21
+ const requiredKeys = schema.required;
22
+ const optionalKeys = object.keysOf(props).filter((k) => {
23
+ return !requiredKeys.includes(k);
24
+ });
25
+ const create = createConfigurator({
26
+ defaults,
27
+ requiredKeys,
28
+ optionalKeys
29
+ });
30
+ const build = /* @__PURE__ */ __name((callback) => {
31
+ const builder = create();
32
+ const options = callback(builder);
33
+ return options;
34
+ }, "build");
35
+ build.getSchemaProps = () => {
36
+ return props;
37
+ };
38
+ build.getSchema = () => {
39
+ return schema;
40
+ };
41
+ build.getDefaults = () => {
42
+ return defaults;
43
+ };
44
+ build.getDefaultKeys = () => {
45
+ return defaultKeys;
46
+ };
47
+ build.cast = (v) => {
48
+ return v;
49
+ };
50
+ build.getCreate = () => {
51
+ return create;
52
+ };
53
+ build.getBuild = () => {
54
+ return build;
55
+ };
56
+ build.createFunction = (func) => {
57
+ function fn(arg0) {
58
+ const options = typeof arg0 === "function" ? build(arg0) : value.Value.Default(schema, arg0);
59
+ tschema.assertValidSchema(schema, options, "Invalid options");
60
+ return func(options);
61
+ }
62
+ __name(fn, "fn");
63
+ if (func.name) {
64
+ Object.defineProperty(fn, "name", {
65
+ value: func.name,
66
+ configurable: true
67
+ });
68
+ }
69
+ Object.defineProperty(fn, "OptionsConfigurator", {
70
+ get() {
71
+ return build;
72
+ },
73
+ enumerable: true
74
+ });
75
+ return fn;
76
+ };
77
+ build.createBaseClass = () => {
78
+ return class Base {
79
+ static {
80
+ __name(this, "Base");
81
+ }
82
+ static get OptionsConfigurator() {
83
+ return build;
84
+ }
85
+ options;
86
+ constructor(arg0) {
87
+ this.options = typeof arg0 === "function" ? build(arg0) : value.Value.Default(schema, arg0);
88
+ this.initialize();
89
+ }
90
+ /**
91
+ * Called by the constructor
92
+ */
93
+ initialize() {
94
+ this.assertValidOptions();
95
+ }
96
+ isValidOptions() {
97
+ return value.Value.Check(schema, this.options);
98
+ }
99
+ assertValidOptions() {
100
+ tschema.assertValidSchema(schema, this.options, "Invalid options");
101
+ }
102
+ };
103
+ };
104
+ return build;
105
+ }
106
+ __name(OptionsConfigurator, "OptionsConfigurator");
107
+ function createConfigurator(options) {
108
+ let CFG = class CFG {
109
+ static {
110
+ __name(this, "CFG");
111
+ }
112
+ $data;
113
+ constructor($data) {
114
+ this.$data = $data;
115
+ }
116
+ /**
117
+ * Applies defaults for missing values and then returns data.
118
+ */
119
+ done() {
120
+ this.applyDefaults();
121
+ return this.$data;
122
+ }
123
+ /**
124
+ * Set default values for missing keys.
125
+ */
126
+ applyDefaults() {
127
+ object.entriesOf(options.defaults).forEach(([key, dfValue]) => {
128
+ if (this.$data[key] === void 0) {
129
+ const df = dfValue?.();
130
+ if (df !== void 0) {
131
+ this.$data[key] = df;
132
+ }
133
+ }
134
+ });
135
+ return this;
136
+ }
137
+ };
138
+ const keys = /* @__PURE__ */ new Set([
139
+ ...options.requiredKeys,
140
+ ...object.keysOf(options.defaults),
141
+ ...options.optionalKeys
142
+ ]);
143
+ for (const key of keys) {
144
+ Object.defineProperty(CFG.prototype, key, {
145
+ value: /* @__PURE__ */ __name(function(value) {
146
+ if (value === void 0) {
147
+ if (key in options.defaults) {
148
+ this.$data[key] = options.defaults[key]();
149
+ } else {
150
+ delete this.$data[key];
151
+ }
152
+ } else {
153
+ this.$data[key] = value;
154
+ }
155
+ return new CFG(this.$data);
156
+ }, "value")
157
+ });
158
+ }
159
+ return /* @__PURE__ */ __name(function configurator() {
160
+ const ins = new CFG({});
161
+ return ins;
162
+ }, "configurator");
163
+ }
164
+ __name(createConfigurator, "createConfigurator");
165
+
166
+ exports.OptionsConfigurator = OptionsConfigurator;