-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from inversify/refactor/add-common-utils
Add common utils
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/container/libraries/core/src/common/calculations/getSelf.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/container/libraries/core/src/common/calculations/getSelf.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function getSelf<T>(self: T): T { | ||
return self; | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/container/libraries/core/src/common/calculations/isPromise.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/container/libraries/core/src/common/calculations/isPromise.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
} |