udic 0.0.3 → 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 CHANGED
@@ -1,23 +1,37 @@
1
1
  A simple dependency injection library.
2
2
  ```ts
3
- import { di } from 'udic';
3
+ import * as di from 'udic';
4
4
 
5
- const rand = di.service('rand')<number>();
6
- const computed = di.compute((val) => val + 1, rand);
5
+ // Create a service that generates random number
6
+ const randNumber = di.service('randNumber')<() => number>();
7
7
 
8
- const rand1 = di.service('rand1')<number>();
9
- const computed1 = di.compute(
10
- (val, computed0) => val + computed0,
11
- rand1,
12
- computed,
8
+ // Use the service
9
+ const computed = di.derive(
10
+ [randNumber],
11
+ (getRandNumber) => getRandNumber() + 1
12
+ );
13
+
14
+ // Create a service that generates random string
15
+ const randString = di.service('randString')<() => string>();
16
+
17
+ // Use the computed value of `computed`
18
+ const computed1 = di.derive(
19
+ [randString, computed],
20
+ (getRandString, computedValue) => getRandString() + ': ' + computedValue,
13
21
  );
14
22
 
15
23
  console.log(
24
+ // Provide implementations of services
16
25
  computed1({
17
- rand: 9,
18
- rand1: 9,
26
+ randNumber: () => Math.random(),
27
+ randString: () => crypto.randomUUID(),
19
28
  }),
20
- );
21
- ```
29
+ ); // Output: '<random uuid>: <random number + 1>'
22
30
 
23
- This should only be used to replace singletons.
31
+ console.log(
32
+ computed1({
33
+ randNumber: () => 0,
34
+ randString: () => 'reve'
35
+ })
36
+ ); // Output: 'reve: 1'
37
+ ```
package/index.d.ts CHANGED
@@ -1,2 +1,37 @@
1
- export * as di from "./di.js";
2
- export * from "./di.js";
1
+ declare const _: 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
+ [_]: undefined extends K ? { [k in T]? : K } : { [k in T] : K };
9
+ }
10
+ export interface Compute<
11
+ T extends Dependency[],
12
+ R
13
+ > {
14
+ (args: InferRecord<T>): R;
15
+ 0: R;
16
+ [_]: InferRecord<T>;
17
+ }
18
+ export type AnyService = Service<string | symbol, any>;
19
+ export type AnyCompute = Compute<any[], any>;
20
+ export type Dependency = AnyService | AnyCompute;
21
+ export type InferDependencies<T extends Dependency[]> = { [K in keyof T] : T[K][0] };
22
+ export type InferRecord<T extends Dependency[]> = UnionToIntersection<T[number][typeof _]>;
23
+ /**
24
+ * Create a service
25
+ * @param name - The service name
26
+ */
27
+ export declare const service: <T extends string | symbol>(name: T) => (<K>() => Service<T, K>);
28
+ /**
29
+ * Create a service that relies on other services
30
+ * @param deps - The service dependencies
31
+ * @param f
32
+ */
33
+ export declare const derive: <
34
+ const T extends Dependency[],
35
+ const R
36
+ >(deps: T, f: (...args: InferDependencies<T>) => R) => Compute<T, R>;
37
+ export {};
package/index.js CHANGED
@@ -1 +1 @@
1
- export*as di from"./di.js";export*from"./di.js";
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]));
package/package.json CHANGED
@@ -1,14 +1,17 @@
1
1
  {
2
2
  "name": "udic",
3
- "version": "0.0.3",
3
+ "version": "0.1.0",
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,29 +0,0 @@
1
- declare const tag: unique symbol;
2
- export interface Service<
3
- T extends string | symbol,
4
- K
5
- > {
6
- 0: K;
7
- 1: K extends undefined ? { [k in T]? : K } : { [k in T] : K };
8
- readonly [tag]: null;
9
- }
10
- export interface Compute<
11
- T extends Dependency[],
12
- R
13
- > {
14
- (args: InferDependenciesRecord<T>): R;
15
- 0: R;
16
- 1: InferDependenciesRecord<T>;
17
- readonly [tag]: null;
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[]> = T extends [infer A extends Dependency, ...infer B extends Dependency[]] ? [A[0], ...InferDependencies<B>] : [];
23
- export type InferDependenciesRecord<T extends Dependency[]> = T extends [infer A extends Dependency, ...infer B extends Dependency[]] ? A[1] & InferDependenciesRecord<B> : {};
24
- export declare const service: <T extends string | symbol>(t: T) => (<K>() => Service<T, K>);
25
- export declare const compute: <
26
- const T extends Dependency[],
27
- const R
28
- >(f: (...args: InferDependencies<NoInfer<T>>) => R, ...deps: T) => Compute<T, R>;
29
- 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]));