]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
No longer merge arrays during the config merge. Simply replace the property
authoretimberg <evert.timberg@gmail.com>
Sun, 9 Oct 2016 20:24:47 +0000 (16:24 -0400)
committerEvert Timberg <evert.timberg+github@gmail.com>
Mon, 10 Oct 2016 12:14:58 +0000 (08:14 -0400)
src/core/core.helpers.js
test/core.helpers.tests.js

index d3fd727419d004dadfb130006a41594bd43d0de7..2cafdcb6ca2e3df8c8cd7c0071aa4006eb6c8ffa 100644 (file)
@@ -58,37 +58,23 @@ module.exports = function(Chart) {
                var base = helpers.clone(_base);
                helpers.each(Array.prototype.slice.call(arguments, 1), function(extension) {
                        helpers.each(extension, function(value, key) {
-                               if (key === 'scales') {
-                                       // Scale config merging is complex. Add out own function here for that
-                                       base[key] = helpers.scaleMerge(base.hasOwnProperty(key) ? base[key] : {}, value);
+                               var baseHasProperty = base.hasOwnProperty(key);
+                               var baseVal = baseHasProperty ? base[key] : {};
 
+                               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') {
                                        // 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);
-                               } else if (base.hasOwnProperty(key) && helpers.isArray(base[key]) && helpers.isArray(value)) {
-                                       // In this case we have an array of objects replacing another array. Rather than doing a strict replace,
-                                       // merge. This allows easy scale option merging
-                                       var baseArray = base[key];
-
-                                       helpers.each(value, function(valueObj, index) {
-
-                                               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') {
+                                       base[key] = helpers.configMerge(baseVal, Chart.scaleService.getScaleDefaults(value.type), value);
+                               } else if (baseHasProperty
+                                               && typeof baseVal === 'object'
+                                               && !helpers.isArray(baseVal)
+                                               && baseVal !== null
+                                               && typeof value === 'object'
+                                               && !helpers.isArray(value)) {
                                        // 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 {
                                        // can just overwrite the value in this case
                                        base[key] = value;
index 8ca5bb59cb4e4ca3d20d1ac5126cace1e31378d4..3d431606234fb17cdfb498102fd59ea77ffa671e 100644 (file)
@@ -121,7 +121,7 @@ describe('Core helper tests', function() {
                expect(merged).toEqual({
                        valueProp: 5,
                        valueProp2: null,
-                       arrayProp: ['a', 'c', 3, 4, 5, 6],
+                       arrayProp: ['a', 'c'],
                        objectProp: {
                                prop1: 'c',
                                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() {
                var baseConfig = {
                        scales: {