Skip to content

Commit 116ac06

Browse files
authoredMar 29, 2021
Treat null as undefined. (#241)
* treat null as undefined * undefined isNaN * treat null as undefined * more tests
1 parent c7efc99 commit 116ac06

12 files changed

+30
-14
lines changed
 

‎src/continuous.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function transformer() {
8383
}
8484

8585
function scale(x) {
86-
return isNaN(x = +x) ? unknown : (output || (output = piecewise(domain.map(transform), range, interpolate)))(transform(clamp(x)));
86+
return x == null || isNaN(x = +x) ? unknown : (output || (output = piecewise(domain.map(transform), range, interpolate)))(transform(clamp(x)));
8787
}
8888

8989
scale.invert = function(y) {

‎src/identity.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function identity(domain) {
55
var unknown;
66

77
function scale(x) {
8-
return isNaN(x = +x) ? unknown : x;
8+
return x == null || isNaN(x = +x) ? unknown : x;
99
}
1010

1111
scale.invert = scale;

‎src/quantile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function quantile() {
1515
}
1616

1717
function scale(x) {
18-
return isNaN(x = +x) ? unknown : range[bisect(thresholds, x)];
18+
return x == null || isNaN(x = +x) ? unknown : range[bisect(thresholds, x)];
1919
}
2020

2121
scale.invertExtent = function(y) {

‎src/quantize.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function quantize() {
1111
unknown;
1212

1313
function scale(x) {
14-
return x <= x ? range[bisect(domain, x, 0, n)] : unknown;
14+
return x != null && x <= x ? range[bisect(domain, x, 0, n)] : unknown;
1515
}
1616

1717
function rescale() {

‎src/sequential.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function transformer() {
1818
unknown;
1919

2020
function scale(x) {
21-
return isNaN(x = +x) ? unknown : interpolator(k10 === 0 ? 0.5 : (x = (transform(x) - t0) * k10, clamp ? Math.max(0, Math.min(1, x)) : x));
21+
return x == null || isNaN(x = +x) ? unknown : interpolator(k10 === 0 ? 0.5 : (x = (transform(x) - t0) * k10, clamp ? Math.max(0, Math.min(1, x)) : x));
2222
}
2323

2424
scale.domain = function(_) {

‎src/sequentialQuantile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function sequentialQuantile() {
77
interpolator = identity;
88

99
function scale(x) {
10-
if (!isNaN(x = +x)) return interpolator((bisect(domain, x, 1) - 1) / (domain.length - 1));
10+
if (x != null && !isNaN(x = +x)) return interpolator((bisect(domain, x, 1) - 1) / (domain.length - 1));
1111
}
1212

1313
scale.domain = function(_) {

‎src/threshold.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function threshold() {
88
n = 1;
99

1010
function scale(x) {
11-
return x <= x ? range[bisect(domain, x, 0, n)] : unknown;
11+
return x != null && x <= x ? range[bisect(domain, x, 0, n)] : unknown;
1212
}
1313

1414
scale.domain = function(_) {

‎test/identity-test.js

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ tape("identity(x) coerces input to a number", function(test) {
3131
test.end();
3232
});
3333

34+
tape("identity(undefined) returns unknown", function(test) {
35+
var s = scale.scaleIdentity().unknown(-1);
36+
test.equal(s(undefined), -1);
37+
test.equal(s(null), -1);
38+
test.equal(s(NaN), -1);
39+
test.equal(s("N/A"), -1);
40+
test.equal(s(0.4), 0.4);
41+
test.end();
42+
});
43+
3444
tape("identity.invert(y) is the identity function", function(test) {
3545
var s = scale.scaleIdentity().domain([1, 2]);
3646
test.equal(s.invert(0.5), 0.5);

‎test/linear-test.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ tape("linear.rangeRound(range) accepts an iterable", function(test) {
209209
test.end();
210210
});
211211

212-
tape("linear.unknown(value) sets the return value for undefined and NaN input", function(test) {
212+
tape("linear.unknown(value) sets the return value for undefined, null, and NaN input", function(test) {
213213
var s = scale.scaleLinear().unknown(-1);
214+
test.equal(s(null), -1);
214215
test.equal(s(undefined), -1);
215216
test.equal(s(NaN), -1);
216217
test.equal(s("N/A"), -1);
@@ -388,15 +389,12 @@ tape("linear.ticks(X) spans linear.nice(X).domain()", function(test) {
388389
var ticks = s.ticks(count);
389390
test.deepEqual([ticks[0], ticks[ticks.length - 1]], s.domain());
390391
}
391-
392392
check([1, 9], 2);
393393
check([1, 9], 3);
394394
check([1, 9], 4);
395-
396395
check([8, 9], 2);
397396
check([8, 9], 3);
398397
check([8, 9], 4);
399-
400398
check([1, 21], 2);
401399
check([2, 21], 2);
402400
check([3, 21], 2);
@@ -408,7 +406,6 @@ tape("linear.ticks(X) spans linear.nice(X).domain()", function(test) {
408406
check([9, 21], 2);
409407
check([10, 21], 2);
410408
check([11, 21], 2);
411-
412409
test.end();
413410
})
414411

‎test/quantile-test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ tape("quantile.invertExtent() returns the first match if duplicate values exist
129129
test.end();
130130
});
131131

132-
tape("quantile.unknown(value) sets the return value for undefined and NaN input", function(test) {
132+
tape("quantile.unknown(value) sets the return value for undefined, null, and NaN input", function(test) {
133133
var s = scale.scaleQuantile().domain([3, 6, 7, 8, 8, 10, 13, 15, 16, 20]).range([0, 1, 2, 3]).unknown(-1);
134134
test.equal(s(undefined), -1);
135+
test.equal(s(null), -1);
135136
test.equal(s(NaN), -1);
136137
test.equal(s("N/A"), -1);
137138
test.equal(s(2), 0);

‎test/quantize-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ tape("quantize(value) clamps input values to the domain", function(test) {
3636
test.end();
3737
});
3838

39+
tape("quantize.unknown(value) sets the return value for undefined, null, and NaN input", function(test) {
40+
var s = scale.scaleQuantize().range([0, 1, 2]).unknown(-1);
41+
test.equal(s(undefined), -1);
42+
test.equal(s(null), -1);
43+
test.equal(s(NaN), -1);
44+
test.end();
45+
});
46+
3947
tape("quantize.domain() coerces domain values to numbers", function(test) {
4048
var s = scale.scaleQuantize().domain(["-1.20", "2.40"]);
4149
test.deepEqual(s.domain(), [-1.2, 2.4]);

‎test/threshold-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tape("threshold(x) returns undefined if the specified value x is not orderable",
2626
test.equal(x(), undefined);
2727
test.equal(x(undefined), undefined);
2828
test.equal(x(NaN), undefined);
29-
test.equal(x(null), "a"); // null < 1/3
29+
test.equal(x(null), undefined);
3030
test.end();
3131
});
3232

0 commit comments

Comments
 (0)
Please sign in to comment.