Skip to content

Commit

Permalink
Use Math.min/max instead of if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed May 27, 2019
1 parent 6234f56 commit 1c51d8f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 33 deletions.
23 changes: 8 additions & 15 deletions src/scales/scale.linear.js
Expand Up @@ -27,8 +27,8 @@ module.exports = LinearScaleBase.extend({
}

// First Calculate the range
me.min = null;
me.max = null;
me.min = Number.POSITIVE_INFINITY;
me.max = Number.NEGATIVE_INFINITY;

var hasStacks = opts.stacked;
if (hasStacks === undefined) {
Expand Down Expand Up @@ -93,10 +93,8 @@ module.exports = LinearScaleBase.extend({

helpers.each(valuesPerStack, function(valuesForType) {
var values = valuesForType.positiveValues.concat(valuesForType.negativeValues);
var minVal = helpers.min(values);
var maxVal = helpers.max(values);
me.min = me.min === null ? minVal : Math.min(me.min, minVal);
me.max = me.max === null ? maxVal : Math.max(me.max, maxVal);
me.min = Math.min(me.min, helpers.min(values));
me.max = Math.max(me.max, helpers.max(values));
});

} else {
Expand All @@ -111,20 +109,15 @@ module.exports = LinearScaleBase.extend({
continue;
}

if (me.min === null || value.min < me.min) {
me.min = value.min;
}

if (me.max === null || me.max < value.max) {
me.max = value.max;
}
me.min = Math.min(value.min, me.min);
me.max = Math.max(value.max, me.max);
}
}
}
}

me.min = isFinite(me.min) && !isNaN(me.min) ? me.min : DEFAULT_MIN;
me.max = isFinite(me.max) && !isNaN(me.max) ? me.max : DEFAULT_MAX;
me.min = helpers.isFinite(me.min) && !isNaN(me.min) ? me.min : DEFAULT_MIN;
me.max = helpers.isFinite(me.max) && !isNaN(me.max) ? me.max : DEFAULT_MAX;

// Common base implementation to handle ticks.min, ticks.max, ticks.beginAtZero
this.handleTickRangeOptions();
Expand Down
27 changes: 13 additions & 14 deletions src/scales/scale.logarithmic.js
Expand Up @@ -80,9 +80,9 @@ module.exports = Scale.extend({
var datasetIndex, meta, value, data, i, ilen;

// Calculate Range
me.min = null;
me.max = null;
me.minNotZero = null;
me.min = Number.POSITIVE_INFINITY;
me.max = Number.NEGATIVE_INFINITY;
me.minNotZero = Number.POSITIVE_INFINITY;

var hasStacks = opts.stacked;
if (hasStacks === undefined) {
Expand Down Expand Up @@ -131,8 +131,8 @@ module.exports = Scale.extend({
if (valuesForType.length > 0) {
var minVal = helpers.min(valuesForType);
var maxVal = helpers.max(valuesForType);
me.min = me.min === null ? minVal : Math.min(me.min, minVal);
me.max = me.max === null ? maxVal : Math.max(me.max, maxVal);
me.min = Math.min(me.min, minVal);
me.max = Math.max(me.max, maxVal);
}
});

Expand All @@ -148,22 +148,21 @@ module.exports = Scale.extend({
continue;
}

if (me.min === null || value.min < me.min) {
me.min = value.min;
}

if (me.max === null || me.max < value.max) {
me.max = value.max;
}
me.min = Math.min(value.min, me.min);
me.max = Math.max(value.max, me.max);

if (value.min !== 0 && (me.minNotZero === null || value.min < me.minNotZero)) {
me.minNotZero = value.min;
if (value.min !== 0) {
me.minNotZero = Math.min(value.min, me.minNotZero);
}
}
}
}
}

me.min = helpers.isFinite(me.min) ? me.min : null;
me.max = helpers.isFinite(me.max) ? me.max : null;
me.minNotZero = helpers.isFinite(me.minNotZero) ? me.minNotZero : null;

// Common base implementation to handle ticks.min, ticks.max
this.handleTickRangeOptions();
},
Expand Down
4 changes: 2 additions & 2 deletions test/specs/core.ticks.tests.js
Expand Up @@ -45,8 +45,8 @@ describe('Test tick generators', function() {
var xAxis = chart.scales['x-axis-0'];
var yAxis = chart.scales['y-axis-0'];

expect(xAxis.ticks).toEqual(['-1', '-0.8', '-0.6', '-0.4', '-0.2', '0', '0.2', '0.4', '0.6', '0.8', '1']);
expect(yAxis.ticks).toEqual(['1', '0.8', '0.6', '0.4', '0.2', '0', '-0.2', '-0.4', '-0.6', '-0.8', '-1']);
expect(xAxis.ticks).toEqual(['0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1']);
expect(yAxis.ticks).toEqual(['1', '0.9', '0.8', '0.7', '0.6', '0.5', '0.4', '0.3', '0.2', '0.1', '0']);
});

it('Should generate logarithmic spaced ticks with correct precision', function() {
Expand Down
5 changes: 3 additions & 2 deletions test/specs/scale.linear.tests.js
Expand Up @@ -453,7 +453,7 @@ describe('Linear Scale', function() {
});

expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
expect(chart.scales.yScale0.min).toBe(-1);
expect(chart.scales.yScale0.min).toBe(0);
expect(chart.scales.yScale0.max).toBe(1);
});

Expand Down Expand Up @@ -829,10 +829,11 @@ describe('Linear Scale', function() {
var chart = window.acquireChart({
type: 'line',
data: {
labels: [-1, 1],
datasets: [{
xAxisID: 'xScale0',
yAxisID: 'yScale0',
data: []
data: [-1, 1]
}],
},
options: {
Expand Down

0 comments on commit 1c51d8f

Please sign in to comment.