Skip to content

Commit

Permalink
Fix #23 - always promote accessors.
Browse files Browse the repository at this point in the history
Fix #24 - a more explicit area.[xy]0 = area.[xy]1.
  • Loading branch information
mbostock committed Nov 18, 2015
1 parent 048ba33 commit 2c28a48
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 48 deletions.
47 changes: 16 additions & 31 deletions src/area.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,30 @@ import {x as pointX, y as pointY} from "./point";

export default function() {
var x0 = pointX,
x1 = pointX,
y0 = 0,
x1 = null,
y0 = constantZero,
y1 = pointY,
x0i,
y0i,
defined = true,
getX0 = pointX,
getX1 = sameX,
getY0 = constantZero,
getY1 = pointY,
getDefined = constantTrue,
defined = constantTrue,
interpolate = linear,
context = null,
output = null;

function sameX() {
return x0i;
}

function sameY() {
return y0i;
}

function area(data) {
var i,
j,
k,
n = data.length,
d,
isDefined = false,
defined0 = false,
buffer,
x0z = new Array(n),
y0z = new Array(n);

if (!context) output = interpolate(buffer = path());

for (i = 0; i <= n; ++i) {
if (!(i < n && getDefined(d = data[i], i)) === isDefined) {
if (isDefined = !isDefined) {
if (!(i < n && defined(d = data[i], i)) === defined0) {
if (defined0 = !defined0) {
j = i;
output.areaStart();
output.lineStart();
Expand All @@ -67,41 +52,41 @@ export default function() {
output.areaEnd();
}
}
if (isDefined) {
x0z[i] = x0i = +getX0(d, i), y0z[i] = y0i = +getY0(d, i);
output.point(+getX1(d, i), +getY1(d, i));
if (defined0) {
x0z[i] = +x0(d, i), y0z[i] = +y0(d, i);
output.point(x1 ? +x1(d, i) : x0z[i], y1 ? +y1(d, i) : y0z[i]);
}
}

if (!context) return output = null, buffer + "" || null;
}

area.x = function(_) {
return arguments.length ? area.x0(_).x1(_) : x1;
return arguments.length ? area.x0(_).x1(null) : x0;
};

area.x0 = function(_) {
return arguments.length ? (x0 = _, getX0 = typeof _ === "function" ? x0 : constant(x0), area.x1(x1)) : x0;
return arguments.length ? (x0 = typeof _ === "function" ? _ : constant(_), area) : x0;
};

area.x1 = function(_) {
return arguments.length ? (x1 = _, getX1 = _ === x0 ? sameX : typeof _ === "function" ? x1 : constant(x1), area) : x1;
return arguments.length ? (x1 = _ == null ? null : typeof _ === "function" ? _ : constant(_), area) : x1;
};

area.y = function(_) {
return arguments.length ? area.y0(_).y1(_) : y1;
return arguments.length ? area.y0(_).y1(null) : y0;
};

area.y0 = function(_) {
return arguments.length ? (y0 = _, getY0 = typeof _ === "function" ? y0 : constant(y0), area.y1(y1)) : y0;
return arguments.length ? (y0 = typeof _ === "function" ? _ : constant(_), area) : y0;
};

area.y1 = function(_) {
return arguments.length ? (y1 = _, getY1 = _ === y0 ? sameY : typeof _ === "function" ? y1 : constant(y1), area) : y1;
return arguments.length ? (y1 = _ == null ? null : typeof _ === "function" ? _ : constant(_), area) : y1;
};

area.defined = function(_) {
return arguments.length ? (defined = _, getDefined = typeof _ === "function" ? defined : constant(defined), area) : defined;
return arguments.length ? (defined = typeof _ === "function" ? _ : constant(_), area) : defined;
};

area.interpolate = function(_, a) {
Expand Down
19 changes: 8 additions & 11 deletions src/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import {x as pointX, y as pointY} from "./point";
export default function() {
var x = pointX,
y = pointY,
defined = true,
getX = pointX,
getY = pointY,
getDefined = constantTrue,
defined = constantTrue,
interpolate = linear,
context = null,
output = null;
Expand All @@ -33,32 +30,32 @@ export default function() {
var i,
n = data.length,
d,
isDefined = false,
defined0 = false,
buffer;

if (!context) output = interpolate(buffer = path());

for (i = 0; i <= n; ++i) {
if (!(i < n && getDefined(d = data[i], i)) === isDefined) {
if (isDefined = !isDefined) output.lineStart();
if (!(i < n && defined(d = data[i], i)) === defined0) {
if (defined0 = !defined0) output.lineStart();
else output.lineEnd();
}
if (isDefined) output.point(+getX(d, i), +getY(d, i));
if (defined0) output.point(+x(d, i), +y(d, i));
}

if (!context) return output = null, buffer + "" || null;
}

line.x = function(_) {
return arguments.length ? (x = _, getX = typeof _ === "function" ? x : constant(x), line) : x;
return arguments.length ? (x = typeof _ === "function" ? _ : constant(_), line) : x;
};

line.y = function(_) {
return arguments.length ? (y = _, getY = typeof _ === "function" ? y : constant(y), line) : y;
return arguments.length ? (y = typeof _ === "function" ? _ : constant(_), line) : y;
};

line.defined = function(_) {
return arguments.length ? (defined = _, getDefined = typeof _ === "function" ? defined : constant(defined), line) : defined;
return arguments.length ? (defined = typeof _ === "function" ? _ : constant(_), line) : defined;
};

line.interpolate = function(_, a) {
Expand Down
45 changes: 42 additions & 3 deletions test/area-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,54 @@ var tape = require("tape"),
tape("area() returns a default area shape", function(test) {
var a = shape.area();
test.equal(a.x0()([42, 34]), 42);
test.equal(a.x1()([42, 34]), 42);
test.equal(a.y0(), 0);
test.equal(a.x1(), null);
test.equal(a.y0()([42, 34]), 0);
test.equal(a.y1()([42, 34]), 34);
test.equal(a.defined(), true);
test.equal(a.defined()([42, 34]), true);
test.equal(a.context(), null);
test.equal(a([[0, 1], [2, 3], [4, 5]]), "M0,1L2,3L4,5L4,0L2,0L0,0Z");
test.end();
});

tape("area.x(x)(data) observes the specified function", function(test) {
var x = function(d) { return d.x; },
a = shape.area().x(x);
test.equal(a.x(), x);
test.equal(a.x0(), x);
test.equal(a.x1(), null);
test.equal(a([{x: 0, 1: 1}, {x: 2, 1: 3}, {x: 4, 1: 5}]), "M0,1L2,3L4,5L4,0L2,0L0,0Z");
test.end();
});

tape("area.x(x)(data) observes the specified constant", function(test) {
var x = 0,
a = shape.area().x(x);
test.equal(a.x()(), 0);
test.equal(a.x0()(), 0);
test.equal(a.x1(), null);
test.equal(a([{1: 1}, {1: 3}, {1: 5}]), "M0,1L0,3L0,5L0,0L0,0L0,0Z");
test.end();
});

tape("area.y(y)(data) observes the specified function", function(test) {
var y = function(d) { return d.y; },
a = shape.area().y(y);
test.equal(a.y(), y);
test.equal(a.y0(), y);
test.equal(a.y1(), null);
test.equal(a([{0: 0, y: 1}, {0: 2, y: 3}, {0: 4, y: 5}]), "M0,1L2,3L4,5L4,5L2,3L0,1Z");
test.end();
});

tape("area.y(y)(data) observes the specified constant", function(test) {
var a = shape.area().y(0);
test.equal(a.y()(), 0);
test.equal(a.y0()(), 0);
test.equal(a.y1(), null);
test.equal(a([{0: 0}, {0: 2}, {0: 4}]), "M0,0L2,0L4,0L4,0L2,0L0,0Z");
test.end();
});

tape("area.interpolate(\"linear\")(data) generates the expected path", function(test) {
var a = shape.area().interpolate("linear");
test.equal(a([]), null);
Expand Down
6 changes: 3 additions & 3 deletions test/line-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ tape("line() returns a default line shape", function(test) {
var l = shape.line();
test.equal(l.x()([42, 34]), 42);
test.equal(l.y()([42, 34]), 34);
test.equal(l.defined(), true);
test.equal(l.defined()([42, 34]), true);
test.equal(l.context(), null);
test.equal(l([[0, 1], [2, 3], [4, 5]]), "M0,1L2,3L4,5");
test.end();
});

tape("line.x(x)(data) observes the specified x-accessor", function(test) {
tape("line.x(x)(data) observes the specified function", function(test) {
var l = shape.line().x(function(d) { return d.x; });
test.equal(l([{x: 0, 1: 1}, {x: 2, 1: 3}, {x: 4, 1: 5}]), "M0,1L2,3L4,5");
test.end();
Expand All @@ -23,7 +23,7 @@ tape("line.x(x)(data) observes the specified constant", function(test) {
test.end();
});

tape("line.y(y)(data) observes the specified y-accessor", function(test) {
tape("line.y(y)(data) observes the specified function", function(test) {
var l = shape.line().y(function(d) { return d.y; });
test.equal(l([{0: 0, y: 1}, {0: 2, y: 3}, {0: 4, y: 5}]), "M0,1L2,3L4,5");
test.end();
Expand Down

0 comments on commit 2c28a48

Please sign in to comment.