2020-07-24 11:37:11 +00:00
|
|
|
import * as plugins from './smartobject.plugins';
|
|
|
|
|
2021-03-22 02:33:01 +00:00
|
|
|
const fastDeepEqual = plugins.fastDeepEqual;
|
2021-03-22 02:33:23 +00:00
|
|
|
export { fastDeepEqual };
|
2021-03-22 02:33:01 +00:00
|
|
|
|
2020-07-24 11:37:11 +00:00
|
|
|
export interface IObjectCompareResult {
|
|
|
|
presentInBothProperties: string[];
|
|
|
|
missingProperties: string[];
|
|
|
|
additionalProperties: string[];
|
|
|
|
nulledProperties: string[];
|
|
|
|
undefinedProperties: string[];
|
|
|
|
divergingProperties: string[];
|
|
|
|
equalProperties: string[];
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:33:23 +00:00
|
|
|
export const compareObjects = (
|
|
|
|
referenceObjectArg: any,
|
|
|
|
comparisonObjectArg: any
|
|
|
|
): IObjectCompareResult => {
|
2020-07-24 11:37:11 +00:00
|
|
|
const returnComparisonObject = {
|
|
|
|
missingProperties: [],
|
|
|
|
additionalProperties: [],
|
|
|
|
presentInBothProperties: [],
|
|
|
|
nulledProperties: [],
|
|
|
|
undefinedProperties: [],
|
|
|
|
divergingProperties: [],
|
|
|
|
equalProperties: [],
|
|
|
|
};
|
2021-03-22 02:33:23 +00:00
|
|
|
|
2020-07-24 11:37:11 +00:00
|
|
|
const allProperties = Object.keys(referenceObjectArg).concat(Object.keys(comparisonObjectArg));
|
|
|
|
for (const currentProperty of allProperties) {
|
|
|
|
// lets find presentInBothProperties
|
|
|
|
if (referenceObjectArg[currentProperty] && comparisonObjectArg[currentProperty]) {
|
|
|
|
returnComparisonObject.presentInBothProperties.push(currentProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// lets find missingProperties
|
|
|
|
if (referenceObjectArg[currentProperty] && !comparisonObjectArg[currentProperty]) {
|
|
|
|
returnComparisonObject.missingProperties.push(currentProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// lets find additionalProperties
|
|
|
|
if (!referenceObjectArg[currentProperty] && comparisonObjectArg[currentProperty]) {
|
|
|
|
returnComparisonObject.additionalProperties.push(currentProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// lets find nulledProperties
|
|
|
|
if (comparisonObjectArg[currentProperty] === null) {
|
|
|
|
returnComparisonObject.nulledProperties.push(currentProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// lets find undefinedProperties
|
|
|
|
if (comparisonObjectArg[currentProperty] === undefined) {
|
|
|
|
returnComparisonObject.undefinedProperties.push(currentProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// lets find divergingProperties
|
2021-03-22 02:33:23 +00:00
|
|
|
if (
|
|
|
|
JSON.stringify(referenceObjectArg[currentProperty]) !==
|
|
|
|
JSON.stringify(comparisonObjectArg[currentProperty])
|
|
|
|
) {
|
2020-07-24 11:37:11 +00:00
|
|
|
returnComparisonObject.divergingProperties.push(currentProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// lets find equalProperties
|
2021-03-22 02:33:23 +00:00
|
|
|
if (
|
|
|
|
JSON.stringify(referenceObjectArg[currentProperty]) ===
|
|
|
|
JSON.stringify(comparisonObjectArg[currentProperty])
|
|
|
|
) {
|
2020-07-24 11:37:11 +00:00
|
|
|
returnComparisonObject.equalProperties.push(currentProperty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const currentProperty of Object.keys(returnComparisonObject)) {
|
2021-03-22 02:33:01 +00:00
|
|
|
const onlyUnique = (value, index, self) => {
|
|
|
|
return self.indexOf(value) === index;
|
|
|
|
};
|
|
|
|
const uniqueArray = returnComparisonObject[currentProperty].filter(onlyUnique);
|
2020-07-24 11:37:11 +00:00
|
|
|
returnComparisonObject[currentProperty] = uniqueArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnComparisonObject;
|
|
|
|
};
|