update settings object merging

This commit is contained in:
Georg Fischer
2016-01-13 17:09:54 +01:00
parent a6ba156c38
commit 9e04379d9c
2 changed files with 48 additions and 11 deletions
+31 -1
View File
@@ -27,9 +27,39 @@ define(
return JSON.parse( JSON.stringify( obj ) );
}
function isEqual ( obj1, obj2 ) {
return JSON.stringify( obj1 ) === JSON.stringify( obj2 );
}
// http://stackoverflow.com/a/383245/229189
// add missing properties from obj2
function merge ( obj1, obj2 ) {
obj1 = getCopy( obj1 );
obj2 = getCopy( obj2 );
for ( var p in obj2 ) {
try {
// Property in destination object set; update its value.
if ( typeof obj2[p] === 'object' ) {
obj1[p] = merge( obj1[p], obj2[p] );
} else {
if ( typeof obj1[p] === 'undefined' && typeof obj2[p] !== 'undefined' ) {
obj1[p] = obj2[p];
}
}
} catch ( e ) {
obj1[p] = obj2[p];
}
}
return obj1;
}
return {
getObjectByString: getObjectByString,
getCopy: getCopy
getCopy: getCopy,
merge: merge,
isEqual: isEqual
};
}
);