Commit 947c6090 authored by Mike Bostock's avatar Mike Bostock
Browse files

Prefer interpolateNumber when coercible to number.

Objects that are coercible to numbers, such as Dates, are often more usefully
interpolated as numbers rather than as objects.
parent 90b6c0b9
......@@ -5692,7 +5692,7 @@
}
d3.interpolators = [ function(a, b) {
var t = typeof 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);
return (t === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_Color ? d3_interpolateRgb : Array.isArray(b) ? d3_interpolateArray : t === "object" && isNaN(b) ? d3_interpolateObject : d3_interpolateNumber)(a, b);
} ];
d3.interpolateArray = d3_interpolateArray;
function d3_interpolateArray(a, b) {
......
This diff is collapsed.
......@@ -19,7 +19,8 @@ d3.interpolators = [
var t = typeof 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)
: Array.isArray(b) ? d3_interpolateArray
: t === "object" && isNaN(b) ? d3_interpolateObject
: d3_interpolateNumber)(a, b);
}
];
......@@ -95,11 +95,11 @@ suite.addBatch({
"interpolates each property in b": function(d3) {
assert.deepEqual(d3.interpolate({foo: 2, bar: 4}, {foo: 12, bar: 24})(.4), {foo: 6, bar: 12});
},
"interpolates arrays, even when both a and b are coercible to numbers": function(d3) {
"interpolates numbers if b is coercible to a number (not NaN)": function(d3) {
var two = new Number(2), twelve = new Number(12);
two.foo = "2px";
twelve.foo = "12px";
assert.deepEqual(d3.interpolate(two, twelve)(.4), {foo: "6px"});
assert.strictEqual(d3.interpolate(two, twelve)(.4), 6);
var start = new Date(2012, 0, 1), end = new Date(2013, 0, 1);
assert.strictEqual(d3.interpolate(start, end)(.5), +new Date(2012, 6, 2, 1));
},
"reuses the returned object during interpolation": function(d3) {
var i = d3.interpolate({foo: 2, bar: 4}, {foo: 12, bar: 24});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment