Skip to content

Instantly share code, notes, and snippets.

@allex
Created January 10, 2013 07:57
Show Gist options
  • Save allex/4500294 to your computer and use it in GitHub Desktop.
Save allex/4500294 to your computer and use it in GitHub Desktop.
var util = exports || {};
util.mergeDeep = function(A, B, depth) {
var forever = depth == null;
for (var p in B) {
if (B[p] != null && B[p].constructor == Object && (forever || depth > 0)) {
A[p] = util.mergeDeep(A.hasOwnProperty(p) ? A[p] : {}, B[p], forever ? null : depth - 1);
} else {
A[p] = B[p];
}
}
return A;
};
util.merge = function(A, B) {
return util.mergeDeep(A, B, 0);
};
util.mergeCopy = function(A, B, depth) {
var A_copy = util.mergeDeep({}, A);
return util.mergeDeep(A_copy, B, depth);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment