Skip to content

Commit 51ba14b

Browse files
committed
stack shorthand
1 parent b3ee575 commit 51ba14b

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

src/transforms/stack.js

+39-19
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,46 @@ import {ascendingDefined} from "../defined.js";
33
import {field, lazyChannel, maybeLazyChannel, maybeZ, mid, range, valueof, identity, maybeZero} from "../mark.js";
44
import {basic} from "./basic.js";
55

6-
export function stackX({y1, y = y1, x, ...options} = {}) {
7-
const [transform, Y, x1, x2] = stack(y, x, "x", options);
8-
return {y1, y: Y, x1, x2, x: mid(x1, x2), ...transform};
6+
export function stackX(stackOptions = {}, options = {}) {
7+
if (arguments.length === 1) options = mergeOptions(stackOptions);
8+
const {y1, y = y1, x, ...rest} = options; // note: consumes x!
9+
const [transform, Y, x1, x2] = stack(y, x, "x", stackOptions, rest);
10+
return {...transform, y1, y: Y, x1, x2, x: mid(x1, x2)};
911
}
1012

11-
export function stackX1({y1, y = y1, x, ...options} = {}) {
12-
const [transform, Y, X] = stack(y, x, "x", options);
13-
return {y1, y: Y, x: X, ...transform};
13+
export function stackX1(stackOptions = {}, options = {}) {
14+
if (arguments.length === 1) options = mergeOptions(stackOptions);
15+
const {y1, y = y1, x} = options;
16+
const [transform, Y, X] = stack(y, x, "x", stackOptions, options);
17+
return {...transform, y1, y: Y, x: X};
1418
}
1519

16-
export function stackX2({y1, y = y1, x, ...options} = {}) {
17-
const [transform, Y,, X] = stack(y, x, "x", options);
18-
return {y1, y: Y, x: X, ...transform};
20+
export function stackX2(stackOptions = {}, options = {}) {
21+
if (arguments.length === 1) options = mergeOptions(stackOptions);
22+
const {y1, y = y1, x} = options;
23+
const [transform, Y,, X] = stack(y, x, "x", stackOptions, options);
24+
return {...transform, y1, y: Y, x: X};
1925
}
2026

21-
export function stackY({x1, x = x1, y, ...options} = {}) {
22-
const [transform, X, y1, y2] = stack(x, y, "y", options);
23-
return {x1, x: X, y1, y2, y: mid(y1, y2), ...transform};
27+
export function stackY(stackOptions = {}, options = {}) {
28+
if (arguments.length === 1) options = mergeOptions(stackOptions);
29+
const {x1, x = x1, y, ...rest} = options; // note: consumes y!
30+
const [transform, X, y1, y2] = stack(x, y, "y", stackOptions, rest);
31+
return {...transform, x1, x: X, y1, y2, y: mid(y1, y2)};
2432
}
2533

26-
export function stackY1({x1, x = x1, y, ...options} = {}) {
27-
const [transform, X, Y] = stack(x, y, "y", options);
28-
return {x1, x: X, y: Y, ...transform};
34+
export function stackY1(stackOptions = {}, options = {}) {
35+
if (arguments.length === 1) options = mergeOptions(stackOptions);
36+
const {x1, x = x1, y} = options;
37+
const [transform, X, Y] = stack(x, y, "y", stackOptions, options);
38+
return {...transform, x1, x: X, y: Y};
2939
}
3040

31-
export function stackY2({x1, x = x1, y, ...options} = {}) {
32-
const [transform, X,, Y] = stack(x, y, "y", options);
33-
return {x1, x: X, y: Y, ...transform};
41+
export function stackY2(stackOptions = {}, options = {}) {
42+
if (arguments.length === 1) options = mergeOptions(stackOptions);
43+
const {x1, x = x1, y} = options;
44+
const [transform, X,, Y] = stack(x, y, "y", stackOptions, options);
45+
return {...transform, x1, x: X, y: Y};
3446
}
3547

3648
export function maybeStackX({x, x1, x2, ...options} = {}) {
@@ -51,7 +63,15 @@ export function maybeStackY({y, y1, y2, ...options} = {}) {
5163
return {...options, y1, y2};
5264
}
5365

54-
function stack(x, y = () => 1, ky, {offset, order, reverse, ...options} = {}) {
66+
// The reverse option is ambiguous: it is both a stack option and a basic
67+
// transform. If only one options object is specified, we interpret it as a
68+
// stack option, and therefore must remove it from the propagated options.
69+
function mergeOptions(options) {
70+
const {reverse} = options;
71+
return reverse ? {...options, reverse: false} : options;
72+
}
73+
74+
function stack(x, y = () => 1, ky, {offset, order, reverse}, options) {
5575
const z = maybeZ(options);
5676
const [X, setX] = maybeLazyChannel(x);
5777
const [Y1, setY1] = lazyChannel(y);

0 commit comments

Comments
 (0)