Skip to content

Commit

Permalink
Add support for excluding NaN values
Browse files Browse the repository at this point in the history
  • Loading branch information
FFKL authored and nunofgs committed Aug 19, 2020
1 parent 9d8313d commit 7020554
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# clean-deep

Removes empty _objects_, _arrays_, empty _strings_, _null_ and _undefined_ values from objects. Does not alter the original object.
Removes empty _objects_, _arrays_, empty _strings_, _NaN_, _null_ and _undefined_ values from objects. Does not alter the original object.

As of version 3.0.0, _clean-deep_ traverses arrays as well as objects.

Expand Down Expand Up @@ -30,6 +30,7 @@ _cleanValues_ | _[]_ | Remove specific values, ie: `['foo', 'bar',
_emptyArrays_ | _true_ | Remove empty arrays, ie: `[]`
_emptyObjects_ | _true_ | Remove empty objects, ie: `{}`
_emptyStrings_ | _true_ | Remove empty strings, ie: `''`
_NaNValues_ | _false_ | Remove NaN values, ie: `NaN`
_nullValues_ | _true_ | Remove null values, ie: `null`
_undefinedValues_ | _true_ | Remove undefined values, ie: `undefined`

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type CleanOptions = {
emptyArrays?: boolean;
emptyObjects?: boolean;
emptyStrings?: boolean;
NaNValues?: boolean;
nullValues?: boolean;
undefinedValues?: boolean;
};
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function cleanDeep(object, {
emptyArrays = true,
emptyObjects = true,
emptyStrings = true,
NaNValues = false,
nullValues = true,
undefinedValues = true
} = {}) {
Expand All @@ -25,10 +26,10 @@ module.exports = function cleanDeep(object, {
if (cleanKeys.includes(key)) {
return;
}

// Recurse into arrays and objects.
if (Array.isArray(value) || isPlainObject(value)) {
value = cleanDeep(value, { cleanKeys, cleanValues, emptyArrays, emptyObjects, emptyStrings, nullValues, undefinedValues });
value = cleanDeep(value, { NaNValues, cleanKeys, cleanValues, emptyArrays, emptyObjects, emptyStrings, nullValues, undefinedValues });
}

// Exclude specific values.
Expand All @@ -51,6 +52,11 @@ module.exports = function cleanDeep(object, {
return;
}

// Exclude NaN values.
if (NaNValues && Number.isNaN(value)) {
return;
}

// Exclude null values.
if (nullValues && value === null) {
return;
Expand All @@ -68,4 +74,4 @@ module.exports = function cleanDeep(object, {

result[key] = value;
});
}
};
37 changes: 37 additions & 0 deletions test/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,43 @@ describe('cleanDeep()', () => {
});
});

it('should exclude NaN values', () => {
const object = {
bar: NaN,
foo: {
bar: NaN,
biz: 33,
qux: [NaN, 1, { foo: 'foo'}]
}
};

expect(cleanDeep(object, { NaNValues: true })).toEqual({
foo: {
biz: 33,
qux: [1, { foo: 'foo'}]
}
})
});

it('should include NaN values if `NaNValues` is `false`', () => {
const object = {
bar: NaN,
foo: {
bar: NaN,
biz: null,
qux: [NaN, undefined, { foo: 'foo'}]
}
};

expect(cleanDeep(object, { NaNValues: false })).toEqual({
bar: NaN,
foo: {
bar: NaN,
qux: [NaN, { foo: 'foo'}]
}
})
});

it('should include null values if `nullValues` is `false`', () => {
const object = {
foo: {
Expand Down

0 comments on commit 7020554

Please sign in to comment.