Files
mtdb_movie/common/resources/client/utils/objects/remove-empty-values-from-object.ts
maher 703f50a09d
Some checks failed
Build / run (push) Has been cancelled
first commit
2025-10-29 11:42:25 +01:00

30 lines
887 B
TypeScript
Executable File

export function removeEmptyValuesFromObject<T extends Record<string, unknown>>(
obj: T,
options?: {copy?: boolean; deep?: boolean; arrays?: boolean},
): T {
const shouldCopy = options?.copy ?? true;
const newObj = shouldCopy ? {...obj} : obj;
Object.keys(newObj).forEach(_key => {
const key = _key as keyof T;
if (
options?.arrays &&
Array.isArray(newObj[key]) &&
(newObj[key] as any[]).length === 0
) {
delete newObj[key];
} else if (
options?.deep &&
newObj[key] &&
typeof newObj[key] === 'object'
) {
newObj[key] = removeEmptyValuesFromObject(newObj[key] as any, options);
if (Object.keys(newObj[key] as object).length === 0) {
delete newObj[key];
}
} else if (newObj[key] == null || newObj[key] === '') {
delete newObj[key];
}
});
return shouldCopy ? newObj : obj;
}