Skip to content

Add isBound and isBoundCurrent methods to container #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/bindbox/src/__tests__/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,67 @@ describe('Container', () => {
container.get(A);
}).toThrow();
});

describe('isBoundCurrent', () => {
it('should be bound to current container', () => {
class Foo {}

const container = new Container();

container.bind(Foo).toSelf();

expect(container.isBoundCurrent(Foo)).toBe(true);
});

it('should not be bound to parent container', () => {
class Foo {}

const container = new Container();
const childContainer = container.createContainer();

container.bind(Foo).toSelf();

expect(container.isBoundCurrent(Foo)).toBe(true);
expect(childContainer.isBoundCurrent(Foo)).toBe(false);
});

it('should not be bound in container', () => {
class Foo {}

const container = new Container();

expect(container.isBoundCurrent(Foo)).toBe(false);
});
});

describe('isBound', () => {
it('should be bound to current container', () => {
class Foo {}

const container = new Container();

container.bind(Foo).toSelf();

expect(container.isBound(Foo)).toBe(true);
});

it('should be bound to parent container', () => {
class Foo {}

const container = new Container();
const childContainer = container.createContainer();

container.bind(Foo).toSelf();

expect(childContainer.isBound(Foo)).toBe(true);
});

it('should not be bound in container', () => {
class Foo {}

const container = new Container();

expect(container.isBound(Foo)).toBe(false);
});
});
});
16 changes: 16 additions & 0 deletions packages/bindbox/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ export class Container implements ContainerContract {
return this.bind(type);
}

isBound<T>(type: Type<T>): boolean {
if (this.isBoundCurrent(type)) {
return true;
}

if (this.#parent) {
return this.#parent.isBound(type);
}

return false;
}

isBoundCurrent<T>(type: Type<T>): boolean {
return this.#bindings.has(type);
}

resolve<T>(request: ResolutionRequestContract<T>): T | T[] {
if (request.target.isArray()) {
return this.#resolveCollection(request);
Expand Down