27 lines
717 B
TypeScript
27 lines
717 B
TypeScript
|
/**
|
||
|
* Merge two objects together ignoring null values in the incoming data
|
||
|
* @param startingObj The original source object to be merged into
|
||
|
* @param newObj The new incoming data
|
||
|
* @signature
|
||
|
* U.mergeWithoutNulls(startingObj, newObj)
|
||
|
*
|
||
|
* @example
|
||
|
* U.mergeWithoutNulls({a:1, b:2}, {c:null, d:4}) // => { a:1, b:2, d:4}
|
||
|
*/
|
||
|
export function mergeWithoutNulls(startingObj: object, newObj: object) : object {
|
||
|
|
||
|
const _cloneObj = Object.assign({}, startingObj);
|
||
|
|
||
|
for (const key of Object.keys(newObj)) {
|
||
|
|
||
|
const properties = Object.getOwnPropertyDescriptor(newObj, key);
|
||
|
|
||
|
if (properties?.value !== null)
|
||
|
|
||
|
Object.defineProperty(_cloneObj, key, { ...properties });
|
||
|
|
||
|
}
|
||
|
|
||
|
return _cloneObj;
|
||
|
}
|