udic 0.0.4 → 0.1.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 +97 -12
- package/index.d.ts +47 -2
- package/index.js +1 -1
- package/package.json +5 -2
- package/di.d.ts +0 -30
- package/di.js +0 -1
package/README.md
CHANGED
|
@@ -1,23 +1,108 @@
|
|
|
1
1
|
A simple dependency injection library.
|
|
2
|
+
|
|
3
|
+
# Examples
|
|
4
|
+
```ts
|
|
5
|
+
import * as di from 'udic';
|
|
6
|
+
```
|
|
7
|
+
|
|
8
|
+
Run code that depends on a service:
|
|
9
|
+
```ts
|
|
10
|
+
// Create a service that generates random number
|
|
11
|
+
const randNumber = di.service('randNumber')<() => number>();
|
|
12
|
+
|
|
13
|
+
// Use the service
|
|
14
|
+
const compute = di.derive(
|
|
15
|
+
[randNumber],
|
|
16
|
+
(getRandNumber) => getRandNumber() + 1
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
// 1
|
|
20
|
+
console.log(
|
|
21
|
+
compute({ randNumber: () => 0 })
|
|
22
|
+
);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Nested `derive()`:
|
|
26
|
+
```ts
|
|
27
|
+
// Create a service that generates random number
|
|
28
|
+
const randNumber = di.service('randNumber')<() => number>();
|
|
29
|
+
|
|
30
|
+
// Use the service
|
|
31
|
+
const compute = di.derive(
|
|
32
|
+
[randNumber],
|
|
33
|
+
(getRandNumber) => getRandNumber() + 1
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
// Use `compute()` within current context
|
|
37
|
+
const anotherCompute = di.derive(
|
|
38
|
+
[compute],
|
|
39
|
+
(computedValue) => computedValue * 2
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// 2
|
|
43
|
+
console.log(
|
|
44
|
+
anotherCompute({ randNumber: () => 0 })
|
|
45
|
+
);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Multiple dependencies:
|
|
2
49
|
```ts
|
|
3
|
-
|
|
50
|
+
// Create a service that generates random number
|
|
51
|
+
const randNumber = di.service('randNumber')<() => number>();
|
|
4
52
|
|
|
5
|
-
|
|
6
|
-
const
|
|
53
|
+
// Use the service
|
|
54
|
+
const computeNumber = di.derive(
|
|
55
|
+
[randNumber],
|
|
56
|
+
(getRandNumber) => getRandNumber() + 1,
|
|
57
|
+
);
|
|
7
58
|
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
59
|
+
// Create a service that generates random string
|
|
60
|
+
const randString = di.service('randString')<() => string>();
|
|
61
|
+
|
|
62
|
+
// Use the computed value of `computed`
|
|
63
|
+
const computeString = di.derive(
|
|
64
|
+
[randString, computeNumber],
|
|
65
|
+
(getRandString, number) => getRandString() + ': ' + number,
|
|
13
66
|
);
|
|
14
67
|
|
|
68
|
+
// 'reve: 1'
|
|
15
69
|
console.log(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
70
|
+
computeString({
|
|
71
|
+
randNumber: () => 0,
|
|
72
|
+
randString: () => 'reve',
|
|
19
73
|
}),
|
|
20
74
|
);
|
|
21
75
|
```
|
|
22
76
|
|
|
23
|
-
|
|
77
|
+
Inject not all dependencies:
|
|
78
|
+
```ts
|
|
79
|
+
// Create a service that generates random number
|
|
80
|
+
const randNumber = di.service('randNumber')<() => number>();
|
|
81
|
+
|
|
82
|
+
// Use the service
|
|
83
|
+
const computeNumber = di.derive(
|
|
84
|
+
[randNumber],
|
|
85
|
+
(getRandNumber) => getRandNumber() + 1,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
// Create a service that generates random string
|
|
89
|
+
const randString = di.service('randString')<() => string>();
|
|
90
|
+
|
|
91
|
+
// Use the computed value of `computed`
|
|
92
|
+
const computeString = di.derive(
|
|
93
|
+
[randString, computeNumber],
|
|
94
|
+
(getRandString, number) => getRandString() + ': ' + number,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
// Inject the randNumber service
|
|
98
|
+
const computeString1 = di.inject(computeString, {
|
|
99
|
+
randNumber: () => 0
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// 'reve: 1'
|
|
103
|
+
console.log(
|
|
104
|
+
computeString1({
|
|
105
|
+
randString: () => 'reve',
|
|
106
|
+
}),
|
|
107
|
+
);
|
|
108
|
+
```
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
declare const _: unique symbol;
|
|
2
|
+
type _ = typeof _;
|
|
3
|
+
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
4
|
+
export interface Service<
|
|
5
|
+
T extends string | symbol,
|
|
6
|
+
K
|
|
7
|
+
> {
|
|
8
|
+
0: K;
|
|
9
|
+
[_]: undefined extends K ? { [k in T]? : K } : { [k in T] : K };
|
|
10
|
+
}
|
|
11
|
+
export interface Compute<
|
|
12
|
+
T,
|
|
13
|
+
R
|
|
14
|
+
> {
|
|
15
|
+
(c: T): R;
|
|
16
|
+
0: R;
|
|
17
|
+
[_]: T;
|
|
18
|
+
}
|
|
19
|
+
export type AnyService = Service<string | symbol, any>;
|
|
20
|
+
export type AnyCompute = Compute<any, any>;
|
|
21
|
+
export type Dependency = AnyService | AnyCompute;
|
|
22
|
+
export type InferDependencies<T extends Dependency[]> = { [K in keyof T] : T[K][0] };
|
|
23
|
+
export type InferRecord<T extends Dependency[]> = UnionToIntersection<T[number][_]>;
|
|
24
|
+
/**
|
|
25
|
+
* Create a service
|
|
26
|
+
* @param name - The service name
|
|
27
|
+
*/
|
|
28
|
+
export declare const service: <T extends string | symbol>(name: T) => (<K>() => Service<T, K>);
|
|
29
|
+
/**
|
|
30
|
+
* Create a service that relies on other services
|
|
31
|
+
* @param deps - The service dependencies
|
|
32
|
+
* @param f
|
|
33
|
+
*/
|
|
34
|
+
export declare const derive: <
|
|
35
|
+
const T extends Dependency[],
|
|
36
|
+
const R
|
|
37
|
+
>(deps: T, f: (...args: InferDependencies<T>) => R) => Compute<InferRecord<T>, R>;
|
|
38
|
+
/**
|
|
39
|
+
* Inject some dependencies to the compute
|
|
40
|
+
* @param compute
|
|
41
|
+
* @param deps
|
|
42
|
+
*/
|
|
43
|
+
export declare const inject: <
|
|
44
|
+
T extends AnyCompute,
|
|
45
|
+
D extends Partial<T[_]>
|
|
46
|
+
>(compute: T, deps: D) => Compute<Omit<T[_], keyof D>, T[0]>;
|
|
47
|
+
export {};
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export let service=e=>()=>e;export let derive=(e,t)=>n=>t(...e.map(e=>typeof e===`function`?n[e]??=e(n):n[e]));export let inject=(e,t)=>n=>e({...n,...t});
|
package/package.json
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "udic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A simple dependency injection library",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/re-utils/di.git"
|
|
10
|
+
},
|
|
7
11
|
"type": "module",
|
|
8
12
|
"main": "./index.js",
|
|
9
13
|
"types": "./index.d.ts",
|
|
10
14
|
"exports": {
|
|
11
|
-
"./di": "./di.js",
|
|
12
15
|
".": "./index.js"
|
|
13
16
|
}
|
|
14
17
|
}
|
package/di.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
declare const tag: unique symbol;
|
|
2
|
-
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
3
|
-
export interface Service<
|
|
4
|
-
T extends string | symbol,
|
|
5
|
-
K
|
|
6
|
-
> {
|
|
7
|
-
0: K;
|
|
8
|
-
1: K extends undefined ? { [k in T]? : K } : { [k in T] : K };
|
|
9
|
-
readonly [tag]: null;
|
|
10
|
-
}
|
|
11
|
-
export interface Compute<
|
|
12
|
-
T extends Dependency[],
|
|
13
|
-
R
|
|
14
|
-
> {
|
|
15
|
-
(args: InferRecord<T>): R;
|
|
16
|
-
0: R;
|
|
17
|
-
1: InferRecord<T>;
|
|
18
|
-
readonly [tag]: null;
|
|
19
|
-
}
|
|
20
|
-
export type AnyService = Service<string | symbol, any>;
|
|
21
|
-
export type AnyCompute = Compute<any[], any>;
|
|
22
|
-
export type Dependency = AnyService | AnyCompute;
|
|
23
|
-
export type InferDependencies<T extends Dependency[]> = { [K in keyof T] : T[K][0] };
|
|
24
|
-
export type InferRecord<T extends Dependency[]> = UnionToIntersection<T[number][1]>;
|
|
25
|
-
export declare const service: <T extends string | symbol>(t: T) => (<K>() => Service<T, K>);
|
|
26
|
-
export declare const compute: <
|
|
27
|
-
const T extends Dependency[],
|
|
28
|
-
const R
|
|
29
|
-
>(f: (...args: InferDependencies<NoInfer<T>>) => R, ...deps: T) => Compute<T, R>;
|
|
30
|
-
export {};
|
package/di.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export let service=e=>()=>e;export let compute=(e,...t)=>n=>e(...t.map(e=>typeof e===`function`?n[e]??=e(n):n[e]));
|