diff --git a/package.json b/package.json index ffecf14..d3e0889 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@brainylab/fetch-wrapper", - "version": "0.5.3", + "version": "0.5.4", "keywords": [ "fetch", "wrapper", diff --git a/src/utils/object-to-url-params.spec.ts b/src/utils/object-to-url-params.spec.ts index b7b4a97..09e8f48 100644 --- a/src/utils/object-to-url-params.spec.ts +++ b/src/utils/object-to-url-params.spec.ts @@ -1,17 +1,26 @@ -import { test } from 'vitest'; +import { describe, it, expect } from 'vitest'; import { objectToUrlParams } from './object-to-url-params'; -test('object-to-url-params', () => { - const obj = { a: '1', b: '2', c: '3' }; - const result = objectToUrlParams(obj); +describe('object-to-url-params', () => { + it('should be convert object to url params', () => { + const obj = { a: '1', b: '2', c: '3' }; + const result = objectToUrlParams(obj); - expect(result).toBe('a=1&b=2&c=3'); -}); + expect(result).toBe('a=1&b=2&c=3'); + }); + + it('should be clear undefined or null object', () => { + const obj = { a: undefined, b: null, c: '3' }; + const result = objectToUrlParams(obj); + + expect(result).toBe('c=3'); + }); -test('array-to-url-params', () => { - const obj = { d: [1, 2, 3, 4] }; - const result = objectToUrlParams(obj); + it('should be convert array to url params', () => { + const obj = { d: [1, 2, 3, 4] }; + const result = objectToUrlParams(obj); - expect(result).toBe('d=1&d=2&d=3&d=4'); + expect(result).toBe('d=1&d=2&d=3&d=4'); + }); }); diff --git a/src/utils/object-to-url-params.ts b/src/utils/object-to-url-params.ts index 1576caa..c2c6f8e 100644 --- a/src/utils/object-to-url-params.ts +++ b/src/utils/object-to-url-params.ts @@ -1,7 +1,10 @@ -function cleanObject( - obj: Record, -): Record { - const cleanedObj: Record = {}; +type ObjectToUrl = Record< + string, + string | number | string[] | number[] | undefined | null +>; + +function cleanObject(obj: ObjectToUrl): ObjectToUrl { + const cleanedObj: ObjectToUrl = {}; Object.keys(obj).forEach((key) => { const value = obj[key]; if (value !== undefined && value !== null) { @@ -12,9 +15,7 @@ function cleanObject( return cleanedObj; } -export function objectToUrlParams( - obj: Record, -): string { +export function objectToUrlParams(obj: ObjectToUrl): string { const cleanedObj = cleanObject(obj); const params = new URLSearchParams();