Skip to content

Commit 122d3d5

Browse files
committed
factor out is-valid-range logic into setConvert helper
1 parent ffccc75 commit 122d3d5

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

src/components/rangeslider/defaults.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
'use strict';
1010

11-
var isNumeric = require('fast-isnumeric');
12-
1311
var Lib = require('../../lib');
1412
var attributes = require('./attributes');
1513

@@ -37,11 +35,7 @@ module.exports = function handleDefaults(layoutIn, layoutOut, axName) {
3735
coerce('borderwidth');
3836
coerce('thickness');
3937

40-
coerce('autorange', !(
41-
(containerIn.range || []).length === 2 &&
42-
isNumeric(axOut.r2l(containerIn.range[0])) &&
43-
isNumeric(axOut.r2l(containerIn.range[1]))
44-
));
38+
coerce('autorange', !axOut.isValidRange(containerIn.range));
4539
coerce('range');
4640

4741
// Expand slider range to the axis range

src/plots/cartesian/axis_defaults.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
'use strict';
1111

12-
var isNumeric = require('fast-isnumeric');
1312
var colorMix = require('tinycolor2').mix;
1413

1514
var Registry = require('../../registry');
@@ -93,12 +92,7 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
9392
color: dfltFontColor
9493
});
9594

96-
var validRange = (
97-
(containerIn.range || []).length === 2 &&
98-
isNumeric(containerOut.r2l(containerIn.range[0])) &&
99-
isNumeric(containerOut.r2l(containerIn.range[1]))
100-
);
101-
var autoRange = coerce('autorange', !validRange);
95+
var autoRange = coerce('autorange', !containerOut.isValidRange(containerIn.range));
10296

10397
if(autoRange) coerce('rangemode');
10498

src/plots/cartesian/set_convert.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,15 @@ module.exports = function setConvert(ax) {
402402
return arrayOut;
403403
};
404404

405+
ax.isValidRange = function(range) {
406+
return (
407+
Array.isArray(range) &&
408+
range.length === 2 &&
409+
isNumeric(ax.r2l(range[0])) &&
410+
isNumeric(ax.r2l(range[1]))
411+
);
412+
};
413+
405414
// for autoranging: arrays of objects:
406415
// {val: axis value, pad: pixel padding}
407416
// on the low and high sides

test/jasmine/tests/axes_test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,37 @@ describe('Test axes', function() {
413413
expect(layoutOut.xaxis.calendar).toBe('coptic');
414414
expect(layoutOut.yaxis.calendar).toBe('thai');
415415
});
416+
417+
it('should set autorange to true when input range is invalid', function() {
418+
layoutIn = {
419+
xaxis: { range: 'not-gonna-work' },
420+
xaxis2: { range: [1, 2, 3] },
421+
yaxis: { range: ['a', 2] },
422+
yaxis2: { range: [1, 'b'] },
423+
yaxis3: { range: [null, {}] }
424+
};
425+
426+
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
427+
428+
Axes.list({ _fullLayout: layoutOut }).forEach(function(ax) {
429+
expect(ax.autorange).toBe(true, ax._name);
430+
});
431+
});
432+
433+
it('should set autorange to false when input range is valid', function() {
434+
layoutIn = {
435+
xaxis: { range: [1, 2] },
436+
xaxis2: { range: [-2, 1] },
437+
yaxis: { range: ['1', 2] },
438+
yaxis2: { range: [1, '2'] }
439+
};
440+
441+
supplyLayoutDefaults(layoutIn, layoutOut, fullData);
442+
443+
Axes.list({ _fullLayout: layoutOut }).forEach(function(ax) {
444+
expect(ax.autorange).toBe(false, ax._name);
445+
});
446+
});
416447
});
417448

418449
describe('categoryorder', function() {

0 commit comments

Comments
 (0)