MSP-3205 add cleanEmptyParamsObject utils
This commit is contained in:
1 parent
889abced88
commit
f53d57f026
1 file changed
+33
-1
@@ -16,4 +16,36 @@ const sortObject = (obj: any): any => {
|
|||||||
return sorted
|
return sorted
|
||||||
}
|
}
|
||||||
|
|
||||||
export { sortObject }
|
function isEmpty(value: unknown): boolean {
|
||||||
|
return value === undefined ||
|
||||||
|
value === null ||
|
||||||
|
(typeof value === 'string' && value.trim() === '') ||
|
||||||
|
(typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length === 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanEmptyParamsObject(obj: any): any {
|
||||||
|
if (typeof obj !== 'object' || obj === null) {
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(obj)) {
|
||||||
|
return obj
|
||||||
|
.map(cleanEmptyParamsObject)
|
||||||
|
.filter(item => !isEmpty(item))
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.keys(obj).reduce((acc, key) => {
|
||||||
|
if (key === 'data') {
|
||||||
|
acc[key] = obj[key]
|
||||||
|
} else {
|
||||||
|
const value = cleanEmptyParamsObject(obj[key])
|
||||||
|
if (!isEmpty(value)) {
|
||||||
|
acc[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, {} as Record<string, unknown>)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export { sortObject, cleanEmptyParamsObject }
|
||||||
Reference in new issue
Block a user