Browse Source

feat: detect add/removal of main properties

pull/6/head
DoneDeal0 2 years ago
parent
commit
7c838f59b4
  1. 42
      src/object-diff.ts
  2. 14
      test/object-diff.test.ts

42
src/object-diff.ts

@ -84,11 +84,27 @@ function getPropertyStatus(subPropertiesDiff: Subproperties[]): DiffStatus {
: STATUS.EQUAL; : STATUS.EQUAL;
} }
function getDeletedProperties( function getDeletedMainProperties(
previousValue: Record<string, any>,
nextValue: Record<string, any>
): { property: string; value: any }[] | undefined {
const prevKeys = Object.keys(previousValue);
const nextKeys = Object.keys(nextValue);
const deletedKeys = prevKeys.filter((prevKey) => !nextKeys.includes(prevKey));
if (deletedKeys.length > 0) {
return deletedKeys.map((deletedKey) => ({
property: deletedKey,
value: previousValue[deletedKey],
}));
}
return undefined;
}
function getDeletedSubProperties(
previousValue: any, previousValue: any,
nextValue: any, nextValue: any,
nextProperty: string nextProperty: string
) { ): { property: string; value: any }[] | undefined {
if (!previousValue) return undefined; if (!previousValue) return undefined;
const previousMatch = previousValue[nextProperty]; const previousMatch = previousValue[nextProperty];
if (!previousMatch) return undefined; if (!previousMatch) return undefined;
@ -130,7 +146,7 @@ function getSubPropertiesDiff(
if (data && data.length > 0) { if (data && data.length > 0) {
subDiff = data; subDiff = data;
} }
const deletedProperties = getDeletedProperties( const deletedProperties = getDeletedSubProperties(
previousValue, previousValue,
nextValue, nextValue,
nextSubProperty nextSubProperty
@ -184,7 +200,14 @@ export function getObjectDiff(
const diff: ObjectDiff["diff"] = []; const diff: ObjectDiff["diff"] = [];
Object.entries(nextData).forEach(([nextProperty, nextValue]) => { Object.entries(nextData).forEach(([nextProperty, nextValue]) => {
const previousValue = prevData[nextProperty]; const previousValue = prevData[nextProperty];
if (!!!previousValue) {
return diff.push({
property: nextProperty,
previousValue: undefined,
currentValue: nextValue,
status: STATUS.ADDED,
});
}
if (isObject(nextValue)) { if (isObject(nextValue)) {
const subPropertiesDiff: Subproperties[] = getSubPropertiesDiff( const subPropertiesDiff: Subproperties[] = getSubPropertiesDiff(
previousValue, previousValue,
@ -205,6 +228,17 @@ export function getObjectDiff(
status: getValueStatus(previousValue, nextValue), status: getValueStatus(previousValue, nextValue),
}); });
}); });
const deletedProperties = getDeletedMainProperties(prevData, nextData);
if (deletedProperties) {
deletedProperties.forEach((deletedProperty) => {
diff.push({
property: deletedProperty.property,
previousValue: deletedProperty.value,
currentValue: undefined,
status: STATUS.DELETED,
});
});
}
return { return {
type: "object", type: "object",
status: getObjectStatus(diff), status: getObjectStatus(diff),

14
test/object-diff.test.ts

@ -76,6 +76,7 @@ describe("getObjectDiff", () => {
getObjectDiff( getObjectDiff(
{ {
id: 54, id: 54,
type: "sport",
user: { user: {
name: "joe", name: "joe",
member: true, member: true,
@ -85,6 +86,7 @@ describe("getObjectDiff", () => {
}, },
{ {
id: 54, id: 54,
country: "us",
user: { user: {
name: "joe", name: "joe",
member: false, member: false,
@ -103,6 +105,12 @@ describe("getObjectDiff", () => {
currentValue: 54, currentValue: 54,
status: "equal", status: "equal",
}, },
{
property: "country",
previousValue: undefined,
currentValue: "us",
status: "added",
},
{ {
property: "user", property: "user",
previousValue: { previousValue: {
@ -145,6 +153,12 @@ describe("getObjectDiff", () => {
}, },
], ],
}, },
{
property: "type",
previousValue: "sport",
currentValue: undefined,
status: "deleted",
},
], ],
}); });
}); });

Loading…
Cancel
Save