2016-02-02 12:46:14 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2019-12-20 00:57:50 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2016-02-02 12:46:14 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For two objects of the form { key: [val1, val2, val3] }, work out the added/removed
|
|
|
|
* values. Entirely new keys will result in the entire value array being added.
|
|
|
|
* @param {Object} before
|
|
|
|
* @param {Object} after
|
|
|
|
* @return {Object[]} An array of objects with the form:
|
|
|
|
* { key: $KEY, val: $VALUE, place: "add|del" }
|
|
|
|
*/
|
2019-12-20 00:57:50 +00:00
|
|
|
export function getKeyValueArrayDiffs(before, after) {
|
2017-07-01 13:38:32 +00:00
|
|
|
const results = [];
|
|
|
|
const delta = {};
|
2016-02-02 12:46:14 +00:00
|
|
|
Object.keys(before).forEach(function(beforeKey) {
|
|
|
|
delta[beforeKey] = delta[beforeKey] || 0; // init to 0 initially
|
|
|
|
delta[beforeKey]--; // keys present in the past have -ve values
|
|
|
|
});
|
|
|
|
Object.keys(after).forEach(function(afterKey) {
|
|
|
|
delta[afterKey] = delta[afterKey] || 0; // init to 0 initially
|
|
|
|
delta[afterKey]++; // keys present in the future have +ve values
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(delta).forEach(function(muxedKey) {
|
|
|
|
switch (delta[muxedKey]) {
|
|
|
|
case 1: // A new key in after
|
|
|
|
after[muxedKey].forEach(function(afterVal) {
|
|
|
|
results.push({ place: "add", key: muxedKey, val: afterVal });
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case -1: // A before key was removed
|
|
|
|
before[muxedKey].forEach(function(beforeVal) {
|
|
|
|
results.push({ place: "del", key: muxedKey, val: beforeVal });
|
|
|
|
});
|
|
|
|
break;
|
2017-07-01 13:38:32 +00:00
|
|
|
case 0: {// A mix of added/removed keys
|
2016-02-02 12:46:14 +00:00
|
|
|
// compare old & new vals
|
2017-07-01 13:38:32 +00:00
|
|
|
const itemDelta = {};
|
2016-02-02 12:46:14 +00:00
|
|
|
before[muxedKey].forEach(function(beforeVal) {
|
|
|
|
itemDelta[beforeVal] = itemDelta[beforeVal] || 0;
|
|
|
|
itemDelta[beforeVal]--;
|
|
|
|
});
|
|
|
|
after[muxedKey].forEach(function(afterVal) {
|
|
|
|
itemDelta[afterVal] = itemDelta[afterVal] || 0;
|
|
|
|
itemDelta[afterVal]++;
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(itemDelta).forEach(function(item) {
|
|
|
|
if (itemDelta[item] === 1) {
|
|
|
|
results.push({ place: "add", key: muxedKey, val: item });
|
|
|
|
} else if (itemDelta[item] === -1) {
|
|
|
|
results.push({ place: "del", key: muxedKey, val: item });
|
|
|
|
} else {
|
2017-01-20 14:22:27 +00:00
|
|
|
// itemDelta of 0 means it was unchanged between before/after
|
2016-02-02 12:46:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
2017-07-01 13:38:32 +00:00
|
|
|
}
|
2016-02-02 12:46:14 +00:00
|
|
|
default:
|
2017-07-01 13:38:32 +00:00
|
|
|
console.error("Calculated key delta of " + delta[muxedKey] + " - this should never happen!");
|
2016-02-02 12:46:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return results;
|
2019-12-20 00:57:50 +00:00
|
|
|
}
|
2016-03-07 22:27:35 +00:00
|
|
|
|
|
|
|
/**
|
2017-07-01 13:38:32 +00:00
|
|
|
* Shallow-compare two objects for equality: each key and value must be identical
|
|
|
|
* @param {Object} objA First object to compare against the second
|
|
|
|
* @param {Object} objB Second object to compare against the first
|
|
|
|
* @return {boolean} whether the two objects have same key=values
|
2016-03-07 22:27:35 +00:00
|
|
|
*/
|
2019-12-20 00:57:50 +00:00
|
|
|
export function shallowEqual(objA, objB) {
|
2016-03-07 22:27:35 +00:00
|
|
|
if (objA === objB) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof objA !== 'object' || objA === null ||
|
|
|
|
typeof objB !== 'object' || objB === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-01 13:38:32 +00:00
|
|
|
const keysA = Object.keys(objA);
|
|
|
|
const keysB = Object.keys(objB);
|
2016-03-07 22:27:35 +00:00
|
|
|
|
|
|
|
if (keysA.length !== keysB.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-01 13:38:32 +00:00
|
|
|
for (let i = 0; i < keysA.length; i++) {
|
|
|
|
const key = keysA[i];
|
2016-03-07 22:27:35 +00:00
|
|
|
if (!objB.hasOwnProperty(key) || objA[key] !== objB[key]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-12-20 00:57:50 +00:00
|
|
|
}
|