From 57cae0efa732440d776610e95f4436984fec2a23 Mon Sep 17 00:00:00 2001 From: Evyweb Date: Sat, 7 Sep 2024 23:01:35 +0200 Subject: [PATCH] feat: add higher order function dedicated api --- README.md | 8 ++------ specs/container.spec.ts | 5 +++-- src/container.ts | 18 ++++++++++-------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1d6dc17..991daef 100644 --- a/README.md +++ b/README.md @@ -37,14 +37,10 @@ container.bind(DI.DEP1).toValue('dependency1'); container.bind(DI.DEP2).toValue(42); // You can register functions without dependencies (e.g. a const sayHelloWorld = () => 'Hello World') -// This kind of function has no dependecies, so you need to register it without the dependency array container.bind(DI.HELLO_WORLD).toFunction(sayHelloWorld); -// You can register functions with dependencies -container.bind(DI.MY_SERVICE_WITH_DEPENDENCIES).toFunction(MyServiceWithDependencies, [DI.DEP1, DI.DEP2]); - -// Note: If your function has no dependencies but is a higher order function, you have to register it as a function with an empty array of dependencies: -container.bind(DI.SERVICE_WITHOUT_DEPENDENCY).toFunction(ServiceWithoutDependency, []); +// You can register functions with dependencies (any higher order function) +container.bind(DI.MY_SERVICE_WITH_DEPENDENCIES).toHigherOrderFunction(MyServiceWithDependencies, [DI.DEP1, DI.DEP2]); // For more complexe cases, you can register a factory so dep1 and dep2 will be injected container.bind(DI.MY_SERVICE).toFactory(() => { diff --git a/specs/container.spec.ts b/specs/container.spec.ts index 9fa8f47..f70f14e 100644 --- a/specs/container.spec.ts +++ b/specs/container.spec.ts @@ -40,7 +40,7 @@ describe('Container', () => { container.bind(DI.DEP2).toValue(42); container.bind(DI.MY_SERVICE_WITH_DEPENDENCIES) - .toFunction(MyServiceWithDependencies, [DI.DEP1, DI.DEP2]); + .toHigherOrderFunction(MyServiceWithDependencies, [DI.DEP1, DI.DEP2]); // Act const myService = container.get(DI.MY_SERVICE_WITH_DEPENDENCIES); @@ -54,7 +54,8 @@ describe('Container', () => { it('should consider the function as an higher order function', () => { // Arrange container.bind(DI.DEP1).toValue('dependency1'); - container.bind(DI.SERVICE_WITHOUT_DEPENDENCY).toFunction(ServiceWithoutDependency, []); + container.bind(DI.SERVICE_WITHOUT_DEPENDENCY) + .toHigherOrderFunction(ServiceWithoutDependency); // Act const myService = container.get(DI.SERVICE_WITHOUT_DEPENDENCY); diff --git a/src/container.ts b/src/container.ts index ab45775..7b4d65b 100644 --- a/src/container.ts +++ b/src/container.ts @@ -1,7 +1,8 @@ export interface Container { bind(key: symbol): { toValue: (value: unknown) => void; - toFunction: (fn: CallableFunction, dependencies?: symbol[]) => void; + toFunction: (fn: CallableFunction) => void; + toHigherOrderFunction: (fn: CallableFunction, dependencies?: symbol[]) => void; toFactory: (factory: CallableFunction) => void; toClass: (constructor: new (...args: any[]) => C, dependencies: symbol[]) => void; }; @@ -19,12 +20,12 @@ export function createContainer(): Container { function bind(key: symbol) { const toValue = (value: unknown) => values.set(key, value); - const toFunction = (fn: CallableFunction, dependencies?: symbol[]) => { - if (dependencies && Array.isArray(dependencies)) { - factories.set(key, () => fn(...resolveDependencies(dependencies))); - } else { - factories.set(key, () => fn); - } + const toFunction = (fn: CallableFunction) => { + factories.set(key, () => fn); + }; + + const toHigherOrderFunction = (fn: CallableFunction, dependencies: symbol[] = []) => { + factories.set(key, () => fn(...resolveDependencies(dependencies))); }; const toFactory = (factory: CallableFunction) => factories.set(key, factory); @@ -37,7 +38,8 @@ export function createContainer(): Container { toValue, toFunction, toFactory, - toClass + toClass, + toHigherOrderFunction }; }