2019-01-02 20:36:00 +01:00
|
|
|
import Debug from '../conf/Debug';
|
|
|
|
|
2018-08-05 23:48:56 +02:00
|
|
|
class ObjectCopy {
|
2019-10-20 21:11:09 +02:00
|
|
|
static addNew(current, newValues){
|
2018-08-05 23:48:56 +02:00
|
|
|
|
|
|
|
// clone target
|
2019-10-20 21:11:09 +02:00
|
|
|
var out = JSON.parse(JSON.stringify(newValues));
|
2018-08-05 23:48:56 +02:00
|
|
|
|
2019-10-20 21:11:09 +02:00
|
|
|
if(! current) {
|
2018-08-29 00:41:26 +02:00
|
|
|
if(Debug.debug) {
|
2018-08-05 23:48:56 +02:00
|
|
|
console.log("[ObjectCopy::addNew] There's no existing value. Returning target value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var k in out) {
|
|
|
|
// if current key exist, replace it with existing value. Take no action otherwise.
|
2019-10-20 21:11:09 +02:00
|
|
|
if(current[k]) {
|
2018-08-05 23:48:56 +02:00
|
|
|
|
|
|
|
// Types and constructors of objects must match. If they don't, we always use the new value.
|
2019-10-20 21:11:09 +02:00
|
|
|
if(typeof out[k] === typeof current[k] && out[k].constructor === current[k].constructor) {
|
2018-08-05 23:48:56 +02:00
|
|
|
|
|
|
|
// objects are special, we need to check them recursively.
|
|
|
|
if(out[k] && typeof out[k] === 'object' && out[k].constructor === Object ) {
|
|
|
|
if(Debug.debug && Debug.settings) {
|
|
|
|
console.log("[ObjectCopy::addNew] current key contains an object. Recursing!")
|
|
|
|
}
|
|
|
|
|
2019-10-20 21:11:09 +02:00
|
|
|
out[k] = this.addNew(current[k], out[k]);
|
2018-08-05 23:48:56 +02:00
|
|
|
} else {
|
2019-10-20 21:11:09 +02:00
|
|
|
out[k] = current[k];
|
2018-08-05 23:48:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-22 22:50:32 +02:00
|
|
|
|
|
|
|
// add the values that would otherwise be deleted back to our object. (We need that so user-defined
|
|
|
|
// sites don't get forgotten)
|
2019-10-20 21:11:09 +02:00
|
|
|
for(var k in current) {
|
2018-09-22 22:50:32 +02:00
|
|
|
if (! out[k]) {
|
2019-10-20 21:11:09 +02:00
|
|
|
out[k] = current[k];
|
2018-09-22 22:50:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2018-08-05 23:48:56 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 21:11:09 +02:00
|
|
|
static overwrite(current, newValues){
|
|
|
|
for(var k in newValues) {
|
2019-07-05 23:45:29 +02:00
|
|
|
// if current key exist, replace it with existing value. Take no action otherwise.
|
2019-10-20 21:11:09 +02:00
|
|
|
if (current[k] !== undefined) {
|
2019-07-05 23:45:29 +02:00
|
|
|
// Types and constructors of objects must match. If they don't, we always use the new value.
|
2019-10-20 21:11:09 +02:00
|
|
|
if (typeof newValues[k] === typeof current[k] && newValues[k].constructor === current[k].constructor) {
|
2019-07-05 23:45:29 +02:00
|
|
|
|
|
|
|
// objects are special, we need to check them recursively.
|
2019-10-20 21:11:09 +02:00
|
|
|
if(current[k] && typeof current[k] === 'object' && current[k].constructor === Object ) {
|
2019-07-05 23:45:29 +02:00
|
|
|
if(Debug.debug && Debug.settings) {
|
|
|
|
console.log("[ObjectCopy::addNew] current key contains an object. Recursing!")
|
|
|
|
}
|
|
|
|
|
2019-10-20 21:11:09 +02:00
|
|
|
current[k] = this.overwrite(current[k], newValues[k]);
|
2019-07-05 23:45:29 +02:00
|
|
|
} else {
|
2019-10-20 21:11:09 +02:00
|
|
|
current[k] = newValues[k];
|
2019-07-05 23:45:29 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-20 21:11:09 +02:00
|
|
|
current[k] = newValues[k];
|
2019-07-05 23:45:29 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 21:11:09 +02:00
|
|
|
else if (newValues[k] !== undefined) {
|
|
|
|
current[k] = newValues[k];
|
|
|
|
}
|
2019-07-05 23:45:29 +02:00
|
|
|
}
|
2019-10-20 21:11:09 +02:00
|
|
|
return current;
|
2019-07-05 23:45:29 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 23:48:56 +02:00
|
|
|
static pruneUnused(existing, target, ignoreKeys) {
|
|
|
|
// TODO: implement at some other date
|
|
|
|
// existing: object that we have.
|
|
|
|
// target: object that we want
|
|
|
|
// ignoreKeys: if key is an object, we don't recursively call this function on that key
|
|
|
|
}
|
2018-12-30 23:16:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ObjectCopy;
|