Skip to content

Commit

Permalink
Prevent explicit first argument deep copy
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrunel committed Jan 29, 2019
1 parent dac95fd commit 1d4b41a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/core/core.controller.js
Expand Up @@ -35,8 +35,13 @@ defaults._set('global', {
responsiveAnimationDuration: 0
});

function mergeScaleConfig(/* objects ... */) {
return helpers.merge(helpers.clone(arguments[0]), [].slice.call(arguments, 1), {
/**
* Recursively merge the given config objects as the `scales` options by
* incorporating scale defaults in `xAxes` and `yAxes` array items, then
* returns a deep copy of the result, thus doesn't alter inputs.
*/
function mergeScaleConfig(/* config objects ... */) {
return helpers.merge({}, Array.prototype.slice.call(arguments), {
merger: function(key, target, source, options) {
if (key === 'xAxes' || key === 'yAxes') {
var slen = source[key].length;
Expand Down Expand Up @@ -70,8 +75,13 @@ function mergeScaleConfig(/* objects ... */) {
});
}

function mergeConfig(/* objects ... */) {
return helpers.merge(helpers.clone(arguments[0]), [].slice.call(arguments, 1), {
/**
* Recursively merge the given config objects as the root options by handling
* default scale options for the `scales` and `scale` properties, then returns
* a deep copy of the result, thus doesn't alter inputs.
*/
function mergeConfig(/* config objects ... */) {
return helpers.merge({}, Array.prototype.slice.call(arguments), {
merger: function(key, target, source, options) {
var tval = target[key] || {};
var sval = source[key];
Expand Down
30 changes: 30 additions & 0 deletions test/specs/core.controller.tests.js
Expand Up @@ -271,6 +271,36 @@ describe('Chart', function() {
_jasmineCheckE: 'e2'
}));
});

it('should not alter defaults when merging config', function() {
var chart = acquireChart({
type: 'line',
options: {
_jasmineCheck: 42,
scales: {
xAxes: [{
id: 'foo',
type: 'linear',
_jasmineCheck: 42,
}],
yAxes: [{
id: 'bar',
type: 'category',
_jasmineCheck: 42,
}]
}
}
});

expect(chart.options._jasmineCheck).toBeDefined();
expect(chart.scales.foo.options._jasmineCheck).toBeDefined();
expect(chart.scales.bar.options._jasmineCheck).toBeDefined();

expect(Chart.defaults.line._jasmineCheck).not.toBeDefined();
expect(Chart.defaults.global._jasmineCheck).not.toBeDefined();
expect(Chart.scaleService.defaults.linear._jasmineCheck).not.toBeDefined();
expect(Chart.scaleService.defaults.category._jasmineCheck).not.toBeDefined();
});
});

describe('config.options.responsive: false', function() {
Expand Down

0 comments on commit 1d4b41a

Please sign in to comment.