Skip to content

Commit 6d622c2

Browse files
committed
-
1 parent 520f86d commit 6d622c2

1 file changed

Lines changed: 31 additions & 37 deletions

File tree

library_standard.js

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -93,59 +93,53 @@ class PuvoxLibrary {
9393
arrayIntersect(source, comparedTo){
9494
return source.filter(x => comparedTo.includes(x));
9595
}
96-
arrayDiffFull(o1,o2) { // never version: https://pastebin_com/VFnY1Xnb
97-
const selfFunc = this;
98-
const typeObject = function(o){
99-
return typeof o === 'object';
100-
};
101-
const bothAreObjects = (o1,o2) =>{
102-
return (typeObject(o1) && typeObject(o2));
103-
};
104-
const bothAreArrays = (o1,o2) =>{
105-
return (this.isArray(o1) && this.isArray(o2));
106-
};
107-
const diff = function (o1, o2) {
96+
object_diff_deep(old_obj, new_obj) {
97+
const isObject = o => typeof o ==='object';
98+
const diff = (o1, o2) => {
10899
const result = {};
100+
// if they are arrays
101+
if (this.isArray(o1) && this.isArray(o2)){
102+
return this.arrayDiff(o1,o2); // non recursive, todo fix
103+
}
109104
// if first is item is not object
110-
if (!typeObject(o1) && typeObject(o2)) {
105+
else if (!isObject(o1) && isObject(o2)) {
111106
return o2;
112107
}
113108
// if second is item is not object
114-
else if (typeObject(o1) && !typeObject(o2)) {
109+
else if (isObject(o1) && !isObject(o2)) {
115110
return undefined;
116111
}
117112
// if they are equal
118113
else if (Object.is(o1, o2)) {
119114
return undefined;
120-
} else if (bothAreArrays(o1,o2)){
121-
return selfFunc.arrayDiff(o1,o2);
122-
}
123-
const keys = Object.keys(o2);
124-
for (let i=0; i<keys.length; i++) {
125-
const key = keys[i];
126-
// if both are objects
127-
if ( bothAreObjects(o1[key],o2[key])) {
128-
// if equal, return nothing
129-
if ( Object.is(o1[key], o2[key]) ) {
130-
// do nothing
131-
} else if (o1[key] === o2[key]) {
132-
// do nothing
133-
} else {
115+
}
116+
// else both objects, but different
117+
else {
118+
for (const key in o2) {
119+
// if both are objects
120+
if ( isObject(o1[key]) && isObject(o2[key])) {
121+
// if equal, return nothing
122+
if ( Object.is(o1[key], o2[key]) ) {
123+
// do nothing
124+
} else if (o1[key] === o2[key]) {
125+
// do nothing
126+
} else {
127+
result[key] = diff(o1[key],o2[key]);
128+
}
129+
} else if (this.isArray(o1[key]) && this.isArray(o2[key])) {
134130
result[key] = diff(o1[key],o2[key]);
131+
} else if (o1[key] !== o2[key]) {
132+
result[key] = o2[key];
133+
} else {
134+
// do nothing
135135
}
136-
} else if (bothAreArrays(o1[key],o2[key])) {
137-
result[key] = diff(o1[key],o2[key]);
138-
} else if (o1[key] !== o2[key]) {
139-
result[key] = o2[key];
140-
} else {
141-
// do nothing
142136
}
137+
return result;
143138
}
144-
return result;
145139
};
146140
return [
147-
diff(o1,o2),
148-
diff(o2,o1),
141+
diff(old_obj, new_obj),
142+
diff(new_obj, old_obj),
149143
];
150144
}
151145

0 commit comments

Comments
 (0)