mirror of
https://github.com/dzeiocom/libs.git
synced 2025-04-22 02:42:13 +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:
parent
fd24924d10
commit
446355b451
@ -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})
|
||||
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user