composition-ts 0.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/LICENSE +21 -0
- package/README.md +38 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.mjs +29 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 kasukabe tsumugi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Composition TS
|
|
2
|
+
|
|
3
|
+
Compose any number of types into a single intersection type.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import type { Compose } from 'composition-ts';
|
|
9
|
+
|
|
10
|
+
type Result = Compose<[{ id: number }, { name: string }, { active: boolean }]>;
|
|
11
|
+
|
|
12
|
+
// Result:
|
|
13
|
+
// { id: number } & { name: string } & { active: boolean }
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## API
|
|
17
|
+
|
|
18
|
+
### Compose<Types>
|
|
19
|
+
|
|
20
|
+
Intersects every type in the tuple from left to right.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
type Empty = Compose<[]>; // unknown
|
|
24
|
+
type Single = Compose<[string]>; // string
|
|
25
|
+
type Mixed = Compose<[string, number]>; // never
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### IntersectAll<Types>
|
|
29
|
+
|
|
30
|
+
Alias of Compose<Types>.
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
MIT
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
type Compose<Types extends readonly unknown[]> = Types extends readonly [infer Head, ...infer Tail]
|
|
2
|
+
? Head & Compose<Tail>
|
|
3
|
+
: unknown;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Instead of using class inheritance, we can compose prototypes from multiple objects. This function takes a constructor function and an array of objects, and returns a new class that creates instances with the combined prototype of all the objects.
|
|
7
|
+
* - `initializer` **must not** be an arrow function!
|
|
8
|
+
*
|
|
9
|
+
* @param initializer A function that initializes the instance. It will be called with the instance as `this` and use the arguments passed to the constructor.
|
|
10
|
+
* @param parts An array of objects whose properties will be copied(via descriptors) to the prototype of the new class.
|
|
11
|
+
* @param className An optional name for the new class. If not provided, it will default to the name of the `initializer` function.
|
|
12
|
+
*
|
|
13
|
+
* ## About
|
|
14
|
+
* @package CompositionTs
|
|
15
|
+
* @author Kasukabe Tsumugi <futami16237@gmail.com>
|
|
16
|
+
* @version 0.0.0 (Last Update: 2026.03.26 10:24:23.468)
|
|
17
|
+
* @license MIT
|
|
18
|
+
* @link git+https://github.com/baendlorel/kt-packages.git
|
|
19
|
+
* @link https://baendlorel.github.io/ Welcome to my site!
|
|
20
|
+
* @description Instead of inheritance, make it composition!
|
|
21
|
+
* @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
|
|
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;
|
|
24
|
+
|
|
25
|
+
export { compose };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const t = Object.defineProperty, n = Object.defineProperties, e = Object.getOwnPropertyDescriptors;
|
|
2
|
+
|
|
3
|
+
function c(c, r, o) {
|
|
4
|
+
if (function(t) {
|
|
5
|
+
if ("function" != typeof t) return !0;
|
|
6
|
+
try {
|
|
7
|
+
return new new Proxy(t, {
|
|
8
|
+
construct: () => ({})
|
|
9
|
+
}), !1;
|
|
10
|
+
} catch {
|
|
11
|
+
return !0;
|
|
12
|
+
}
|
|
13
|
+
}(c)) throw new TypeError("First argument must be a non-arrow function because it cannot access the instance's 'this' context.");
|
|
14
|
+
const s = t(class {
|
|
15
|
+
constructor(...t) {
|
|
16
|
+
c.apply(this, t);
|
|
17
|
+
}
|
|
18
|
+
}, "name", {
|
|
19
|
+
value: o ?? c.name,
|
|
20
|
+
configurable: !0
|
|
21
|
+
});
|
|
22
|
+
for (let t = 0; t < r.length; t++) {
|
|
23
|
+
const c = e(r[t]);
|
|
24
|
+
n(s.prototype, c);
|
|
25
|
+
}
|
|
26
|
+
return s;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { c as compose };
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "composition-ts",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Kasukabe Tsumugi",
|
|
6
|
+
"email": "futami16237@gmail.com"
|
|
7
|
+
},
|
|
8
|
+
"purpose": "npm",
|
|
9
|
+
"description": "Instead of inheritance, make it composition!",
|
|
10
|
+
"description_zh": "不使用继承,使用组合吧!",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"module": "./dist/index.mjs",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.mjs",
|
|
17
|
+
"default": "./dist/index.mjs"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/baendlorel/kt-packages.git",
|
|
25
|
+
"directory": "packages/composition-ts"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"typescript",
|
|
29
|
+
"javascript"
|
|
30
|
+
],
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/baendlorel/kt-packages/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/baendlorel/kt-packages/tree/main/packages/composition-ts#readme"
|
|
36
|
+
}
|