@bemoje/composition 2.0.0 → 2.0.1
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/README.md +150 -0
- package/index.d.ts +1 -1
- package/index.mjs +1 -1
- package/package.json +1 -1
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
|
+
[](https://www.typescriptlang.org/)
|
|
6
|
+
[](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/index.d.ts
CHANGED
package/index.mjs
CHANGED
|
@@ -286,7 +286,7 @@ var Inspector = class _Inspector extends View {
|
|
|
286
286
|
const { output, inspectOptions } = this.compile(depth);
|
|
287
287
|
Object.assign(inspectOptions, options);
|
|
288
288
|
const result = inspect(output, options);
|
|
289
|
-
return result.
|
|
289
|
+
return result.replaceAll(/Object \[\w+] {/gm, (match) => {
|
|
290
290
|
match = match.substring(8, match.length - 3);
|
|
291
291
|
return `[${inspectOptions.colors ? colors2.magenta(match) : match}] {`;
|
|
292
292
|
});
|