Skip to content

Commit 3fefb4a

Browse files
committed
Merge remote-tracking branch 'origin/fix-interpolate' into 3.1.7
2 parents c8b9164 + 756ff5c commit 3fefb4a

6 files changed

Lines changed: 40 additions & 32 deletions

File tree

d3.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4926,7 +4926,9 @@ d3 = function() {
49264926
n--;
49274927
}
49284928
if (s.length === 1) {
4929-
return s[0] == null ? q[0].x : function() {
4929+
return s[0] == null ? (o = q[0].x, function(t) {
4930+
return o(t) + "";
4931+
}) : function() {
49304932
return b;
49314933
};
49324934
}
@@ -4947,7 +4949,7 @@ d3 = function() {
49474949
}
49484950
d3.interpolators = [ function(a, b) {
49494951
var t = typeof b;
4950-
return (t === "string" || t !== typeof a ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_Color ? d3_interpolateRgb : t === "object" ? Array.isArray(b) ? d3_interpolateArray : d3_interpolateObject : d3_interpolateNumber)(a, b);
4952+
return (t === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_Color ? d3_interpolateRgb : t === "object" ? Array.isArray(b) ? d3_interpolateArray : d3_interpolateObject : d3_interpolateNumber)(a, b);
49514953
} ];
49524954
d3.interpolateArray = d3_interpolateArray;
49534955
function d3_interpolateArray(a, b) {

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/interpolate/interpolate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function d3_interpolateByName(name) {
2424
d3.interpolators = [
2525
function(a, b) {
2626
var t = typeof b;
27-
return (t === "string" || t !== typeof a ? (d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString)
27+
return (t === "string" ? (d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString)
2828
: b instanceof d3_Color ? d3_interpolateRgb
2929
: t === "object" ? (Array.isArray(b) ? d3_interpolateArray : d3_interpolateObject)
3030
: d3_interpolateNumber)(a, b);

src/interpolate/string.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ function d3_interpolateString(a, b) {
7373

7474
// Special optimization for only a single match.
7575
if (s.length === 1) {
76-
return s[0] == null ? q[0].x : function() { return b; };
76+
return s[0] == null
77+
? (o = q[0].x, function(t) { return o(t) + ""; })
78+
: function() { return b; };
7779
}
7880

7981
// Otherwise, interpolate each of the numbers and rejoin the string.

test/interpolate/interpolate-test.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ suite.addBatch({
88
"interpolate": {
99
topic: load("interpolate/interpolate").document(),
1010

11-
"when a and b are numbers": {
11+
"when b is a number": {
1212
"interpolates numbers": function(d3) {
1313
assert.strictEqual(d3.interpolate(2, 12)(.4), 6);
14+
},
15+
"coerces a to a number": function(d3) {
16+
assert.strictEqual(d3.interpolate("", 1)(.5), .5);
17+
assert.strictEqual(d3.interpolate("2", 12)(.4), 6);
18+
assert.strictEqual(d3.interpolate([2], 12)(.4), 6);
1419
}
1520
},
1621

17-
"when a and b are color strings": {
22+
"when b is a color string": {
1823
"interpolates RGB values and returns a hexadecimal string": function(d3) {
1924
assert.strictEqual(d3.interpolate("#ff0000", "#008000")(.4), "#993300");
2025
},
@@ -26,10 +31,13 @@ suite.addBatch({
2631
},
2732
"interpolates decimal HSL colors in RGB": function(d3) {
2833
assert.strictEqual(d3.interpolate("hsl(0,100%,50%)", "hsl(120,100%,25%)")(.4), "#993300");
34+
},
35+
"coerces a to a color": function(d3) {
36+
assert.strictEqual(d3.interpolate({toString: function() { return "red"; }}, "green")(.4), "#993300");
2937
}
3038
},
3139

32-
"when a and b are color objects": {
40+
"when b is a color object": {
3341
"interpolates RGB values and returns a hexadecimal string": function(d3) {
3442
assert.strictEqual(d3.interpolate(d3.rgb(255, 0, 0), d3.rgb(0, 128, 0))(.4), "#993300");
3543
},
@@ -41,16 +49,19 @@ suite.addBatch({
4149
},
4250
"interpolates d3.hcl in RGB": function(d3) {
4351
assert.strictEqual(d3.interpolate(d3.hcl("red"), d3.hcl("green"))(.4), "#993300");
52+
},
53+
"coerces a to a color": function(d3) {
54+
assert.strictEqual(d3.interpolate({toString: function() { return "red"; }}, "green")(.4), "#993300");
4455
}
4556
},
4657

47-
"when a and b are strings": {
58+
"when b is a string": {
4859
"interpolates matching numbers in both strings": function(d3) {
4960
assert.strictEqual(d3.interpolate(" 10/20 30", "50/10 100 ")(.4), "26/16 58 ");
5061
},
51-
"if a and b are coercible to numbers, interpolates numbers rather than strings": function(d3) {
52-
assert.strictEqual(d3.interpolate("1.", "2.")(.5), 1.5);
53-
assert.strictEqual(d3.interpolate("1e+3", "1e+4")(.5), 5500);
62+
"if b is coercible to a number, still returns a string": function(d3) {
63+
assert.strictEqual(d3.interpolate("1.", "2.")(.5), "1.5");
64+
assert.strictEqual(d3.interpolate("1e+3", "1e+4")(.5), "5500");
5465
},
5566
"preserves non-numbers in string b": function(d3) {
5667
assert.strictEqual(d3.interpolate(" 10/20 30", "50/10 foo ")(.4), "26/16 foo ");
@@ -60,10 +71,13 @@ suite.addBatch({
6071
},
6172
"preserves equal-value numbers in both strings": function(d3) {
6273
assert.strictEqual(d3.interpolate(" 10/20 100 20", "50/10 100, 20 ")(.4), "26/16 100, 20 ");
74+
},
75+
"coerces a to a string": function(d3) {
76+
assert.strictEqual(d3.interpolate({toString: function() { return "1."; }}, "2.")(.5), "1.5");
6377
}
6478
},
6579

66-
"when a and b are arrays": {
80+
"when b is an array": {
6781
"interpolates each element in b": function(d3) {
6882
assert.strictEqual(JSON.stringify(d3.interpolate([2, 4], [12, 24])(.4)), "[6,12]");
6983
},
@@ -77,7 +91,7 @@ suite.addBatch({
7791
}
7892
},
7993

80-
"when a and b are objects": {
94+
"when b is an object": {
8195
"interpolates each property in b": function(d3) {
8296
assert.deepEqual(d3.interpolate({foo: 2, bar: 4}, {foo: 12, bar: 24})(.4), {foo: 6, bar: 12});
8397
},
@@ -93,16 +107,6 @@ suite.addBatch({
93107
}
94108
},
95109

96-
"when a and b are different types": {
97-
"coerces both types to strings": function(d3) {
98-
assert.strictEqual(d3.interpolate("2", 12)(.4), 6);
99-
assert.strictEqual(d3.interpolate("2px", 12)(.4), 6);
100-
assert.strictEqual(d3.interpolate([2], 12)(.4), 6);
101-
assert.strictEqual(d3.interpolate({valueOf: function() { return 2; }}, 12)(.4), 6);
102-
assert.strictEqual(d3.interpolate({toString: function() { return 2; }}, 12)(.4), 6);
103-
}
104-
},
105-
106110
"may or may not interpolate between enumerable and non-enumerable properties": function(d3) {
107111
var a = Object.create({}, {foo: {value: 1, enumerable: true}}),
108112
b = Object.create({}, {foo: {value: 2, enumerable: false}});

test/interpolate/string-test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ suite.addBatch({
2727
assert.strictEqual(interpolate(" 10/20 100 20", "50/10 100, 20 ")(.4), "26/16 100, 20 ");
2828
},
2929
"interpolates decimal notation correctly": function(interpolate) {
30-
assert.strictEqual(interpolate("1.", "2.")(.5), 1.5);
30+
assert.strictEqual(interpolate("1.", "2.")(.5), "1.5");
3131
},
3232
"interpolates exponent notation correctly": function(interpolate) {
33-
assert.strictEqual(interpolate("1e+3", "1e+4")(.5), 5500);
34-
assert.strictEqual(interpolate("1e-3", "1e-4")(.5), 0.00055);
35-
assert.strictEqual(interpolate("1.e-3", "1.e-4")(.5), 0.00055);
36-
assert.strictEqual(interpolate("-1.e-3", "-1.e-4")(.5), -0.00055);
37-
assert.strictEqual(interpolate("+1.e-3", "+1.e-4")(.5), 0.00055);
38-
assert.strictEqual(interpolate(".1e-2", ".1e-3")(.5), 0.00055);
33+
assert.strictEqual(interpolate("1e+3", "1e+4")(.5), "5500");
34+
assert.strictEqual(interpolate("1e-3", "1e-4")(.5), "0.00055");
35+
assert.strictEqual(interpolate("1.e-3", "1.e-4")(.5), "0.00055");
36+
assert.strictEqual(interpolate("-1.e-3", "-1.e-4")(.5), "-0.00055");
37+
assert.strictEqual(interpolate("+1.e-3", "+1.e-4")(.5), "0.00055");
38+
assert.strictEqual(interpolate(".1e-2", ".1e-3")(.5), "0.00055");
3939
}
4040
}
4141
});

0 commit comments

Comments
 (0)