udic 0.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 +17 -0
- package/index.d.ts +2 -0
- package/index.js +1 -0
- package/main.d.ts +44 -0
- package/main.js +1 -0
- package/package.json +14 -0
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
## DEI
|
|
2
|
+
A simple dependency injection library
|
|
3
|
+
```ts
|
|
4
|
+
const rand = dependency<number>();
|
|
5
|
+
|
|
6
|
+
const code = compute((value) => {
|
|
7
|
+
return value + 1;
|
|
8
|
+
}, rand);
|
|
9
|
+
|
|
10
|
+
const dependentCode = compute((computedValue) => computedValue * 2, code);
|
|
11
|
+
|
|
12
|
+
const ctx = {
|
|
13
|
+
[rand]: 9
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
dependentCode(ctx);
|
|
17
|
+
```
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export*as udic from"./main.js";export*from"./main.js";
|
package/main.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
2
|
+
export type Dependency<
|
|
3
|
+
Tag extends {
|
|
4
|
+
0: any
|
|
5
|
+
} = any,
|
|
6
|
+
T = any
|
|
7
|
+
> = {
|
|
8
|
+
0: Record<Tag[0], T>
|
|
9
|
+
1: T
|
|
10
|
+
} & symbol;
|
|
11
|
+
export interface Compute<
|
|
12
|
+
T extends Params[] = any,
|
|
13
|
+
R = any
|
|
14
|
+
> {
|
|
15
|
+
(c: UnionToIntersection<T[number][0] & {}>): R;
|
|
16
|
+
0: UnionToIntersection<T[number]>;
|
|
17
|
+
1: R;
|
|
18
|
+
}
|
|
19
|
+
type Params = Dependency | Compute;
|
|
20
|
+
type InferParams<T extends Params[]> = T extends [infer A extends Params, ...infer B extends Params[]] ? [A[1] & {}, ...InferParams<B>] : [];
|
|
21
|
+
/**
|
|
22
|
+
* Create a dependency
|
|
23
|
+
*/
|
|
24
|
+
export declare const dependency: <T>() => Dependency<{
|
|
25
|
+
readonly 0: unique symbol
|
|
26
|
+
}, T>;
|
|
27
|
+
/**
|
|
28
|
+
* Compute another value based on dependencies or other computes
|
|
29
|
+
* @param f
|
|
30
|
+
* @param deps
|
|
31
|
+
* @returns a compute
|
|
32
|
+
*/
|
|
33
|
+
export declare const compute: <
|
|
34
|
+
const T extends Params[],
|
|
35
|
+
const R
|
|
36
|
+
>(f: (...data: InferParams<NoInfer<T>>) => R, ...deps: T) => Compute<T, R>;
|
|
37
|
+
/**
|
|
38
|
+
* Create a dependency context
|
|
39
|
+
* @param x
|
|
40
|
+
*/
|
|
41
|
+
export declare const provide: <T extends {
|
|
42
|
+
[K: Dependency]: (typeof K)[1]
|
|
43
|
+
}>(x: T) => Readonly<UnionToIntersection<keyof T extends Dependency ? (keyof T)[0] : any>>;
|
|
44
|
+
export {};
|
package/main.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export let dependency=Symbol;export let compute=(e,...t)=>n=>{for(let e=0;e<t.length;e++)t[e]=typeof t[e]===`symbol`?n[t[e]]:n[t[e]]??=t[e](n);return e(...t)};export let provide=e=>e;
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "udic",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A simple dependency injection library",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
"./main": "./main.js",
|
|
12
|
+
".": "./index.js"
|
|
13
|
+
}
|
|
14
|
+
}
|