From 7020554796569d48d169af3e59798fdd8341cdc0 Mon Sep 17 00:00:00 2001 From: dmitrii Date: Fri, 3 Apr 2020 22:31:55 +0300 Subject: [PATCH] Add support for excluding NaN values --- README.md | 3 ++- index.d.ts | 1 + src/index.js | 12 +++++++++--- test/src/index.test.js | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 50305b5..f70f736 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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` diff --git a/index.d.ts b/index.d.ts index 4917ecb..4e2ea1e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,6 +8,7 @@ export type CleanOptions = { emptyArrays?: boolean; emptyObjects?: boolean; emptyStrings?: boolean; + NaNValues?: boolean; nullValues?: boolean; undefinedValues?: boolean; }; diff --git a/src/index.js b/src/index.js index 7d50107..a9d7823 100755 --- a/src/index.js +++ b/src/index.js @@ -17,6 +17,7 @@ module.exports = function cleanDeep(object, { emptyArrays = true, emptyObjects = true, emptyStrings = true, + NaNValues = false, nullValues = true, undefinedValues = true } = {}) { @@ -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. @@ -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; @@ -68,4 +74,4 @@ module.exports = function cleanDeep(object, { result[key] = value; }); -} +}; diff --git a/test/src/index.test.js b/test/src/index.test.js index 32c1c76..b39645d 100755 --- a/test/src/index.test.js +++ b/test/src/index.test.js @@ -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: {