Skip to content

Commit

Permalink
expose indexOf, and use Uint32Array (#1456)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock authored Apr 17, 2023
1 parent 05d3e83 commit ec6440a
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export {Text, text, textX, textY} from "./marks/text.js";
export {TickX, TickY, tickX, tickY} from "./marks/tick.js";
export {tree, cluster} from "./marks/tree.js";
export {Vector, vector, vectorX, vectorY, spike} from "./marks/vector.js";
export {valueof, column, identity} from "./options.js";
export {valueof, column, identity, indexOf} from "./options.js";
export {filter, reverse, sort, shuffle, basic as transform, initializer} from "./transforms/basic.js";
export {bin, binX, binY} from "./transforms/bin.js";
export {centroid, geoCentroid} from "./transforms/centroid.js";
Expand Down
6 changes: 6 additions & 0 deletions src/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ export function column(source?: any): [ChannelTransform, <T>(value: T) => T];
* ```
*/
export const identity: ChannelTransform;

/**
* A channel transform that returns the zero-based index [0, 1, 2, …] of the
* data; often used as a default for mark shorthand.
*/
export const indexOf: ChannelTransform;
2 changes: 1 addition & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function floater(f) {
}

export const field = (name) => (d) => d[name];
export const indexOf = (d, i) => i;
export const indexOf = {transform: range};
export const identity = {transform: (d) => d};
export const zero = () => 0;
export const one = () => 1;
Expand Down
10 changes: 2 additions & 8 deletions test/marks/cell-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ it("cellX() defaults x to identity and y to null", () => {
assert.deepStrictEqual(Object.keys(cell.channels), ["fill", "x"]);
assert.deepStrictEqual(
Object.values(cell.channels).map((c) => Plot.valueof([1, 2, 3], c.value)),
[
[1, 2, 3],
[0, 1, 2]
]
[[1, 2, 3], Uint32Array.of(0, 1, 2)]
);
assert.deepStrictEqual(
Object.values(cell.channels).map((c) => c.scale),
Expand All @@ -114,10 +111,7 @@ it("cellY() defaults y to identity and x to null", () => {
assert.deepStrictEqual(Object.keys(cell.channels), ["fill", "y"]);
assert.deepStrictEqual(
Object.values(cell.channels).map((c) => Plot.valueof([1, 2, 3], c.value)),
[
[1, 2, 3],
[0, 1, 2]
]
[[1, 2, 3], Uint32Array.of(0, 1, 2)]
);
assert.deepStrictEqual(
Object.values(cell.channels).map((c) => c.scale),
Expand Down
6 changes: 1 addition & 5 deletions test/marks/text-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ it("text() has the expected defaults", () => {
c.value
)
),
[
[1, 3],
[2, 4],
[0, 1]
]
[[1, 3], [2, 4], Uint32Array.of(0, 1)]
);
assert.deepStrictEqual(
Object.values(text.channels).map((c) => c.scale),
Expand Down

0 comments on commit ec6440a

Please sign in to comment.