perf: refs #8217 getDifferences
gitea/salix-front/pipeline/pr-dev There was a failure building this commit
Details
gitea/salix-front/pipeline/pr-dev There was a failure building this commit
Details
This commit is contained in:
parent
1ba41949d1
commit
9b4f3c71c0
|
@ -1,23 +1,54 @@
|
|||
export default function getDifferences(obj1, obj2) {
|
||||
let diff = {};
|
||||
const diff = {};
|
||||
|
||||
delete obj1.$index;
|
||||
if (!obj2) return obj1;
|
||||
delete obj2.$index;
|
||||
const copyObj1 = { ...obj1 };
|
||||
const copyObj2 = obj2 ? { ...obj2 } : {};
|
||||
|
||||
for (let key in obj1) {
|
||||
if (obj2[key] && JSON.stringify(obj1[key]) !== JSON.stringify(obj2[key])) {
|
||||
diff[key] = obj2[key];
|
||||
delete copyObj1.$index;
|
||||
if (copyObj2) delete copyObj2.$index;
|
||||
|
||||
for (const key in copyObj1) {
|
||||
if (copyObj1.hasOwnProperty(key)) {
|
||||
if (
|
||||
!copyObj2.hasOwnProperty(key) ||
|
||||
!deepCompare(copyObj1[key], copyObj2[key])
|
||||
) {
|
||||
diff[key] = copyObj1[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let key in obj2) {
|
||||
if (
|
||||
obj1[key] === undefined ||
|
||||
JSON.stringify(obj1[key]) !== JSON.stringify(obj2[key])
|
||||
) {
|
||||
diff[key] = obj2[key];
|
||||
|
||||
for (const key in copyObj2) {
|
||||
if (copyObj2.hasOwnProperty(key) && !copyObj1.hasOwnProperty(key)) {
|
||||
diff[key] = copyObj2[key];
|
||||
}
|
||||
}
|
||||
|
||||
return diff;
|
||||
}
|
||||
|
||||
function deepCompare(obj1, obj2) {
|
||||
if (
|
||||
typeof obj1 !== 'object' ||
|
||||
obj1 === null ||
|
||||
typeof obj2 !== 'object' ||
|
||||
obj2 === null
|
||||
) {
|
||||
return obj1 === obj2;
|
||||
}
|
||||
|
||||
const keys1 = Object.keys(obj1);
|
||||
const keys2 = Object.keys(obj2);
|
||||
|
||||
if (keys1.length !== keys2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const key of keys1) {
|
||||
if (!obj2.hasOwnProperty(key) || !deepCompare(obj1[key], obj2[key])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue