From 7f8236fb32ebae4a307a3e6495bb68d4ed4efae2 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Fri, 19 Mar 2021 18:28:00 -0700 Subject: [PATCH 1/4] identity transform --- src/mark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mark.js b/src/mark.js index 5b2a611d22..73c581d6f2 100644 --- a/src/mark.js +++ b/src/mark.js @@ -68,7 +68,7 @@ export function valueof(data, value, type) { export const field = label => Object.assign(d => d[label], {label}); export const indexOf = (d, i) => i; -export const identity = d => d; +export const identity = {transform: d => d}; export const zero = () => 0; export const string = x => x == null ? undefined : x + ""; export const number = x => x == null ? undefined : +x; From 8126108d5ec6f344837964fc7108c48e2af27c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Sat, 20 Mar 2021 15:08:53 +0100 Subject: [PATCH 2/4] fix tests --- test/marks/bar-test.js | 12 ++++++------ test/marks/cell-test.js | 18 +++++++++--------- test/marks/rule-test.js | 12 ++++++------ test/marks/tick-test.js | 12 ++++++------ 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/test/marks/bar-test.js b/test/marks/bar-test.js index 5d683485d2..096816d7d7 100644 --- a/test/marks/bar-test.js +++ b/test/marks/bar-test.js @@ -2,11 +2,11 @@ import * as Plot from "@observablehq/plot"; import tape from "tape-await"; tape("barX() has the expected defaults", test => { - const bar = Plot.barX(); - test.strictEqual(bar.data, undefined); + const bar = Plot.barX([1, 2, 3]); + test.deepEqual(bar.data, [1, 2, 3]); test.strictEqual(bar.transform, undefined); test.deepEqual(bar.channels.map(c => c.name), ["x1", "x2"]); - test.deepEqual(bar.channels.map(c => c.value(1, 0)), [0, 1]); + test.deepEqual(bar.initialize().channels.map(d => d[1].value), [ [ 0, 0, 0 ], [ 1, 2, 3 ] ]); test.deepEqual(bar.channels.map(c => c.scale), ["x", "x"]); test.strictEqual(bar.fill, undefined); test.strictEqual(bar.fillOpacity, undefined); @@ -96,11 +96,11 @@ tape("barX(data, {x, y}) defaults x1 to zero and x2 to x", test => { }); tape("barY() has the expected defaults", test => { - const bar = Plot.barY(); - test.strictEqual(bar.data, undefined); + const bar = Plot.barY([1, 2, 3]); + test.deepEqual(bar.data, [1, 2, 3]); test.strictEqual(bar.transform, undefined); test.deepEqual(bar.channels.map(c => c.name), ["y1", "y2"]); - test.deepEqual(bar.channels.map(c => c.value(1, 0)), [0, 1]); + test.deepEqual(bar.initialize().channels.map(d => d[1].value), [ [ 0, 0, 0 ], [ 1, 2, 3 ] ]); test.deepEqual(bar.channels.map(c => c.scale), ["y", "y"]); test.strictEqual(bar.fill, undefined); test.strictEqual(bar.fillOpacity, undefined); diff --git a/test/marks/cell-test.js b/test/marks/cell-test.js index 26526100fe..9ec82b77cd 100644 --- a/test/marks/cell-test.js +++ b/test/marks/cell-test.js @@ -2,11 +2,11 @@ import * as Plot from "@observablehq/plot"; import tape from "tape-await"; tape("cell() has the expected defaults", test => { - const cell = Plot.cell(); - test.strictEqual(cell.data, undefined); + const cell = Plot.cell([[0, 1], [0, 2]]); + test.deepEqual(cell.data, [[0, 1], [0, 2]]); test.strictEqual(cell.transform, undefined); test.deepEqual(cell.channels.map(c => c.name), ["x", "y"]); - test.deepEqual(cell.channels.map(c => c.value([1, 2])), [1, 2]); + test.deepEqual(cell.initialize().channels.map(d => d[1].value), [[0, 0], [1, 2]]); test.deepEqual(cell.channels.map(c => c.scale), ["x", "y"]); test.strictEqual(cell.channels.find(c => c.name === "x").type, "band"); test.strictEqual(cell.channels.find(c => c.name === "y").type, "band"); @@ -77,21 +77,21 @@ tape("cell(data, {stroke}) allows stroke to be a variable color", test => { }); tape("cellX() defaults x to identity and y to null", test => { - const cell = Plot.cellX(); - test.strictEqual(cell.data, undefined); + const cell = Plot.cellX([12]); + test.deepEqual(cell.data, [12]); test.strictEqual(cell.transform, undefined); test.deepEqual(cell.channels.map(c => c.name), ["x"]); - test.deepEqual(cell.channels.map(c => c.value("foo")), ["foo"]); + test.deepEqual(cell.initialize().channels.map(d => d[1].value), [[ 12 ]]); test.deepEqual(cell.channels.map(c => c.scale), ["x"]); test.strictEqual(cell.channels.find(c => c.name === "x").type, "band"); }); tape("cellY() defaults y to identity and x to null", test => { - const cell = Plot.cellY(); - test.strictEqual(cell.data, undefined); + const cell = Plot.cellY([12]); + test.deepEqual(cell.data, [12]); test.strictEqual(cell.transform, undefined); test.deepEqual(cell.channels.map(c => c.name), ["y"]); - test.deepEqual(cell.channels.map(c => c.value("foo")), ["foo"]); + test.deepEqual(cell.initialize().channels.map(d => d[1].value), [[ 12 ]]); test.deepEqual(cell.channels.map(c => c.scale), ["y"]); test.strictEqual(cell.channels.find(c => c.name === "y").type, "band"); }); diff --git a/test/marks/rule-test.js b/test/marks/rule-test.js index 83d17d059a..6cc9fc3cab 100644 --- a/test/marks/rule-test.js +++ b/test/marks/rule-test.js @@ -2,11 +2,11 @@ import * as Plot from "@observablehq/plot"; import tape from "tape-await"; tape("ruleX() has the expected defaults", test => { - const rule = Plot.ruleX(); - test.strictEqual(rule.data, undefined); + const rule = Plot.ruleX([42]); + test.deepEqual(rule.data, [42]); test.strictEqual(rule.transform, undefined); test.deepEqual(rule.channels.map(c => c.name), ["x"]); - test.deepEqual(rule.channels.map(c => c.value("foo")), ["foo"]); + test.deepEqual(rule.initialize().channels.map(d => d[1].value), [[ 42 ]]); test.deepEqual(rule.channels.map(c => c.scale), ["x"]); test.strictEqual(rule.fill, undefined); test.strictEqual(rule.fillOpacity, undefined); @@ -96,11 +96,11 @@ tape("ruleX(data, {x, y1, y2}) specifies x, y1, y2", test => { }); tape("ruleY() has the expected defaults", test => { - const rule = Plot.ruleY(); - test.strictEqual(rule.data, undefined); + const rule = Plot.ruleY([42]); + test.deepEqual(rule.data, [42]); test.strictEqual(rule.transform, undefined); test.deepEqual(rule.channels.map(c => c.name), ["y"]); - test.deepEqual(rule.channels.map(c => c.value("foo")), ["foo"]); + test.deepEqual(rule.initialize().channels.map(d => d[1].value), [[ 42 ]]); test.deepEqual(rule.channels.map(c => c.scale), ["y"]); test.strictEqual(rule.fill, undefined); test.strictEqual(rule.fillOpacity, undefined); diff --git a/test/marks/tick-test.js b/test/marks/tick-test.js index e6242ae513..453d8e758a 100644 --- a/test/marks/tick-test.js +++ b/test/marks/tick-test.js @@ -2,11 +2,11 @@ import * as Plot from "@observablehq/plot"; import tape from "tape-await"; tape("tickX() has the expected defaults", test => { - const tick = Plot.tickX(); - test.strictEqual(tick.data, undefined); + const tick = Plot.tickX([42]); + test.deepEqual(tick.data, [42]); test.strictEqual(tick.transform, undefined); test.deepEqual(tick.channels.map(c => c.name), ["x"]); - test.deepEqual(tick.channels.map(c => c.value("foo", 0)), ["foo"]); + test.deepEqual(tick.initialize().channels.map(d => d[1].value), [[ 42 ]]); test.deepEqual(tick.channels.map(c => c.scale), ["x"]); test.strictEqual(tick.fill, undefined); test.strictEqual(tick.fillOpacity, undefined); @@ -61,11 +61,11 @@ tape("tickX(data, {stroke}) allows stroke to be a variable color", test => { }); tape("tickY() has the expected defaults", test => { - const tick = Plot.tickY(); - test.strictEqual(tick.data, undefined); + const tick = Plot.tickY([42]); + test.deepEqual(tick.data, [42]); test.strictEqual(tick.transform, undefined); test.deepEqual(tick.channels.map(c => c.name), ["y"]); - test.deepEqual(tick.channels.map(c => c.value("foo", 0)), ["foo"]); + test.deepEqual(tick.initialize().channels.map(d => d[1].value), [[ 42 ]]); test.deepEqual(tick.channels.map(c => c.scale), ["y"]); test.strictEqual(tick.fill, undefined); test.strictEqual(tick.fillOpacity, undefined); From 56536a39b26a825e8908011e8cebd7fd2ac66d9f Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sat, 20 Mar 2021 11:39:03 -0700 Subject: [PATCH 3/4] use Plot.valueof to test --- test/marks/bar-test.js | 12 ++++++------ test/marks/cell-test.js | 18 +++++++++--------- test/marks/rule-test.js | 12 ++++++------ test/marks/tick-test.js | 12 ++++++------ 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/test/marks/bar-test.js b/test/marks/bar-test.js index 096816d7d7..9574e796e6 100644 --- a/test/marks/bar-test.js +++ b/test/marks/bar-test.js @@ -2,11 +2,11 @@ import * as Plot from "@observablehq/plot"; import tape from "tape-await"; tape("barX() has the expected defaults", test => { - const bar = Plot.barX([1, 2, 3]); - test.deepEqual(bar.data, [1, 2, 3]); + const bar = Plot.barX(); + test.strictEqual(bar.data, undefined); test.strictEqual(bar.transform, undefined); test.deepEqual(bar.channels.map(c => c.name), ["x1", "x2"]); - test.deepEqual(bar.initialize().channels.map(d => d[1].value), [ [ 0, 0, 0 ], [ 1, 2, 3 ] ]); + test.deepEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]); test.deepEqual(bar.channels.map(c => c.scale), ["x", "x"]); test.strictEqual(bar.fill, undefined); test.strictEqual(bar.fillOpacity, undefined); @@ -96,11 +96,11 @@ tape("barX(data, {x, y}) defaults x1 to zero and x2 to x", test => { }); tape("barY() has the expected defaults", test => { - const bar = Plot.barY([1, 2, 3]); - test.deepEqual(bar.data, [1, 2, 3]); + const bar = Plot.barY(); + test.strictEqual(bar.data, undefined); test.strictEqual(bar.transform, undefined); test.deepEqual(bar.channels.map(c => c.name), ["y1", "y2"]); - test.deepEqual(bar.initialize().channels.map(d => d[1].value), [ [ 0, 0, 0 ], [ 1, 2, 3 ] ]); + test.deepEqual(bar.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[0, 0, 0], [1, 2, 3]]); test.deepEqual(bar.channels.map(c => c.scale), ["y", "y"]); test.strictEqual(bar.fill, undefined); test.strictEqual(bar.fillOpacity, undefined); diff --git a/test/marks/cell-test.js b/test/marks/cell-test.js index 9ec82b77cd..35b44d873d 100644 --- a/test/marks/cell-test.js +++ b/test/marks/cell-test.js @@ -2,11 +2,11 @@ import * as Plot from "@observablehq/plot"; import tape from "tape-await"; tape("cell() has the expected defaults", test => { - const cell = Plot.cell([[0, 1], [0, 2]]); - test.deepEqual(cell.data, [[0, 1], [0, 2]]); + const cell = Plot.cell(); + test.strictEqual(cell.data, undefined); test.strictEqual(cell.transform, undefined); test.deepEqual(cell.channels.map(c => c.name), ["x", "y"]); - test.deepEqual(cell.initialize().channels.map(d => d[1].value), [[0, 0], [1, 2]]); + test.deepEqual(cell.channels.map(c => c.value([1, 2])), [1, 2]); test.deepEqual(cell.channels.map(c => c.scale), ["x", "y"]); test.strictEqual(cell.channels.find(c => c.name === "x").type, "band"); test.strictEqual(cell.channels.find(c => c.name === "y").type, "band"); @@ -77,21 +77,21 @@ tape("cell(data, {stroke}) allows stroke to be a variable color", test => { }); tape("cellX() defaults x to identity and y to null", test => { - const cell = Plot.cellX([12]); - test.deepEqual(cell.data, [12]); + const cell = Plot.cellX(); + test.strictEqual(cell.data, undefined); test.strictEqual(cell.transform, undefined); test.deepEqual(cell.channels.map(c => c.name), ["x"]); - test.deepEqual(cell.initialize().channels.map(d => d[1].value), [[ 12 ]]); + test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]); test.deepEqual(cell.channels.map(c => c.scale), ["x"]); test.strictEqual(cell.channels.find(c => c.name === "x").type, "band"); }); tape("cellY() defaults y to identity and x to null", test => { - const cell = Plot.cellY([12]); - test.deepEqual(cell.data, [12]); + const cell = Plot.cellY(); + test.strictEqual(cell.data, undefined); test.strictEqual(cell.transform, undefined); test.deepEqual(cell.channels.map(c => c.name), ["y"]); - test.deepEqual(cell.initialize().channels.map(d => d[1].value), [[ 12 ]]); + test.deepEqual(cell.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]); test.deepEqual(cell.channels.map(c => c.scale), ["y"]); test.strictEqual(cell.channels.find(c => c.name === "y").type, "band"); }); diff --git a/test/marks/rule-test.js b/test/marks/rule-test.js index 6cc9fc3cab..64e5ec8af4 100644 --- a/test/marks/rule-test.js +++ b/test/marks/rule-test.js @@ -2,11 +2,11 @@ import * as Plot from "@observablehq/plot"; import tape from "tape-await"; tape("ruleX() has the expected defaults", test => { - const rule = Plot.ruleX([42]); - test.deepEqual(rule.data, [42]); + const rule = Plot.ruleX(); + test.strictEqual(rule.data, undefined); test.strictEqual(rule.transform, undefined); test.deepEqual(rule.channels.map(c => c.name), ["x"]); - test.deepEqual(rule.initialize().channels.map(d => d[1].value), [[ 42 ]]); + test.deepEqual(rule.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]); test.deepEqual(rule.channels.map(c => c.scale), ["x"]); test.strictEqual(rule.fill, undefined); test.strictEqual(rule.fillOpacity, undefined); @@ -96,11 +96,11 @@ tape("ruleX(data, {x, y1, y2}) specifies x, y1, y2", test => { }); tape("ruleY() has the expected defaults", test => { - const rule = Plot.ruleY([42]); - test.deepEqual(rule.data, [42]); + const rule = Plot.ruleY(); + test.strictEqual(rule.data, undefined); test.strictEqual(rule.transform, undefined); test.deepEqual(rule.channels.map(c => c.name), ["y"]); - test.deepEqual(rule.initialize().channels.map(d => d[1].value), [[ 42 ]]); + test.deepEqual(rule.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]); test.deepEqual(rule.channels.map(c => c.scale), ["y"]); test.strictEqual(rule.fill, undefined); test.strictEqual(rule.fillOpacity, undefined); diff --git a/test/marks/tick-test.js b/test/marks/tick-test.js index 453d8e758a..ffec65d807 100644 --- a/test/marks/tick-test.js +++ b/test/marks/tick-test.js @@ -2,11 +2,11 @@ import * as Plot from "@observablehq/plot"; import tape from "tape-await"; tape("tickX() has the expected defaults", test => { - const tick = Plot.tickX([42]); - test.deepEqual(tick.data, [42]); + const tick = Plot.tickX(); + test.strictEqual(tick.data, undefined); test.strictEqual(tick.transform, undefined); test.deepEqual(tick.channels.map(c => c.name), ["x"]); - test.deepEqual(tick.initialize().channels.map(d => d[1].value), [[ 42 ]]); + test.deepEqual(tick.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]); test.deepEqual(tick.channels.map(c => c.scale), ["x"]); test.strictEqual(tick.fill, undefined); test.strictEqual(tick.fillOpacity, undefined); @@ -61,11 +61,11 @@ tape("tickX(data, {stroke}) allows stroke to be a variable color", test => { }); tape("tickY() has the expected defaults", test => { - const tick = Plot.tickY([42]); - test.deepEqual(tick.data, [42]); + const tick = Plot.tickY(); + test.strictEqual(tick.data, undefined); test.strictEqual(tick.transform, undefined); test.deepEqual(tick.channels.map(c => c.name), ["y"]); - test.deepEqual(tick.initialize().channels.map(d => d[1].value), [[ 42 ]]); + test.deepEqual(tick.channels.map(c => Plot.valueof([1, 2, 3], c.value)), [[1, 2, 3]]); test.deepEqual(tick.channels.map(c => c.scale), ["y"]); test.strictEqual(tick.fill, undefined); test.strictEqual(tick.fillOpacity, undefined); From 81c8150de29e693df2896517ba79ce60fc54a271 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sat, 20 Mar 2021 11:41:03 -0700 Subject: [PATCH 4/4] use Plot.valueof to test --- test/marks/cell-test.js | 2 +- test/marks/dot-test.js | 2 +- test/marks/line-test.js | 2 +- test/marks/text-test.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/marks/cell-test.js b/test/marks/cell-test.js index 35b44d873d..5ab37425b3 100644 --- a/test/marks/cell-test.js +++ b/test/marks/cell-test.js @@ -6,7 +6,7 @@ tape("cell() has the expected defaults", test => { test.strictEqual(cell.data, undefined); test.strictEqual(cell.transform, undefined); test.deepEqual(cell.channels.map(c => c.name), ["x", "y"]); - test.deepEqual(cell.channels.map(c => c.value([1, 2])), [1, 2]); + test.deepEqual(cell.channels.map(c => Plot.valueof([[1, 2], [3, 4]], c.value)), [[1, 3], [2, 4]]); test.deepEqual(cell.channels.map(c => c.scale), ["x", "y"]); test.strictEqual(cell.channels.find(c => c.name === "x").type, "band"); test.strictEqual(cell.channels.find(c => c.name === "y").type, "band"); diff --git a/test/marks/dot-test.js b/test/marks/dot-test.js index b700655640..74e7591485 100644 --- a/test/marks/dot-test.js +++ b/test/marks/dot-test.js @@ -6,7 +6,7 @@ tape("dot() has the expected defaults", test => { test.strictEqual(dot.data, undefined); test.strictEqual(dot.transform, undefined); test.deepEqual(dot.channels.map(c => c.name), ["x", "y"]); - test.deepEqual(dot.channels.map(c => c.value([1, 2])), [1, 2]); + test.deepEqual(dot.channels.map(c => Plot.valueof([[1, 2], [3, 4]], c.value)), [[1, 3], [2, 4]]); test.deepEqual(dot.channels.map(c => c.scale), ["x", "y"]); test.strictEqual(dot.r, 3); test.strictEqual(dot.fill, "none"); diff --git a/test/marks/line-test.js b/test/marks/line-test.js index 396ab301c1..bb0b66579b 100644 --- a/test/marks/line-test.js +++ b/test/marks/line-test.js @@ -7,7 +7,7 @@ tape("line() has the expected defaults", test => { test.strictEqual(line.data, undefined); test.strictEqual(line.transform, undefined); test.deepEqual(line.channels.map(c => c.name), ["x", "y"]); - test.deepEqual(line.channels.map(c => c.value([1, 2])), [1, 2]); + test.deepEqual(line.channels.map(c => Plot.valueof([[1, 2], [3, 4]], c.value)), [[1, 3], [2, 4]]); test.deepEqual(line.channels.map(c => c.scale), ["x", "y"]); test.strictEqual(line.curve, curveLinear); test.strictEqual(line.fill, "none"); diff --git a/test/marks/text-test.js b/test/marks/text-test.js index 96045516dd..9a0d2a8e08 100644 --- a/test/marks/text-test.js +++ b/test/marks/text-test.js @@ -6,7 +6,7 @@ tape("text() has the expected defaults", test => { test.strictEqual(text.data, undefined); test.strictEqual(text.transform, undefined); test.deepEqual(text.channels.map(c => c.name), ["x", "y", "text"]); - test.deepEqual(text.channels.map(c => c.value([1, 2], 0)), [1, 2, 0]); + test.deepEqual(text.channels.map(c => Plot.valueof([[1, 2], [3, 4]], c.value)), [[1, 3], [2, 4], [0, 1]]); test.deepEqual(text.channels.map(c => c.scale), ["x", "y", undefined]); test.strictEqual(text.fill, undefined); test.strictEqual(text.fillOpacity, undefined);