Fixed merge issue

This commit is contained in:
Giuseppe Digilio
2018-06-11 11:55:02 +02:00
parent b17cb180bb
commit 98f79a7f67
4 changed files with 55 additions and 4 deletions

View File

@@ -0,0 +1,52 @@
import { isNotEmpty } from './empty.util';
import { isEqual, isObject, transform } from 'lodash';
/**
* Returns passed object without specified property
*/
export function deleteProperty(object, key): object {
const {[key]: deletedKey, ...otherKeys} = object;
return otherKeys;
}
/**
* Returns true if the passed value is null or undefined.
* hasNoValue(); // true
* hasNoValue(null); // true
* hasNoValue(undefined); // true
* hasNoValue(''); // false
* hasNoValue({}); // false
* hasNoValue([]); // false
* hasNoValue(function() {}); // false
*/
export function isObjectEmpty(obj: any): boolean {
const objectType = typeof obj;
if (objectType === 'object') {
if (Object.keys(obj).length === 0) {
return true;
} else {
let result = true;
for (const key in obj) {
if (isNotEmpty(obj[key])) {
result = false;
break;
}
}
return result;
}
}
}
export function difference(object, base) {
const changes = (o, b) => {
return transform(o, (result, value, key) => {
if (!isEqual(value, b[key]) && isNotEmpty(value)) {
const resultValue = (isObject(value) && isObject(b[key])) ? changes(value, b[key]) : value;
if (!isObjectEmpty(resultValue)) {
result[key] = resultValue;
}
}
});
};
return changes(object, base);
}