smartobject/ts/tools/compareobjects.ts

86 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-07-12 17:40:41 +00:00
import * as plugins from '../smartobject.plugins.js';
2022-03-07 14:28:32 +00:00
export interface IObjectCompareResult {
presentInBothProperties: string[];
missingProperties: string[];
additionalProperties: string[];
nulledProperties: string[];
undefinedProperties: string[];
divergingProperties: string[];
equalProperties: string[];
}
export const compareObjects = (
referenceObjectArg: any,
comparisonObjectArg: any
): IObjectCompareResult => {
const returnComparisonObject = {
missingProperties: [] as string[],
additionalProperties: [] as string[],
presentInBothProperties: [] as string[],
nulledProperties: [] as string[],
undefinedProperties: [] as string[],
divergingProperties: [] as string[],
equalProperties: [] as string[],
};
const allProperties = Object.keys(referenceObjectArg).concat(Object.keys(comparisonObjectArg));
for (const currentProperty of allProperties) {
// lets find presentInBothProperties
2023-07-12 17:40:41 +00:00
if (referenceObjectArg[currentProperty] && comparisonObjectArg[currentProperty]) {
2022-03-07 14:28:32 +00:00
returnComparisonObject.presentInBothProperties.push(currentProperty);
}
// lets find missingProperties
2023-07-12 17:40:41 +00:00
if (referenceObjectArg[currentProperty] && !comparisonObjectArg[currentProperty]) {
2022-03-07 14:28:32 +00:00
returnComparisonObject.missingProperties.push(currentProperty);
}
// lets find additionalProperties
2023-07-12 17:40:41 +00:00
if (!referenceObjectArg[currentProperty] && comparisonObjectArg[currentProperty]) {
2022-03-07 14:28:32 +00:00
returnComparisonObject.additionalProperties.push(currentProperty);
}
// lets find nulledProperties
2023-07-12 17:40:41 +00:00
if (comparisonObjectArg[currentProperty] === null) {
2022-03-07 14:28:32 +00:00
returnComparisonObject.nulledProperties.push(currentProperty);
}
// lets find undefinedProperties
2023-07-12 17:40:41 +00:00
if (comparisonObjectArg[currentProperty] === undefined) {
2022-03-07 14:28:32 +00:00
returnComparisonObject.undefinedProperties.push(currentProperty);
}
// lets find divergingProperties
if (
2023-07-12 17:40:41 +00:00
JSON.stringify(referenceObjectArg[currentProperty]) !==
JSON.stringify(comparisonObjectArg[currentProperty])
2022-03-07 14:28:32 +00:00
) {
returnComparisonObject.divergingProperties.push(currentProperty);
}
// lets find equalProperties
if (
2023-07-12 17:40:41 +00:00
plugins.fastDeepEqual(
referenceObjectArg[currentProperty],
comparisonObjectArg[currentProperty]
)
2022-03-07 14:28:32 +00:00
) {
returnComparisonObject.equalProperties.push(currentProperty);
}
}
for (const currentProperty of Object.keys(returnComparisonObject)) {
const onlyUnique = (value: any, index: number, self: Array<any>) => {
return self.indexOf(value) === index;
};
2023-07-12 17:40:41 +00:00
const uniqueArray =
returnComparisonObject[currentProperty as keyof typeof returnComparisonObject].filter(
onlyUnique
);
returnComparisonObject[currentProperty as keyof typeof returnComparisonObject] = uniqueArray;
2022-03-07 14:28:32 +00:00
}
return returnComparisonObject;
2023-07-12 17:40:41 +00:00
};