Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix-interpolate' into 3.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 15, 2013
2 parents c8b9164 + 756ff5c commit 3fefb4a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
6 changes: 4 additions & 2 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4926,7 +4926,9 @@ d3 = function() {
n--;
}
if (s.length === 1) {
return s[0] == null ? q[0].x : function() {
return s[0] == null ? (o = q[0].x, function(t) {
return o(t) + "";
}) : function() {
return b;
};
}
Expand All @@ -4947,7 +4949,7 @@ d3 = function() {
}
d3.interpolators = [ function(a, b) {
var t = typeof b;
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);
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);
} ];
d3.interpolateArray = d3_interpolateArray;
function d3_interpolateArray(a, b) {
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/interpolate/interpolate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function d3_interpolateByName(name) {
d3.interpolators = [
function(a, b) {
var t = typeof b;
return (t === "string" || t !== typeof a ? (d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString)
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);
Expand Down
4 changes: 3 additions & 1 deletion src/interpolate/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ function d3_interpolateString(a, b) {

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

// Otherwise, interpolate each of the numbers and rejoin the string.
Expand Down
42 changes: 23 additions & 19 deletions test/interpolate/interpolate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ suite.addBatch({
"interpolate": {
topic: load("interpolate/interpolate").document(),

"when a and b are numbers": {
"when b is a number": {
"interpolates numbers": function(d3) {
assert.strictEqual(d3.interpolate(2, 12)(.4), 6);
},
"coerces a to a number": function(d3) {
assert.strictEqual(d3.interpolate("", 1)(.5), .5);
assert.strictEqual(d3.interpolate("2", 12)(.4), 6);
assert.strictEqual(d3.interpolate([2], 12)(.4), 6);
}
},

"when a and b are color strings": {
"when b is a color string": {
"interpolates RGB values and returns a hexadecimal string": function(d3) {
assert.strictEqual(d3.interpolate("#ff0000", "#008000")(.4), "#993300");
},
Expand All @@ -26,10 +31,13 @@ suite.addBatch({
},
"interpolates decimal HSL colors in RGB": function(d3) {
assert.strictEqual(d3.interpolate("hsl(0,100%,50%)", "hsl(120,100%,25%)")(.4), "#993300");
},
"coerces a to a color": function(d3) {
assert.strictEqual(d3.interpolate({toString: function() { return "red"; }}, "green")(.4), "#993300");
}
},

"when a and b are color objects": {
"when b is a color object": {
"interpolates RGB values and returns a hexadecimal string": function(d3) {
assert.strictEqual(d3.interpolate(d3.rgb(255, 0, 0), d3.rgb(0, 128, 0))(.4), "#993300");
},
Expand All @@ -41,16 +49,19 @@ suite.addBatch({
},
"interpolates d3.hcl in RGB": function(d3) {
assert.strictEqual(d3.interpolate(d3.hcl("red"), d3.hcl("green"))(.4), "#993300");
},
"coerces a to a color": function(d3) {
assert.strictEqual(d3.interpolate({toString: function() { return "red"; }}, "green")(.4), "#993300");
}
},

"when a and b are strings": {
"when b is a string": {
"interpolates matching numbers in both strings": function(d3) {
assert.strictEqual(d3.interpolate(" 10/20 30", "50/10 100 ")(.4), "26/16 58 ");
},
"if a and b are coercible to numbers, interpolates numbers rather than strings": function(d3) {
assert.strictEqual(d3.interpolate("1.", "2.")(.5), 1.5);
assert.strictEqual(d3.interpolate("1e+3", "1e+4")(.5), 5500);
"if b is coercible to a number, still returns a string": function(d3) {
assert.strictEqual(d3.interpolate("1.", "2.")(.5), "1.5");
assert.strictEqual(d3.interpolate("1e+3", "1e+4")(.5), "5500");
},
"preserves non-numbers in string b": function(d3) {
assert.strictEqual(d3.interpolate(" 10/20 30", "50/10 foo ")(.4), "26/16 foo ");
Expand All @@ -60,10 +71,13 @@ suite.addBatch({
},
"preserves equal-value numbers in both strings": function(d3) {
assert.strictEqual(d3.interpolate(" 10/20 100 20", "50/10 100, 20 ")(.4), "26/16 100, 20 ");
},
"coerces a to a string": function(d3) {
assert.strictEqual(d3.interpolate({toString: function() { return "1."; }}, "2.")(.5), "1.5");
}
},

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

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

"when a and b are different types": {
"coerces both types to strings": function(d3) {
assert.strictEqual(d3.interpolate("2", 12)(.4), 6);
assert.strictEqual(d3.interpolate("2px", 12)(.4), 6);
assert.strictEqual(d3.interpolate([2], 12)(.4), 6);
assert.strictEqual(d3.interpolate({valueOf: function() { return 2; }}, 12)(.4), 6);
assert.strictEqual(d3.interpolate({toString: function() { return 2; }}, 12)(.4), 6);
}
},

"may or may not interpolate between enumerable and non-enumerable properties": function(d3) {
var a = Object.create({}, {foo: {value: 1, enumerable: true}}),
b = Object.create({}, {foo: {value: 2, enumerable: false}});
Expand Down
14 changes: 7 additions & 7 deletions test/interpolate/string-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ suite.addBatch({
assert.strictEqual(interpolate(" 10/20 100 20", "50/10 100, 20 ")(.4), "26/16 100, 20 ");
},
"interpolates decimal notation correctly": function(interpolate) {
assert.strictEqual(interpolate("1.", "2.")(.5), 1.5);
assert.strictEqual(interpolate("1.", "2.")(.5), "1.5");
},
"interpolates exponent notation correctly": function(interpolate) {
assert.strictEqual(interpolate("1e+3", "1e+4")(.5), 5500);
assert.strictEqual(interpolate("1e-3", "1e-4")(.5), 0.00055);
assert.strictEqual(interpolate("1.e-3", "1.e-4")(.5), 0.00055);
assert.strictEqual(interpolate("-1.e-3", "-1.e-4")(.5), -0.00055);
assert.strictEqual(interpolate("+1.e-3", "+1.e-4")(.5), 0.00055);
assert.strictEqual(interpolate(".1e-2", ".1e-3")(.5), 0.00055);
assert.strictEqual(interpolate("1e+3", "1e+4")(.5), "5500");
assert.strictEqual(interpolate("1e-3", "1e-4")(.5), "0.00055");
assert.strictEqual(interpolate("1.e-3", "1.e-4")(.5), "0.00055");
assert.strictEqual(interpolate("-1.e-3", "-1.e-4")(.5), "-0.00055");
assert.strictEqual(interpolate("+1.e-3", "+1.e-4")(.5), "0.00055");
assert.strictEqual(interpolate(".1e-2", ".1e-3")(.5), "0.00055");
}
}
});
Expand Down

0 comments on commit 3fefb4a

Please sign in to comment.