1
0
mirror of https://github.com/dzeiocom/libs.git synced 2025-04-22 10:52:11 +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:
Florian Bouillon 2023-03-14 11:00:19 +01:00
parent fd24924d10
commit 446355b451
Signed by: Florian Bouillon
GPG Key ID: E05B3A94178D3A7C
2 changed files with 28 additions and 10 deletions

View File

@ -324,6 +324,22 @@ describe('Object Clean Tests', () => {
objectClean(obj) objectClean(obj)
expect(obj).toEqual({a: '', b: null}) 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', () => { it('should not clean when options.cleanUndefined is false', () => {
const obj2 = {a: '', b: null, c: undefined} const obj2 = {a: '', b: null, c: undefined}
objectClean(obj2, {cleanUndefined: false}) objectClean(obj2, {cleanUndefined: false})

View File

@ -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 obj the object to clean
* @param options cleanup options * @param options cleanup options
* @param {boolean?} options.cleanUndefined (default: true) clean undefined from the object * @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.cleanNull (default: false) clean null from the object
* @param {boolean?} options.deep (default: true) deeply clean 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) mustBeObject(obj)
objectLoop(obj, (item, key) => { objectLoop(obj, (item, key) => {
if ((typeof options?.cleanUndefined === 'undefined' || options?.cleanUndefined) && item === undefined) { if ((typeof options?.cleanUndefined === 'undefined' || options.cleanUndefined) && item === undefined) {
delete obj[key] 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) { // deeply clean the object
delete obj[key] if ((typeof options?.deep === 'undefined' || options.deep) && isObject(item)) {
} objectClean(item, options)
if ((typeof options?.deep === 'undefined' || options?.deep) && isObject(item)) {
return objectClean(item, options)
} }
}) })
} }