Skip to content

Commit c98aa39

Browse files
committed
make is-valid colorscale array more strict
1 parent c9960b1 commit c98aa39

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/components/colorscale/is_valid_scale_array.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@ var tinycolor = require('tinycolor2');
1414

1515
module.exports = function isValidScaleArray(scl) {
1616
var isValid = true,
17-
highestVal = 0,
18-
si;
19-
20-
if(!Array.isArray(scl)) return false;
21-
else {
22-
if(+scl[0][0] !== 0 || +scl[scl.length - 1][0] !== 1) return false;
23-
for(var i = 0; i < scl.length; i++) {
24-
si = scl[i];
25-
if(si.length !== 2 || +si[0] < highestVal || !tinycolor(si[1]).isValid()) {
26-
isValid = false;
27-
break;
28-
}
29-
highestVal = +si[0];
17+
highestVal = 0;
18+
19+
if(!Array.isArray(scl) || scl.length === 0) return false;
20+
21+
if(!scl[0] || !scl[scl.length - 1]) return false;
22+
23+
if(+scl[0][0] !== 0 || +scl[scl.length - 1][0] !== 1) return false;
24+
25+
for(var i = 0; i < scl.length; i++) {
26+
var si = scl[i];
27+
28+
if(si.length !== 2 || +si[0] < highestVal || !tinycolor(si[1]).isValid()) {
29+
isValid = false;
30+
break;
3031
}
31-
return isValid;
32+
33+
highestVal = +si[0];
3234
}
35+
36+
return isValid;
3337
};

test/jasmine/tests/colorscale_test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ describe('Test colorscale:', function() {
2020

2121
it('should accept only array of 2-item arrays', function() {
2222
expect(isValidScale('a')).toBe(false);
23+
expect(isValidScale([])).toBe(false);
24+
expect(isValidScale([null, undefined])).toBe(false);
25+
expect(isValidScale([{}, [1, 'rgb(0, 0, 200']])).toBe(false);
26+
expect(isValidScale([[0, 'rgb(200, 0, 0)'], {}])).toBe(false);
27+
expect(isValidScale([[0, 'rgb(0, 0, 200)'], undefined])).toBe(false);
28+
expect(isValidScale([null, [1, 'rgb(0, 0, 200)']])).toBe(false);
29+
expect(isValidScale(['a', 'b'])).toBe(false);
2330
expect(isValidScale(['a'])).toBe(false);
2431
expect(isValidScale([['a'], ['b']])).toBe(false);
2532

test/jasmine/tests/lib_test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,6 @@ describe('Test lib.js:', function() {
905905
bad3 = [ ['red'], ['blue']],
906906
bad4 = ['red', 'blue'];
907907

908-
// Fails at [], should be an easy fix though.
909-
910908
var shouldPass = ['Viridis', 'Greens', good],
911909
shouldFail = ['red', 1, undefined, null, {}, [], bad, bad2, bad3, bad4];
912910

0 commit comments

Comments
 (0)