1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-07-02 12:19:17 +00:00

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 <f.bouillon@aptatio.com>
This commit is contained in:
2023-03-14 11:00:19 +01:00
parent fd24924d10
commit 446355b451
2 changed files with 28 additions and 10 deletions

View File

@ -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})