Skip to content

Commit 306986d

Browse files
committed
check that typed arrays items aren't all NaNs in hasColorscale
- we could have used isNaN here, but isNumeric is fast enough that the gains would be negligible.
1 parent 6dd2f69 commit 306986d

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/components/colorscale/has_colorscale.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ var isValidScale = require('./is_valid_scale');
1414

1515
module.exports = function hasColorscale(trace, containerStr) {
1616
var container = containerStr ?
17-
Lib.nestedProperty(trace, containerStr).get() || {} :
18-
trace,
19-
color = container.color,
20-
isArrayWithOneNumber = false;
17+
Lib.nestedProperty(trace, containerStr).get() || {} :
18+
trace;
19+
var color = container.color;
2120

22-
if(Lib.isTypedArray(color)) {
23-
isArrayWithOneNumber = true;
24-
} else if(Array.isArray(color)) {
21+
var isArrayWithOneNumber = false;
22+
if(Lib.isArrayOrTypedArray(color)) {
2523
for(var i = 0; i < color.length; i++) {
2624
if(isNumeric(color[i])) {
2725
isArrayWithOneNumber = true;

test/jasmine/tests/colorscale_test.js

+35
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,41 @@ describe('Test colorscale:', function() {
190190
expect(hasColorscale(trace, 'marker')).toBe(true);
191191
expect(hasColorscale(trace, 'marker.line')).toBe(true);
192192
});
193+
194+
it('should return true when marker color is a typed array with at least one non-NaN', function() {
195+
trace = {
196+
marker: {
197+
color: new Float32Array([1, 2, 3]),
198+
line: {
199+
color: new Float32Array([2, 3, 4])
200+
}
201+
}
202+
};
203+
expect(hasColorscale(trace, 'marker')).toBe(true);
204+
expect(hasColorscale(trace, 'marker.line')).toBe(true);
205+
206+
trace = {
207+
marker: {
208+
color: new Float32Array([1, NaN, 3]),
209+
line: {
210+
color: new Float32Array([2, 3, NaN])
211+
}
212+
}
213+
};
214+
expect(hasColorscale(trace, 'marker')).toBe(true);
215+
expect(hasColorscale(trace, 'marker.line')).toBe(true);
216+
217+
trace = {
218+
marker: {
219+
color: new Float32Array([NaN, undefined, 'not-a-number']),
220+
line: {
221+
color: new Float32Array(['not-a-number', NaN, undefined])
222+
}
223+
}
224+
};
225+
expect(hasColorscale(trace, 'marker')).toBe(false);
226+
expect(hasColorscale(trace, 'marker.line')).toBe(false);
227+
});
193228
});
194229

195230
describe('handleDefaults (heatmap-like version)', function() {

0 commit comments

Comments
 (0)