From d45763888d357133049753a3beaac24ae538f5aa Mon Sep 17 00:00:00 2001 From: DoneDeal0 Date: Sun, 25 Dec 2022 12:01:57 +0100 Subject: [PATCH] feat: finish object diff --- src/object-diff.ts | 63 ++++++++++++---------------------------- test/object-diff.test.ts | 30 +++++++++++-------- 2 files changed, 36 insertions(+), 57 deletions(-) diff --git a/src/object-diff.ts b/src/object-diff.ts index f2d2b82..a64276a 100644 --- a/src/object-diff.ts +++ b/src/object-diff.ts @@ -84,10 +84,11 @@ function getPropertyStatus(subPropertiesDiff: Subproperties[]): DiffStatus { : STATUS.EQUAL; } -function getDeletedMainProperties( - previousValue: Record, +function getDeletedProperties( + previousValue: Record | undefined, nextValue: Record ): { property: string; value: any }[] | undefined { + if (!previousValue) return undefined; const prevKeys = Object.keys(previousValue); const nextKeys = Object.keys(nextValue); const deletedKeys = prevKeys.filter((prevKey) => !nextKeys.includes(prevKey)); @@ -100,34 +101,26 @@ function getDeletedMainProperties( 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; - const nextMatch = nextValue[nextProperty]; - const nextKeys = isObject(nextMatch) ? Object.keys(nextMatch) : []; - const prevKeys = isObject(previousMatch) ? Object.keys(previousMatch) : []; - const deletedKeys = prevKeys.filter( - (previousKey) => !nextKeys.includes(previousKey) - ); - const result = deletedKeys.map((deletedKey) => ({ - property: deletedKey, - value: previousMatch[deletedKey], - })); - if (result.length > 0) return result; - return undefined; -} - function getSubPropertiesDiff( previousValue: Record | undefined, nextValue: Record ): Subproperties[] { const subPropertiesDiff: Subproperties[] = []; let subDiff: Subproperties[]; + const deletedMainSubProperties = getDeletedProperties( + previousValue, + nextValue + ); + if (deletedMainSubProperties) { + deletedMainSubProperties.forEach((deletedProperty) => { + subPropertiesDiff.push({ + name: deletedProperty.property, + previousValue: deletedProperty.value, + currentValue: undefined, + status: STATUS.DELETED, + }); + }); + } Object.entries(nextValue).forEach(([nextSubProperty, nextSubValue]) => { const previousMatch = getPreviousMatch(previousValue, nextSubProperty); if (!!!previousMatch && !!nextSubProperty) { @@ -146,26 +139,6 @@ function getSubPropertiesDiff( if (data && data.length > 0) { subDiff = data; } - const deletedProperties = getDeletedSubProperties( - previousValue, - nextValue, - nextSubProperty - ); - if (deletedProperties) { - deletedProperties.forEach((deletedProperty) => { - const deletedData = { - name: deletedProperty.property, - previousValue: deletedProperty.value, - currentValue: undefined, - status: STATUS.DELETED, - }; - if (subDiff) { - subDiff.push(deletedData); - } else { - subDiff = [deletedData]; - } - }); - } } if (previousMatch) { subPropertiesDiff.push({ @@ -228,7 +201,7 @@ export function getObjectDiff( status: getValueStatus(previousValue, nextValue), }); }); - const deletedProperties = getDeletedMainProperties(prevData, nextData); + const deletedProperties = getDeletedProperties(prevData, nextData); if (deletedProperties) { deletedProperties.forEach((deletedProperty) => { diff.push({ diff --git a/test/object-diff.test.ts b/test/object-diff.test.ts index 1797485..00fa639 100644 --- a/test/object-diff.test.ts +++ b/test/object-diff.test.ts @@ -91,7 +91,7 @@ describe("getObjectDiff", () => { name: "joe", member: false, hobbies: ["golf", "chess"], - age: 66, + nickname: "super joe", }, } ) @@ -123,10 +123,16 @@ describe("getObjectDiff", () => { name: "joe", member: false, hobbies: ["golf", "chess"], - age: 66, + nickname: "super joe", }, status: "updated", subPropertiesDiff: [ + { + name: "age", + previousValue: 66, + currentValue: undefined, + status: "deleted", + }, { name: "name", previousValue: "joe", @@ -146,10 +152,10 @@ describe("getObjectDiff", () => { status: "updated", }, { - name: "age", - previousValue: 66, - currentValue: 66, - status: "equal", + name: "nickname", + previousValue: undefined, + currentValue: "super joe", + status: "added", }, ], }, @@ -268,6 +274,12 @@ describe("getObjectDiff", () => { }, status: "updated", subDiff: [ + { + name: "rugby", + previousValue: ["france"], + currentValue: undefined, + status: "deleted", + }, { name: "football", previousValue: ["psg"], @@ -280,12 +292,6 @@ describe("getObjectDiff", () => { currentValue: ["st andrews"], status: "added", }, - { - name: "rugby", - previousValue: ["france"], - currentValue: undefined, - status: "deleted", - }, ], }, ],