Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
test(object-to-url): adicioando teste de remocão de nullish no object…
Browse files Browse the repository at this point in the history
… to url params
  • Loading branch information
andrefelipeschulle committed Aug 2, 2024
1 parent 9cb856e commit 0608988
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@brainylab/fetch-wrapper",
"version": "0.5.3",
"version": "0.5.4",
"keywords": [
"fetch",
"wrapper",
Expand Down
29 changes: 19 additions & 10 deletions src/utils/object-to-url-params.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
15 changes: 8 additions & 7 deletions src/utils/object-to-url-params.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
function cleanObject(
obj: Record<string, string | number | string[] | number[] | undefined | null>,
): Record<string, string | number | string[] | number[]> {
const cleanedObj: Record<string, string | number | string[] | number[]> = {};
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) {
Expand All @@ -12,9 +15,7 @@ function cleanObject(
return cleanedObj;
}

export function objectToUrlParams(
obj: Record<string, string | number | string[] | number[]>,
): string {
export function objectToUrlParams(obj: ObjectToUrl): string {
const cleanedObj = cleanObject(obj);
const params = new URLSearchParams();

Expand Down

0 comments on commit 0608988

Please sign in to comment.