Skip to content

Commit

Permalink
feat: add higher order function dedicated api
Browse files Browse the repository at this point in the history
  • Loading branch information
Evyweb committed Sep 7, 2024
1 parent 1540cf9 commit 57cae0e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
5 changes: 3 additions & 2 deletions specs/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyServiceInterface>(DI.MY_SERVICE_WITH_DEPENDENCIES);
Expand All @@ -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<ServiceWithoutDependencyInterface>(DI.SERVICE_WITHOUT_DEPENDENCY);
Expand Down
18 changes: 10 additions & 8 deletions src/container.ts
Original file line number Diff line number Diff line change
@@ -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: <C>(constructor: new (...args: any[]) => C, dependencies: symbol[]) => void;
};
Expand All @@ -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);
Expand All @@ -37,7 +38,8 @@ export function createContainer(): Container {
toValue,
toFunction,
toFactory,
toClass
toClass,
toHigherOrderFunction
};
}

Expand Down

0 comments on commit 57cae0e

Please sign in to comment.