Skip to content

Instantly share code, notes, and snippets.

@TorsteinHonsi
Created December 19, 2014 10:10
Show Gist options
  • Save TorsteinHonsi/f646f39d51d18b7d6bfb to your computer and use it in GitHub Desktop.
Save TorsteinHonsi/f646f39d51d18b7d6bfb to your computer and use it in GitHub Desktop.
/**
* @license Highcharts Merge
*
* (c) 2009-2014 Torstein Honsi
*
* License: MIT
*
* This function is part of the Highcharts core, and shared under an MIT license on
* user request.
*/
/**
* Deep merge two or more objects and return a third object. If the first argument is
* true, the contents of the second object is copied into the first object.
* Previously this function redirected to jQuery.extend(true), but this had two limitations.
* First, it deep merged arrays, which lead to workarounds in Highcharts. Second,
* it copied properties from extended prototypes.
*/
function merge() {
var i,
args = arguments,
len,
ret = {},
doCopy = function (copy, original) {
var value, key;
// An object is replacing a primitive
if (typeof copy !== 'object') {
copy = {};
}
for (key in original) {
if (original.hasOwnProperty(key)) {
value = original[key];
// Copy the contents of objects, but not arrays or DOM nodes
if (value && typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]'
&& key !== 'renderTo' && typeof value.nodeType !== 'number') {
copy[key] = doCopy(copy[key] || {}, value);
// Primitives and arrays are copied over directly
} else {
copy[key] = original[key];
}
}
}
return copy;
};
// If first argument is true, copy into the existing object. Used in setOptions.
if (args[0] === true) {
ret = args[1];
args = Array.prototype.slice.call(args, 2);
}
// For each argument, extend the return
len = args.length;
for (i = 0; i < len; i++) {
ret = doCopy(ret, args[i]);
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment