Skip to content

Commit

Permalink
check that typed arrays items aren't all NaNs in hasColorscale
Browse files Browse the repository at this point in the history
- we could have used isNaN here, but isNumeric is fast enough
  that the gains would be negligible.
  • Loading branch information
etpinard committed Feb 28, 2018
1 parent 6dd2f69 commit 306986d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/components/colorscale/has_colorscale.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ var isValidScale = require('./is_valid_scale');

module.exports = function hasColorscale(trace, containerStr) {
var container = containerStr ?
Lib.nestedProperty(trace, containerStr).get() || {} :
trace,
color = container.color,
isArrayWithOneNumber = false;
Lib.nestedProperty(trace, containerStr).get() || {} :
trace;
var color = container.color;

if(Lib.isTypedArray(color)) {
isArrayWithOneNumber = true;
} else if(Array.isArray(color)) {
var isArrayWithOneNumber = false;
if(Lib.isArrayOrTypedArray(color)) {
for(var i = 0; i < color.length; i++) {
if(isNumeric(color[i])) {
isArrayWithOneNumber = true;
Expand Down
35 changes: 35 additions & 0 deletions test/jasmine/tests/colorscale_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,41 @@ describe('Test colorscale:', function() {
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);
});

it('should return true when marker color is a typed array with at least one non-NaN', function() {
trace = {
marker: {
color: new Float32Array([1, 2, 3]),
line: {
color: new Float32Array([2, 3, 4])
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);

trace = {
marker: {
color: new Float32Array([1, NaN, 3]),
line: {
color: new Float32Array([2, 3, NaN])
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);

trace = {
marker: {
color: new Float32Array([NaN, undefined, 'not-a-number']),
line: {
color: new Float32Array(['not-a-number', NaN, undefined])
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(false);
expect(hasColorscale(trace, 'marker.line')).toBe(false);
});
});

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

0 comments on commit 306986d

Please sign in to comment.