From 7c838f59b487a039585573e6bcb506dd3ef7c283 Mon Sep 17 00:00:00 2001 From: DoneDeal0 Date: Sat, 24 Dec 2022 13:47:34 +0100 Subject: [PATCH] feat: detect add/removal of main properties --- src/object-diff.ts | 42 ++++++++++++++++++++++++++++++++++++---- test/object-diff.test.ts | 14 ++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/object-diff.ts b/src/object-diff.ts index 64e88de..f2d2b82 100644 --- a/src/object-diff.ts +++ b/src/object-diff.ts @@ -84,11 +84,27 @@ function getPropertyStatus(subPropertiesDiff: Subproperties[]): DiffStatus { : STATUS.EQUAL; } -function getDeletedProperties( +function getDeletedMainProperties( + previousValue: Record, + nextValue: Record +): { 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, nextValue: any, nextProperty: string -) { +): { property: string; value: any }[] | undefined { if (!previousValue) return undefined; const previousMatch = previousValue[nextProperty]; if (!previousMatch) return undefined; @@ -130,7 +146,7 @@ function getSubPropertiesDiff( if (data && data.length > 0) { subDiff = data; } - const deletedProperties = getDeletedProperties( + const deletedProperties = getDeletedSubProperties( previousValue, nextValue, nextSubProperty @@ -184,7 +200,14 @@ export function getObjectDiff( const diff: ObjectDiff["diff"] = []; Object.entries(nextData).forEach(([nextProperty, nextValue]) => { const previousValue = prevData[nextProperty]; - + if (!!!previousValue) { + return diff.push({ + property: nextProperty, + previousValue: undefined, + currentValue: nextValue, + status: STATUS.ADDED, + }); + } if (isObject(nextValue)) { const subPropertiesDiff: Subproperties[] = getSubPropertiesDiff( previousValue, @@ -205,6 +228,17 @@ export function getObjectDiff( 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 { type: "object", status: getObjectStatus(diff), diff --git a/test/object-diff.test.ts b/test/object-diff.test.ts index 48cfe25..1797485 100644 --- a/test/object-diff.test.ts +++ b/test/object-diff.test.ts @@ -76,6 +76,7 @@ describe("getObjectDiff", () => { getObjectDiff( { id: 54, + type: "sport", user: { name: "joe", member: true, @@ -85,6 +86,7 @@ describe("getObjectDiff", () => { }, { id: 54, + country: "us", user: { name: "joe", member: false, @@ -103,6 +105,12 @@ describe("getObjectDiff", () => { currentValue: 54, status: "equal", }, + { + property: "country", + previousValue: undefined, + currentValue: "us", + status: "added", + }, { property: "user", previousValue: { @@ -145,6 +153,12 @@ describe("getObjectDiff", () => { }, ], }, + { + property: "type", + previousValue: "sport", + currentValue: undefined, + status: "deleted", + }, ], }); });