Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ console.log(removeUndefinedObjects({ key: [], key2: 123 }));

## Behavior

Any items with the following value will be removed:
Any items with the following value will be removed by default:

- Empty object, `{}`
- Empty array, `[]`
Expand All @@ -34,6 +34,26 @@ The following items will NOT be removed:

## Options

### `preserveEmptyArray`

Optional boolean.
If provided, empty arrays `[]` will not get removed

```js
import removeUndefinedObjects from 'remove-undefined-objects';

console.log(removeUndefinedObjects({ key1: [], key2: [undefined], nested: { key3: 'a', key4: [] } }));
// { nested: { key3: 'a' } }

console.log(
removeUndefinedObjects(
{ key1: [], key2: [undefined], nested: { key3: 'a', key4: [] } },
{ preserveEmptyArray: true },
),
);
// { key1: [], key2: [], nested: { key3: 'a', key4: [] } }
```

### `removeAllFalsy`

Optional boolean.
Expand All @@ -42,6 +62,6 @@ If provided, the empty string `''` and `null` will be removed as well.
```js
import removeUndefinedObjects from 'remove-undefined-objects';

console.log(removeUndefinedObjects({ key1: null, key2: 123, key3: '' }), { removeAllFalsy: true });
console.log(removeUndefinedObjects({ key1: null, key2: 123, key3: '' }, { removeAllFalsy: true }));
// { key2: 123 }
```
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 @@ -42,6 +47,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 @@ -57,6 +64,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 @@ -84,8 +93,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;
}
30 changes: 30 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ 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(
{ key1: [], key2: [undefined], nested: { key3: 'a', key4: [] } },
{ preserveEmptyArray: true },
),
).toStrictEqual({ key1: [], key2: [], nested: { key3: 'a', key4: [] } });
expect(
removeUndefinedObjects(
{ value: { a: 'a', nested: { b: 'b', nested2: { c: [undefined], d: [] } } } },
{ preserveEmptyArray: true },
),
).toStrictEqual({ value: { a: 'a', nested: { b: 'b', nested2: { c: [], d: [] } } } });
});

test('should leave alone non-falsey values when preserveEmptyArray is true', () => {
expect(removeUndefinedObjects({ value: { a: [1, 2, 3] }, b: null }, { preserveEmptyArray: true })).toStrictEqual({
value: { a: [1, 2, 3] },
b: null,
});
expect(
removeUndefinedObjects(
{ value: { a: [1, undefined], nested: { b: 'b', c: [undefined], d: [] } } },
{ preserveEmptyArray: true },
),
).toStrictEqual({ value: { a: [1], nested: { b: 'b', c: [], d: [] } } });
});

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