MOBILE-3320 DX: Implement withoutUndefined helper

main
Noel De Martin 2021-06-21 17:33:59 +02:00
parent b8fc43680e
commit 2d2913f0c9
1 changed files with 24 additions and 0 deletions

View File

@ -16,6 +16,10 @@ export type CoreObjectWithoutEmpty<T> = {
[k in keyof T]: T[k] extends undefined | null ? never : T[k];
};
export type CoreObjectWithoutUndefined<T> = {
[k in keyof T]: T[k] extends undefined ? never : T[k];
};
/**
* Singleton with helper functions for objects.
*/
@ -79,4 +83,24 @@ export class CoreObject {
return cleanObj as CoreObjectWithoutEmpty<T>;
}
/**
* Create a new object without undefined values.
*
* @param obj Objet.
* @return New object without undefined values.
*/
static withoutUndefined<T>(obj: T): CoreObjectWithoutUndefined<T> {
const cleanObj = {};
for (const [key, value] of Object.entries(obj)) {
if (value === undefined) {
continue;
}
cleanObj[key] = value;
}
return cleanObj as CoreObjectWithoutUndefined<T>;
}
}