Skip to content

Commit

Permalink
Merge pull request #76 from d3/exact-ends
Browse files Browse the repository at this point in the history
Exact ends
  • Loading branch information
Fil authored Nov 20, 2019
2 parents 00a0674 + 4b9001b commit c3473cf
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/date.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function(a, b) {
var d = new Date;
return a = +a, b -= a, function(t) {
return d.setTime(a + b * t), d;
return a = +a, b = +b, function(t) {
return d.setTime(a * (1 - t) + b * t), d;
};
}
4 changes: 2 additions & 2 deletions src/number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function(a, b) {
return a = +a, b -= a, function(t) {
return a + b * t;
return a = +a, b = +b, function(t) {
return a * (1 - t) + b * t;
};
}
4 changes: 2 additions & 2 deletions src/round.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default function(a, b) {
return a = +a, b -= a, function(t) {
return Math.round(a + b * t);
return a = +a, b = +b, function(t) {
return Math.round(a * (1 - t) + b * t);
};
}
7 changes: 7 additions & 0 deletions test/array-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ tape("interpolateArray(a, b) interpolates array-like objects", function(test) {
test.deepEqual(interpolate.interpolateArray(args, [4, 24])(0.5), [3, 18]);
test.end();
});

tape("interpolateArray(a, b) gives exact ends for t=0 and t=1", function(test) {
var a = [2e+42], b = [335];
test.deepEqual(interpolate.interpolateArray(a, b)(1), b);
test.deepEqual(interpolate.interpolateArray(a, b)(0), a);
test.end();
});
7 changes: 7 additions & 0 deletions test/date-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ tape("interpolateDate(a, b) reuses the output datea", function(test) {
test.strictEqual(i(0.2), i(0.4));
test.end();
});

tape("interpolateDate(a, b) gives exact ends for t=0 and t=1", function(test) {
var a = new Date(1e8 * 24 * 60 * 60 * 1000), b = new Date(-1e8 * 24 * 60 * 60 * 1000 +1);
test.equal(+interpolate.interpolateDate(a, b)(1), +b);
test.equal(+interpolate.interpolateDate(a, b)(0), +a);
test.end();
});
8 changes: 8 additions & 0 deletions test/number-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ tape("interpolateNumber(a, b) interpolates between two numbers a and b", functio
test.inDelta(i(1.0), 42.0);
test.end();
});


tape("interpolateNumber(a, b) gives exact ends for t=0 and t=1", function(test) {
var a = 2e+42, b = 335;
test.equal(interpolate.interpolateNumber(a, b)(1), b);
test.equal(interpolate.interpolateNumber(a, b)(0), a);
test.end();
});
7 changes: 7 additions & 0 deletions test/round-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ tape("interpolateRound(a, b) does not pre-round a and b", function(test) {
test.equal(i(0.6), 3);
test.end();
});

tape("interpolateRound(a, b) gives exact ends for t=0 and t=1", function(test) {
var a = 2e+42, b = 335;
test.equal(interpolate.interpolateRound(a, b)(1), b);
test.equal(interpolate.interpolateRound(a, b)(0), a);
test.end();
});

0 comments on commit c3473cf

Please sign in to comment.