composition-ts 0.0.0 → 0.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.
- package/README.md +50 -16
- package/dist/index.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,38 +1,72 @@
|
|
|
1
1
|
# Composition TS
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Instead of inheritance, compose prototype parts into a generated class.
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
7
7
|
```ts
|
|
8
|
-
import
|
|
8
|
+
import { compose } from 'composition-ts';
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
const statePart = {
|
|
11
|
+
id: 0,
|
|
12
|
+
name: '',
|
|
13
|
+
};
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
const renamable = {
|
|
16
|
+
rename(this: typeof statePart, next: string) {
|
|
17
|
+
this.name = next;
|
|
18
|
+
return this.name;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const describable = {
|
|
23
|
+
get label() {
|
|
24
|
+
const self = this as typeof statePart;
|
|
25
|
+
return `${self.id}:${self.name}`;
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const PersonModel = compose(
|
|
30
|
+
function Person(this: Person, id: number, name: string) {
|
|
31
|
+
this.id = id;
|
|
32
|
+
this.name = name;
|
|
33
|
+
},
|
|
34
|
+
[statePart, renamable, describable],
|
|
35
|
+
'PersonModel',
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const person = new PersonModel(1, 'Ada');
|
|
39
|
+
|
|
40
|
+
person.rename('Grace');
|
|
41
|
+
console.log(person.label); // 1:Grace
|
|
14
42
|
```
|
|
15
43
|
|
|
16
44
|
## API
|
|
17
45
|
|
|
18
|
-
###
|
|
46
|
+
### compose(initializer, parts, className?)
|
|
19
47
|
|
|
20
|
-
|
|
48
|
+
Creates a new class and copies every property descriptor from `parts` onto its prototype.
|
|
21
49
|
|
|
22
50
|
```ts
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
51
|
+
declare function compose<Protos extends readonly object[], Args extends any[], Instance extends Compose<Protos>>(
|
|
52
|
+
initializer: (this: Instance, ...args: Args) => any,
|
|
53
|
+
parts: Protos,
|
|
54
|
+
className?: string,
|
|
55
|
+
): new (...args: Args) => Instance;
|
|
26
56
|
```
|
|
27
57
|
|
|
28
|
-
|
|
58
|
+
Parameters:
|
|
29
59
|
|
|
30
|
-
|
|
60
|
+
- `initializer`: A regular function invoked inside the generated constructor with the new instance bound as `this`.
|
|
61
|
+
- `parts`: An array of objects whose own property descriptors are copied onto the generated class prototype.
|
|
62
|
+
- `className`: Optional class name for the generated constructor. Defaults to `initializer.name`.
|
|
31
63
|
|
|
32
|
-
|
|
64
|
+
Notes:
|
|
33
65
|
|
|
34
|
-
|
|
66
|
+
- `initializer` must be a regular function. Arrow functions cannot receive the instance through `this`.
|
|
67
|
+
- Property descriptors are copied as-is, so getters, setters, enumerability, and writability are preserved.
|
|
68
|
+
- When multiple parts define the same key, later parts overwrite earlier ones.
|
|
35
69
|
|
|
36
|
-
|
|
70
|
+
## License
|
|
37
71
|
|
|
38
|
-
|
|
72
|
+
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -13,13 +13,13 @@ type Compose<Types extends readonly unknown[]> = Types extends readonly [infer H
|
|
|
13
13
|
* ## About
|
|
14
14
|
* @package CompositionTs
|
|
15
15
|
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
16
|
-
* @version 0.
|
|
16
|
+
* @version 0.1.0 (Last Update: 2026.03.26 11:15:42.072)
|
|
17
17
|
* @license MIT
|
|
18
18
|
* @link git+https://github.com/baendlorel/kt-packages.git
|
|
19
19
|
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
20
20
|
* @description Instead of inheritance, make it composition!
|
|
21
21
|
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
22
22
|
*/
|
|
23
|
-
declare function compose<Protos extends readonly object[], Args extends any[], Instance extends Compose<Protos>>(initializer: (this: Instance, ...args: Args) => any, parts: Protos, className?: string): new (...args: Args) => Instance;
|
|
23
|
+
declare function compose<Protos extends readonly object[], Args extends any[], Instance extends Compose<Protos> = Compose<Protos>>(initializer: (this: Instance, ...args: Args) => any, parts: Protos, className?: string): new (...args: Args) => Instance;
|
|
24
24
|
|
|
25
25
|
export { compose };
|