Permalink
Browse files

Fix #392 - interpolate string to number.

  • Loading branch information...
1 parent 12711f2 commit 2be4efd609575bfbe18bdf8362f66ea5ee5645ef @mbostock mbostock committed Nov 30, 2011
Showing with 10 additions and 8 deletions.
  1. +3 −3 d3.js
  2. +2 −2 d3.min.js
  3. +3 −3 src/core/interpolate.js
  4. +2 −0 test/core/interpolate-test.js
View
@@ -977,9 +977,9 @@ function d3_interpolateByName(n) {
d3.interpolators = [
d3.interpolateObject,
function(a, b) { return (b instanceof Array) && d3.interpolateArray(a, b); },
- function(a, b) { return (typeof b === "string") && d3.interpolateString(a + "", b); },
- function(a, b) { return (typeof b === "string" ? b in d3_rgb_names || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a + "", b); },
- function(a, b) { return (typeof b === "number") && d3.interpolateNumber(+a, b); }
+ function(a, b) { return (typeof a === "string" || typeof b === "string") && d3.interpolateString(a + "", b + ""); },
+ function(a, b) { return (typeof b === "string" ? b in d3_rgb_names || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); },
+ function(a, b) { return !isNaN(a = +a) && !isNaN(b = +b) && d3.interpolateNumber(a, b); }
];
function d3_uninterpolateNumber(a, b) {
b = b - (a = +a) ? 1 / (b - a) : 0;
View
Oops, something went wrong.
@@ -177,7 +177,7 @@ function d3_interpolateByName(n) {
d3.interpolators = [
d3.interpolateObject,
function(a, b) { return (b instanceof Array) && d3.interpolateArray(a, b); },
- function(a, b) { return (typeof b === "string") && d3.interpolateString(a + "", b); },
- function(a, b) { return (typeof b === "string" ? b in d3_rgb_names || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a + "", b); },
- function(a, b) { return (typeof b === "number") && d3.interpolateNumber(+a, b); }
+ function(a, b) { return (typeof a === "string" || typeof b === "string") && d3.interpolateString(a + "", b + ""); },
+ function(a, b) { return (typeof b === "string" ? b in d3_rgb_names || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); },
+ function(a, b) { return !isNaN(a = +a) && !isNaN(b = +b) && d3.interpolateNumber(a, b); }
];
@@ -13,12 +13,14 @@ suite.addBatch({
},
"interpolates numbers": function(interpolate) {
assert.equal(interpolate(2, 12)(.4), 6);
+ assert.equal(interpolate("2px", 12)(.4), 6);
},
"interpolates colors": function(interpolate) {
assert.equal(interpolate("#abcdef", "#fedcba")(.4), "#ccd3da");
},
"interpolates strings": function(interpolate) {
assert.equal(interpolate("width:10px;", "width:50px;")(.2), "width:18px;");
+ assert.equal(interpolate(2, "12px")(.4), "6px");
},
"interpolates arrays": function(interpolate) {
assert.deepEqual(interpolate([2, 4], [12, 24])(.4), [6, 12]);

0 comments on commit 2be4efd

Please sign in to comment.