From e8466670bc37a091504444b30b0ac3bebb87a1fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Pintos=20L=C3=B3pez?= Date: Sun, 1 Dec 2024 00:55:25 +0100 Subject: [PATCH 1/2] refactor(core): add getSelf --- .../src/common/calculations/getSelf.spec.ts | 23 +++++++++++++++++++ .../core/src/common/calculations/getSelf.ts | 3 +++ 2 files changed, 26 insertions(+) create mode 100644 packages/container/libraries/core/src/common/calculations/getSelf.spec.ts create mode 100644 packages/container/libraries/core/src/common/calculations/getSelf.ts diff --git a/packages/container/libraries/core/src/common/calculations/getSelf.spec.ts b/packages/container/libraries/core/src/common/calculations/getSelf.spec.ts new file mode 100644 index 00000000..b015529d --- /dev/null +++ b/packages/container/libraries/core/src/common/calculations/getSelf.spec.ts @@ -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); + }); + }); +}); diff --git a/packages/container/libraries/core/src/common/calculations/getSelf.ts b/packages/container/libraries/core/src/common/calculations/getSelf.ts new file mode 100644 index 00000000..0a0e6434 --- /dev/null +++ b/packages/container/libraries/core/src/common/calculations/getSelf.ts @@ -0,0 +1,3 @@ +export function getSelf(self: T): T { + return self; +} From d9ab894077edbe5e0981b4c78641590f02034812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Pintos=20L=C3=B3pez?= Date: Sun, 1 Dec 2024 00:55:38 +0100 Subject: [PATCH 2/2] refactor(core): add isPromise --- .../src/common/calculations/isPromise.spec.ts | 56 +++++++++++++++++++ .../core/src/common/calculations/isPromise.ts | 9 +++ 2 files changed, 65 insertions(+) create mode 100644 packages/container/libraries/core/src/common/calculations/isPromise.spec.ts create mode 100644 packages/container/libraries/core/src/common/calculations/isPromise.ts diff --git a/packages/container/libraries/core/src/common/calculations/isPromise.spec.ts b/packages/container/libraries/core/src/common/calculations/isPromise.spec.ts new file mode 100644 index 00000000..a3054fdf --- /dev/null +++ b/packages/container/libraries/core/src/common/calculations/isPromise.spec.ts @@ -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); + }); + }); + }); +}); diff --git a/packages/container/libraries/core/src/common/calculations/isPromise.ts b/packages/container/libraries/core/src/common/calculations/isPromise.ts new file mode 100644 index 00000000..d7b43f82 --- /dev/null +++ b/packages/container/libraries/core/src/common/calculations/isPromise.ts @@ -0,0 +1,9 @@ +export function isPromise(object: unknown): object is Promise { + const isObjectOrFunction: boolean = + (typeof object === 'object' && object !== null) || + typeof object === 'function'; + + return ( + isObjectOrFunction && typeof (object as PromiseLike).then === 'function' + ); +}