Skip to content

Commit

Permalink
refactor: add getBaseType
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Dec 3, 2024
1 parent f7dd2c8 commit 61cae4e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/test/utils/get_base_type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Newable } from '@inversifyjs/common';
import { expect } from 'chai';

import { getBaseType } from '../../utils/get_base_type';

describe(getBaseType.name, () => {
describe('having a type with base type', () => {
let baseTypeFixture: Newable;
let typeFixture: Newable;

before(() => {
class BaseType {}

baseTypeFixture = BaseType;
typeFixture = class extends BaseType {};
});

describe('when called', () => {
let result: unknown;

before(() => {
result = getBaseType(typeFixture);
});

it('should return base type', () => {
expect(result).to.eq(baseTypeFixture);
});
});
});

describe('having a type with no base type', () => {
let typeFixture: Newable;

before(() => {
typeFixture = Object;
});

describe('when called', () => {
let result: unknown;

before(() => {
result = getBaseType(typeFixture);
});

it('should return undefined', () => {
expect(result).to.eq(undefined);
});
});
});
});
16 changes: 16 additions & 0 deletions src/utils/get_base_type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Newable } from '@inversifyjs/common';

interface Prototype {
constructor: Newable;
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function getBaseType(type: Function): Newable | undefined {
const prototype: Prototype | null = Object.getPrototypeOf(
type.prototype,
) as Prototype | null;

const baseType: Newable | undefined = prototype?.constructor;

return baseType;
}

0 comments on commit 61cae4e

Please sign in to comment.