Browse Source

feat: finish object diff

pull/6/head
DoneDeal0 2 years ago
parent
commit
d45763888d
  1. 63
      src/object-diff.ts
  2. 30
      test/object-diff.test.ts

63
src/object-diff.ts

@ -84,10 +84,11 @@ function getPropertyStatus(subPropertiesDiff: Subproperties[]): DiffStatus { @@ -84,10 +84,11 @@ function getPropertyStatus(subPropertiesDiff: Subproperties[]): DiffStatus {
: STATUS.EQUAL;
}
function getDeletedMainProperties(
previousValue: Record<string, any>,
function getDeletedProperties(
previousValue: Record<string, any> | undefined,
nextValue: Record<string, any>
): { 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( @@ -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<string, any> | undefined,
nextValue: Record<string, any>
): 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( @@ -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( @@ -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({

30
test/object-diff.test.ts

@ -91,7 +91,7 @@ describe("getObjectDiff", () => { @@ -91,7 +91,7 @@ describe("getObjectDiff", () => {
name: "joe",
member: false,
hobbies: ["golf", "chess"],
age: 66,
nickname: "super joe",
},
}
)
@ -123,10 +123,16 @@ describe("getObjectDiff", () => { @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -280,12 +292,6 @@ describe("getObjectDiff", () => {
currentValue: ["st andrews"],
status: "added",
},
{
name: "rugby",
previousValue: ["france"],
currentValue: undefined,
status: "deleted",
},
],
},
],

Loading…
Cancel
Save