chore: add is function helper

This commit is contained in:
Dmitriy Pleshevskiy 2020-11-20 19:44:02 +03:00
parent 078b5c1a3a
commit b944725e07
2 changed files with 24 additions and 2 deletions

View file

@ -1,8 +1,12 @@
export function isObject(val: any) { export function isObject(val: any): val is Record<string, unknown> {
return Object.prototype.toString.call(val) === '[object Object]'; 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<string, any>) { export function formDataFromObject(obj: Record<string, any>) {
const formData = new FormData(); const formData = new FormData();
Object.entries(obj) Object.entries(obj)

View file

@ -1,4 +1,4 @@
import { formDataFromObject, isObject, urlSearchParamsFromObject } from '../src/misc'; import { formDataFromObject, isFunction, isObject, urlSearchParamsFromObject } from '../src/misc';
describe('misc', () => { describe('misc', () => {
describe('isObject', () => { 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', () => { describe('formDataFromObject', () => {
it('should convert object to form data successfully', () => { it('should convert object to form data successfully', () => {
const formData = formDataFromObject({ const formData = formDataFromObject({