Skip to content

Commit

Permalink
Merge pull request #147 from inversify/refactor/add-common-utils
Browse files Browse the repository at this point in the history
Add common utils
  • Loading branch information
notaphplover authored Dec 1, 2024
2 parents fbd9d41 + d9ab894 commit 7dd6742
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { beforeAll, describe, expect, it } from '@jest/globals';

import { getSelf } from './getSelf';

describe(getSelf.name, () => {
let value: unknown;

beforeAll(() => {
value = Symbol();
});

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

beforeAll(() => {
result = getSelf(value);
});

it('should return expected value', () => {
expect(result).toBe(value);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getSelf<T>(self: T): T {
return self;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { beforeAll, describe, expect, it } from '@jest/globals';

import { isPromise } from './isPromise';

describe(isPromise.name, () => {
describe.each<[string, unknown, boolean]>([
['null', null, false],
['a string', 'string-fixture', false],
['a function with no "then" property', () => undefined, false],
['an object with no "then" property', {}, false],
[
'a function with non function "then" property',
(() => {
const value: (() => void) & {
then?: unknown;
} = () => undefined;

value.then = 'fixture';

return value;
})(),
false,
],
['an object with non function "then" property', { then: 'fixture' }, false],
[
'a function with function "then" property',
(() => {
const value: (() => void) & {
then?: unknown;
} = () => undefined;

value.then = () => undefined;

return value;
})(),
true,
],
[
'an object with function "then" property',
{ then: () => undefined },
true,
],
])('having %s', (_: string, value: unknown, expectedResult: boolean) => {
describe('when called', () => {
let result: unknown;

beforeAll(() => {
result = isPromise(value);
});

it('should return expected value', () => {
expect(result).toBe(expectedResult);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function isPromise<T>(object: unknown): object is Promise<T> {
const isObjectOrFunction: boolean =
(typeof object === 'object' && object !== null) ||
typeof object === 'function';

return (
isObjectOrFunction && typeof (object as PromiseLike<T>).then === 'function'
);
}

0 comments on commit 7dd6742

Please sign in to comment.