From 446355b451e09da86900f5d8e1a856d548cfa7f5 Mon Sep 17 00:00:00 2001 From: Avior Date: Tue, 14 Mar 2023 11:00:19 +0100 Subject: [PATCH] feat: Add option to clean falsy values in object clean Add a new option to `objectClean` function which allows to clean falsy values (including `undefined` and `null`) from an object when set to `true`. This is achieved by deleting all the falsy values from the object when the option is enabled. The change also includes updates to the corresponding tests. Signed-off-by: Avior --- packages/object-util/__tests__/index.test.ts | 16 ++++++++++++++ packages/object-util/src/ObjectUtil.ts | 22 +++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/packages/object-util/__tests__/index.test.ts b/packages/object-util/__tests__/index.test.ts index 5e6fcb8..ab91400 100644 --- a/packages/object-util/__tests__/index.test.ts +++ b/packages/object-util/__tests__/index.test.ts @@ -324,6 +324,22 @@ describe('Object Clean Tests', () => { objectClean(obj) expect(obj).toEqual({a: '', b: null}) }) + it('should clean undefined when told to', () => { + const obj = {a: '', b: null, c: undefined} + objectClean(obj, {cleanUndefined: true}) + expect(obj).toEqual({a: '', b: null}) + }) + it('should clean deeply when told to', () => { + const obj = {a: '', b: null, c: {aa: undefined}} + objectClean(obj, {deep: true}) + expect(obj).toEqual({a: '', b: null, c: {}}) + }) + + it('should clean falsy values when told to', () => { + const obj = {obj: 'util', a: '', b: null, c: {aa: undefined}} + objectClean(obj, {cleanFalsy: true}) + expect(obj).toEqual({obj: 'util', c: {}}) + }) it('should not clean when options.cleanUndefined is false', () => { const obj2 = {a: '', b: null, c: undefined} objectClean(obj2, {cleanUndefined: false}) diff --git a/packages/object-util/src/ObjectUtil.ts b/packages/object-util/src/ObjectUtil.ts index 800a378..bb136eb 100644 --- a/packages/object-util/src/ObjectUtil.ts +++ b/packages/object-util/src/ObjectUtil.ts @@ -256,27 +256,29 @@ export function objectEqual(first: BasicObject, second: BasicObject): boolean { } /** - * Deeply clean an object from having `undefined` and/or `null` (option to enable) + * Deeply clean an object from having `undefined`,`null` and/or flasy values (options to enable) * * @param obj the object to clean * @param options cleanup options * @param {boolean?} options.cleanUndefined (default: true) clean undefined from the object + * @param {boolean?} options.cleanFalsy (default: false) clean falsy values (including undefined and null) from the object see https://developer.mozilla.org/en-US/docs/Glossary/Falsy * @param {boolean?} options.cleanNull (default: false) clean null from the object * @param {boolean?} options.deep (default: true) deeply clean the object */ -export function objectClean(obj: BasicObject, options?: {cleanUndefined?: boolean, cleanNull?: boolean, deep?: boolean}): void { +export function objectClean(obj: BasicObject, options?: {cleanUndefined?: boolean, cleanNull?: boolean, cleanFalsy?: boolean, deep?: boolean}): void { mustBeObject(obj) objectLoop(obj, (item, key) => { - if ((typeof options?.cleanUndefined === 'undefined' || options?.cleanUndefined) && item === undefined) { - delete obj[key] + if ((typeof options?.cleanUndefined === 'undefined' || options.cleanUndefined) && item === undefined) { + delete obj[key] // clean undefined values + } else if (options?.cleanFalsy && !obj[key]) { + delete obj[key] // clean falsy values + } else if (options?.cleanNull && item === null) { + delete obj[key] // clean null values } - if (options?.cleanNull && item === null) { - delete obj[key] - } - - if ((typeof options?.deep === 'undefined' || options?.deep) && isObject(item)) { - return objectClean(item, options) + // deeply clean the object + if ((typeof options?.deep === 'undefined' || options.deep) && isObject(item)) { + objectClean(item, options) } }) }