mirror of
https://github.com/chartjs/Chart.js.git
synced 2025-12-08 20:36:08 +00:00
No longer merge arrays during the config merge. Simply replace the property
This commit is contained in:
parent
c61ab012c4
commit
0817199f45
@ -58,37 +58,23 @@ module.exports = function(Chart) {
|
|||||||
var base = helpers.clone(_base);
|
var base = helpers.clone(_base);
|
||||||
helpers.each(Array.prototype.slice.call(arguments, 1), function(extension) {
|
helpers.each(Array.prototype.slice.call(arguments, 1), function(extension) {
|
||||||
helpers.each(extension, function(value, key) {
|
helpers.each(extension, function(value, key) {
|
||||||
if (key === 'scales') {
|
var baseHasProperty = base.hasOwnProperty(key);
|
||||||
// Scale config merging is complex. Add out own function here for that
|
var baseVal = baseHasProperty ? base[key] : {};
|
||||||
base[key] = helpers.scaleMerge(base.hasOwnProperty(key) ? base[key] : {}, value);
|
|
||||||
|
|
||||||
|
if (key === 'scales') {
|
||||||
|
// Scale config merging is complex. Add our own function here for that
|
||||||
|
base[key] = helpers.scaleMerge(baseVal, value);
|
||||||
} else if (key === 'scale') {
|
} else if (key === 'scale') {
|
||||||
// Used in polar area & radar charts since there is only one scale
|
// Used in polar area & radar charts since there is only one scale
|
||||||
base[key] = helpers.configMerge(base.hasOwnProperty(key) ? base[key] : {}, Chart.scaleService.getScaleDefaults(value.type), value);
|
base[key] = helpers.configMerge(baseVal, Chart.scaleService.getScaleDefaults(value.type), value);
|
||||||
} else if (base.hasOwnProperty(key) && helpers.isArray(base[key]) && helpers.isArray(value)) {
|
} else if (baseHasProperty
|
||||||
// In this case we have an array of objects replacing another array. Rather than doing a strict replace,
|
&& typeof baseVal === 'object'
|
||||||
// merge. This allows easy scale option merging
|
&& !helpers.isArray(baseVal)
|
||||||
var baseArray = base[key];
|
&& baseVal !== null
|
||||||
|
&& typeof value === 'object'
|
||||||
helpers.each(value, function(valueObj, index) {
|
&& !helpers.isArray(value)) {
|
||||||
|
|
||||||
if (index < baseArray.length) {
|
|
||||||
if (typeof baseArray[index] === 'object' && baseArray[index] !== null && typeof valueObj === 'object' && valueObj !== null) {
|
|
||||||
// Two objects are coming together. Do a merge of them.
|
|
||||||
baseArray[index] = helpers.configMerge(baseArray[index], valueObj);
|
|
||||||
} else {
|
|
||||||
// Just overwrite in this case since there is nothing to merge
|
|
||||||
baseArray[index] = valueObj;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
baseArray.push(valueObj); // nothing to merge
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
} else if (base.hasOwnProperty(key) && typeof base[key] === 'object' && base[key] !== null && typeof value === 'object') {
|
|
||||||
// If we are overwriting an object with an object, do a merge of the properties.
|
// If we are overwriting an object with an object, do a merge of the properties.
|
||||||
base[key] = helpers.configMerge(base[key], value);
|
base[key] = helpers.configMerge(baseVal, value);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// can just overwrite the value in this case
|
// can just overwrite the value in this case
|
||||||
base[key] = value;
|
base[key] = value;
|
||||||
|
|||||||
@ -121,7 +121,7 @@ describe('Core helper tests', function() {
|
|||||||
expect(merged).toEqual({
|
expect(merged).toEqual({
|
||||||
valueProp: 5,
|
valueProp: 5,
|
||||||
valueProp2: null,
|
valueProp2: null,
|
||||||
arrayProp: ['a', 'c', 3, 4, 5, 6],
|
arrayProp: ['a', 'c'],
|
||||||
objectProp: {
|
objectProp: {
|
||||||
prop1: 'c',
|
prop1: 'c',
|
||||||
prop2: 56,
|
prop2: 56,
|
||||||
@ -130,37 +130,6 @@ describe('Core helper tests', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should merge arrays containing objects', function() {
|
|
||||||
var baseConfig = {
|
|
||||||
arrayProp: [{
|
|
||||||
prop1: 'abc',
|
|
||||||
prop2: 56
|
|
||||||
}],
|
|
||||||
};
|
|
||||||
|
|
||||||
var toMerge = {
|
|
||||||
arrayProp: [{
|
|
||||||
prop1: 'myProp1',
|
|
||||||
prop3: 'prop3'
|
|
||||||
}, 2, {
|
|
||||||
prop1: 'myProp1'
|
|
||||||
}],
|
|
||||||
};
|
|
||||||
|
|
||||||
var merged = helpers.configMerge(baseConfig, toMerge);
|
|
||||||
expect(merged).toEqual({
|
|
||||||
arrayProp: [{
|
|
||||||
prop1: 'myProp1',
|
|
||||||
prop2: 56,
|
|
||||||
prop3: 'prop3'
|
|
||||||
},
|
|
||||||
2, {
|
|
||||||
prop1: 'myProp1'
|
|
||||||
}
|
|
||||||
],
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should merge scale configs', function() {
|
it('should merge scale configs', function() {
|
||||||
var baseConfig = {
|
var baseConfig = {
|
||||||
scales: {
|
scales: {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user