Skip to content

Commit e55edd1

Browse files
committed
👍 Add isSyncFunction and isAsyncFunction
1 parent 5478044 commit e55edd1

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

is.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,24 @@ export function isFunction(x: unknown): x is (...args: unknown[]) => unknown {
281281
return x instanceof Function;
282282
}
283283

284+
/**
285+
* Return `true` if the type of `x` is `function` (non async function).
286+
*/
287+
export function isSyncFunction(
288+
x: unknown,
289+
): x is (...args: unknown[]) => unknown {
290+
return Object.prototype.toString.call(x) === "[object Function]";
291+
}
292+
293+
/**
294+
* Return `true` if the type of `x` is `function` (async function).
295+
*/
296+
export function isAsyncFunction(
297+
x: unknown,
298+
): x is (...args: unknown[]) => unknown {
299+
return Object.prototype.toString.call(x) === "[object AsyncFunction]";
300+
}
301+
284302
/**
285303
* Return `true` if the type of `x` is instance of `ctor`.
286304
*
@@ -502,6 +520,8 @@ export default {
502520
RecordOf: isRecordOf,
503521
ObjectOf: isObjectOf,
504522
Function: isFunction,
523+
SyncFunction: isSyncFunction,
524+
AsyncFunction: isAsyncFunction,
505525
InstanceOf: isInstanceOf,
506526
Null: isNull,
507527
Undefined: isUndefined,

is_test.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import is, {
1414
isAny,
1515
isArray,
1616
isArrayOf,
17+
isAsyncFunction,
1718
isBigInt,
1819
isBoolean,
1920
isFunction,
@@ -31,6 +32,7 @@ import is, {
3132
isRecordOf,
3233
isString,
3334
isSymbol,
35+
isSyncFunction,
3436
isTupleOf,
3537
isUndefined,
3638
isUniformTupleOf,
@@ -45,7 +47,8 @@ const examples = {
4547
boolean: [true, false],
4648
array: [[], [0, 1, 2], ["a", "b", "c"], [0, "a", true]],
4749
record: [{}, { a: 0, b: 1, c: 2 }, { a: "a", b: "b", c: "c" }],
48-
function: [function a() {}, () => {}, async function b() {}, async () => {}],
50+
syncFunction: [function a() {}, () => {}],
51+
asyncFunction: [async function b() {}, async () => {}],
4952
null: [null],
5053
undefined: [undefined],
5154
symbol: [Symbol("a"), Symbol("b"), Symbol("c")],
@@ -121,7 +124,8 @@ Deno.test("isAny", async (t) => {
121124
"boolean",
122125
"array",
123126
"record",
124-
"function",
127+
"syncFunction",
128+
"asyncFunction",
125129
"null",
126130
"undefined",
127131
"symbol",
@@ -453,7 +457,21 @@ Deno.test("isObjectOf<T>", async (t) => {
453457
});
454458

455459
Deno.test("isFunction", async (t) => {
456-
await testWithExamples(t, isFunction, { validExamples: ["function"] });
460+
await testWithExamples(t, isFunction, {
461+
validExamples: ["syncFunction", "asyncFunction"],
462+
});
463+
});
464+
465+
Deno.test("isSyncFunction", async (t) => {
466+
await testWithExamples(t, isSyncFunction, {
467+
validExamples: ["syncFunction"],
468+
});
469+
});
470+
471+
Deno.test("isAsyncFunction", async (t) => {
472+
await testWithExamples(t, isAsyncFunction, {
473+
validExamples: ["asyncFunction"],
474+
});
457475
});
458476

459477
Deno.test("isInstanceOf<T>", async (t) => {
@@ -694,7 +712,17 @@ Deno.test("isOptionalOf<T>", async (t) => {
694712
});
695713
await t.step("with isFunction", async (t) => {
696714
await testWithExamples(t, isOptionalOf(isFunction), {
697-
validExamples: ["function", "undefined"],
715+
validExamples: ["syncFunction", "asyncFunction", "undefined"],
716+
});
717+
});
718+
await t.step("with isSyncFunction", async (t) => {
719+
await testWithExamples(t, isOptionalOf(isSyncFunction), {
720+
validExamples: ["syncFunction", "undefined"],
721+
});
722+
});
723+
await t.step("with isAsyncFunction", async (t) => {
724+
await testWithExamples(t, isOptionalOf(isAsyncFunction), {
725+
validExamples: ["asyncFunction", "undefined"],
698726
});
699727
});
700728
await t.step("with isNull", async (t) => {

0 commit comments

Comments
 (0)