Skip to content

Commit

Permalink
Set up a scenario that makes use of ambient modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Mar 24, 2022
1 parent 2c22a6b commit a57fa0c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"eslint-plugin-jsdoc": "^36.1.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"is-callable": "^1.2.4",
"jest": "^26.4.2",
"prettier": "^2.2.1",
"prettier-plugin-packagejson": "^2.2.11",
Expand Down
21 changes: 21 additions & 0 deletions src/callMeMaybe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { mocked } from 'ts-jest/utils';
import isCallable from 'is-callable';
import callMeMaybe from './callMeMaybe';

jest.mock('is-callable');

const mockedIsCallable = mocked(isCallable, true);

describe('callMeMaybe', () => {
it('returns the result of the callable when isCallable returns true', () => {
mockedIsCallable.mockReturnValue(true);
const fn = () => 42;
expect(callMeMaybe(fn)).toBe(42);
});

it('returns null when isCallable returns false', () => {
mockedIsCallable.mockReturnValue(false);
const fn = () => 42;
expect(callMeMaybe(fn)).toBeNull();
});
});
16 changes: 16 additions & 0 deletions src/callMeMaybe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import isCallable from 'is-callable';

/**
* Returns whether the given argument is callable or not.
*
* @param callable - A function.
* @returns Whether the given argument is callable or not.
*/
function maybeCall(callable: any): any {
if (isCallable(callable)) {
return callable();
}
return null;
}

export default maybeCall;
5 changes: 5 additions & 0 deletions src/dependencies.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'is-callable' {
export default function isCallable<T>(
value: any,
): value is (...args: any[]) => T;
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,11 @@ is-callable@^1.1.4, is-callable@^1.2.3:
resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.2.3.tgz#8b1e0500b73a1d76c70487636f368e519de8db8e"
integrity sha512-J1DcMe8UYTBSrKezuIUTUwjXsho29693unXM2YhJUTR2txK/eG47bvNa/wipPFmZFgr/N6f1GA66dv0mEyTIyQ==

is-callable@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.2.4.tgz#47301d58dd0259407865547853df6d61fe471945"
integrity sha512-nsuwtxZfMX67Oryl9LCQ+upnC0Z0BgpwntpS89m1H/TLF0zNfzfLMV/9Wa/6MZsj0acpEjAO0KF1xT6ZdLl95w==

is-ci@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/is-ci/-/is-ci-2.0.0.tgz#6bc6334181810e04b5c22b3d589fdca55026404c"
Expand Down

0 comments on commit a57fa0c

Please sign in to comment.