Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ function isObject(obj: unknown) {
}

function isEmptyObject(obj: unknown) {
return typeof obj === 'object' && obj !== null && !Object.keys(obj).length;
return typeof obj === 'object' && obj !== null && !Array.isArray(obj) && !Object.keys(obj).length;
}

function isEmptyArray(arr: unknown) {
return Array.isArray(arr) && arr.length === 0;
}

interface RemovalOptions {
preserveEmptyArray?: boolean;
removeAllFalsy?: boolean;
}

Expand Down Expand Up @@ -41,6 +46,8 @@ function stripEmptyObjects(obj: any, options: RemovalOptions = {}) {

if (isEmptyObject(value)) {
delete cleanObj[key];
} else if (isEmptyArray(value) && !options.preserveEmptyArray) {
delete cleanObj[key];
} else {
cleanObj[key] = value;
}
Expand All @@ -56,6 +63,8 @@ function stripEmptyObjects(obj: any, options: RemovalOptions = {}) {

if (isEmptyObject(value)) {
delete cleanObj[idx];
} else if (isEmptyArray(value) && !options.preserveEmptyArray) {
delete cleanObj[idx];
} else {
cleanObj[idx] = value;
}
Expand Down Expand Up @@ -83,8 +92,9 @@ export default function removeUndefinedObjects<T>(obj?: T, options?: RemovalOpti
// Then we recursively remove all empty objects and nullish arrays.
withoutUndefined = stripEmptyObjects(withoutUndefined, options);

// If the only thing that's leftover is an empty object then return nothing.
if (isEmptyObject(withoutUndefined)) return undefined;
// If the only thing that's leftover is an empty object or empty array then return nothing.
if (isEmptyObject(withoutUndefined) || (isEmptyArray(withoutUndefined) && !options?.preserveEmptyArray))
return undefined;

return withoutUndefined;
}
6 changes: 6 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ test("should also remove '' and null values when removeAllFalsy is true", () =>
expect(removeUndefinedObjects({ value: undefined }, { removeAllFalsy: true })).toBeUndefined();
});

test('should not remove empty arrays when preserveEmptyArray is true', () => {
expect(removeUndefinedObjects({ value: [] }, { preserveEmptyArray: true })).toStrictEqual({ value: [] });
expect(removeUndefinedObjects({ value: [undefined] }, { preserveEmptyArray: true })).toStrictEqual({ value: [] });
expect(removeUndefinedObjects({ value: [null] }, { preserveEmptyArray: true })).toStrictEqual({ value: [] });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add another case in here for an array with non-falsy values to assert that it is left alone. Also can you add some more examples for arrays in a nested object?

});

test('should remove empty objects with only empty properties', () => {
const obj = {
a: {
Expand Down