Skip to content

Commit

Permalink
fix: tests for clean and recursive behavior resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhaticus committed Nov 10, 2024
1 parent 65452ce commit 88241e9
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
100 changes: 100 additions & 0 deletions __tests__/serializers/utils/clean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Chance } from 'chance';
import { clean } from '../../../src/serializers/utils/clean';

describe('`clean`', () => {
let chance: Chance.Chance;

beforeEach(() => {
chance = new Chance();
});

it('should return an empty object when given an empty object', () => {
const result = clean({});

expect(result).toEqual({});
});

it('should removed undefined values from the given object', () => {
const object = {
a: chance.string(),
b: undefined,
};

const result = clean(object);

expect(result).toEqual({
a: object.a,
});
});

it('should remove undefined values, recursively, from the given object', () => {
const object = {
a: {
b: undefined,
},
c: chance.string(),
};

const result = clean(object);

expect(result).toEqual({
c: object.c,
});
});

it('should remove empty objects from the given object', () => {
const object = {
a: {},
b: chance.string(),
};

const result = clean(object);

expect(result).toEqual({
b: object.b,
});
});

it('should remove empty objects, recursively, from the given object', () => {
const object = {
a: {
b: {},
},
c: chance.string(),
};

const result = clean(object);

expect(result).toEqual({
c: object.c,
});
});

it('should remove empty arrays from the given object', () => {
const object = {
a: [],
b: chance.string(),
};

const result = clean(object);

expect(result).toEqual({
b: object.b,
});
});

it('should remove empty arrays, recursively, from the given object', () => {
const object = {
a: {
b: [],
},
c: chance.string(),
};

const result = clean(object);

expect(result).toEqual({
c: object.c,
});
});
});
8 changes: 7 additions & 1 deletion src/serializers/utils/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ export const clean = <O extends object = object>(object: O): O => {
return acc;
}

acc[key] = clean(value);
const child = clean(value);

if (Object.keys(child).length === 0) {
return acc;
}

acc[key] = child;

return acc;
}
Expand Down
1 change: 1 addition & 0 deletions src/serializers/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './assertMetadataIsPresent';
export * from './getMetadataBySymbol';
export * from './collect';
export * from './clean';

0 comments on commit 88241e9

Please sign in to comment.