diff --git a/src/misc.ts b/src/misc.ts index 510c376..ba3c3a5 100644 --- a/src/misc.ts +++ b/src/misc.ts @@ -1,8 +1,12 @@ -export function isObject(val: any) { +export function isObject(val: any): val is Record { return Object.prototype.toString.call(val) === '[object Object]'; } +export function isFunction(val: any): val is (...args: any[]) => any { + return typeof val === 'function'; +} + export function formDataFromObject(obj: Record) { const formData = new FormData(); Object.entries(obj) diff --git a/tests/misc.spec.ts b/tests/misc.spec.ts index f607c25..7b291ef 100644 --- a/tests/misc.spec.ts +++ b/tests/misc.spec.ts @@ -1,4 +1,4 @@ -import { formDataFromObject, isObject, urlSearchParamsFromObject } from '../src/misc'; +import { formDataFromObject, isFunction, isObject, urlSearchParamsFromObject } from '../src/misc'; describe('misc', () => { describe('isObject', () => { @@ -19,6 +19,24 @@ describe('misc', () => { }); }); + describe('isFunction', () => { + it('should return thruthy successfully', () => { + expect(isFunction(function () { return null; })).toBeTruthy(); + }); + + it('should return falsy', () => { + expect(isFunction(1)).toBeFalsy(); + expect(isFunction(true)).toBeFalsy(); + expect(isFunction('')).toBeFalsy(); + expect(isFunction([])).toBeFalsy(); + expect(isFunction({})).toBeFalsy(); + expect(isFunction(null)).toBeFalsy(); + expect(isFunction(undefined)).toBeFalsy(); + expect(isFunction(NaN)).toBeFalsy(); + expect(isFunction(Infinity)).toBeFalsy(); + }); + }); + describe('formDataFromObject', () => { it('should convert object to form data successfully', () => { const formData = formDataFromObject({