Fix #643 Prevent infinite loop in getBounds if bounds.valueRange is very small.

Add check for bounds.step to be greater than EPSILON.
This commit is contained in:
hansmaad 2016-03-18 15:02:04 +01:00
parent e38aa2757d
commit bfdc8f2617
2 changed files with 18 additions and 4 deletions

View File

@ -727,6 +727,10 @@ var Chartist = {
}
}
// step must not be less than EPSILON to create values that can be represented as floating number.
var EPSILON = 2.221E-16;
bounds.step = Math.max(bounds.step, EPSILON);
// Narrow min and max based on new step
newMin = bounds.min;
newMax = bounds.max;
@ -740,11 +744,12 @@ var Chartist = {
bounds.max = newMax;
bounds.range = bounds.max - bounds.min;
bounds.values = [];
for (i = bounds.min; i <= bounds.max; i += bounds.step) {
bounds.values.push(Chartist.roundWithPrecision(i));
var values = [];
for (i = bounds.min; i <= bounds.max; i += bounds.step) {
var value = Chartist.roundWithPrecision(i);
value != values[values.length - 1] && values.push(i);
}
bounds.values = values;
return bounds;
};

View File

@ -385,6 +385,15 @@ describe('Chartist core', function() {
expect(bounds.values).toEqual([0]);
// Is this correct behaviour? Should it be [0, 5]?
});
it('should return single step if range is less than epsilon', function() {
var bounds = Chartist.getBounds(100, { high: 1.0000000000000002, low: 1 }, 20, false);
expect(bounds.min).toBe(1);
expect(bounds.max).toBe(1.0000000000000002);
expect(bounds.low).toBe(1);
expect(bounds.high).toBe(1.0000000000000002);
expect(bounds.values).toEqual([1]);
});
});