From 523c7db14ae5f44ab35063ffb312e301b7e8c8ef Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sun, 2 Jun 2024 16:10:57 -0700 Subject: [PATCH 1/4] add index.js to sideEffects --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 59da041c0d..fb5d338e1e 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "@observablehq/plot": "./src/index.js" }, "sideEffects": [ + "./src/index.js", "./src/plot.js" ], "devDependencies": { From 933b6af166f0ce7851385dce6ff9763b56abe9a1 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sun, 2 Jun 2024 19:34:13 -0700 Subject: [PATCH 2/4] lift side effect up --- package.json | 3 +-- src/index.js | 8 ++++++++ src/plot.js | 7 ------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index fb5d338e1e..b875fe73c4 100644 --- a/package.json +++ b/package.json @@ -44,8 +44,7 @@ "@observablehq/plot": "./src/index.js" }, "sideEffects": [ - "./src/index.js", - "./src/plot.js" + "./src/index.js" ], "devDependencies": { "@observablehq/runtime": "^5.7.3", diff --git a/src/index.js b/src/index.js index 9fde7ce2d5..306e579414 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,11 @@ +import {Mark} from "./mark.js"; +import {plot} from "./plot.js"; + +// Note: This side-effect avoids a circular dependency. +Mark.prototype.plot = function plotThis({marks = [], ...options} = {}) { + return plot({...options, marks: [...marks, this]}); +}; + export {plot} from "./plot.js"; export {Mark, marks} from "./mark.js"; export {Area, area, areaX, areaY} from "./marks/area.js"; diff --git a/src/plot.js b/src/plot.js index 628108bd65..f0829af9cc 100644 --- a/src/plot.js +++ b/src/plot.js @@ -368,13 +368,6 @@ function createFigcaption(document, caption) { return e; } -function plotThis({marks = [], ...options} = {}) { - return plot({...options, marks: [...marks, this]}); -} - -// Note: This side-effect avoids a circular dependency. -Mark.prototype.plot = plotThis; - function flatMarks(marks) { return marks .flat(Infinity) From bc74ed1be2f9f430f9d8d51087ed905aa2e10468 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sun, 2 Jun 2024 19:40:17 -0700 Subject: [PATCH 3/4] side effect, not side-effect --- docs/features/transforms.md | 2 +- src/index.js | 2 +- src/mark.js | 2 +- src/transforms/basic.d.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/features/transforms.md b/docs/features/transforms.md index dac990022e..4c309c985c 100644 --- a/docs/features/transforms.md +++ b/docs/features/transforms.md @@ -181,7 +181,7 @@ The **transform** function is passed three arguments, *data*, *facets*, and *opt If the **transform** option is specified, it supersedes any basic transforms (*i.e.*, the **filter**, **sort** and **reverse** options are ignored). However, the **transform** option is rarely used directly; instead one of Plot’s built-in transforms are used, and these transforms automatically compose with the basic **filter**, **sort** and **reverse** transforms. -While transform functions often produce new *data* or *facets*, they may return the passed-in *data* and *facets* as-is, and often have a side-effect of constructing derived channels. For example, the count of elements in a [groupX transform](../transforms/group.md) might be returned as a new *y* channel. In this case, the transform is typically expressed as an options transform: a function that takes a mark *options* object and returns a new, transformed options object, where the returned options object implements the **transform** option. Transform functions should not mutate the input *data* or *facets*. Likewise options transforms should not mutate the input *options* object. +While transform functions often produce new *data* or *facets*, they may return the passed-in *data* and *facets* as-is, and often have a side effect of constructing derived channels. For example, the count of elements in a [groupX transform](../transforms/group.md) might be returned as a new *y* channel. In this case, the transform is typically expressed as an options transform: a function that takes a mark *options* object and returns a new, transformed options object, where the returned options object implements the **transform** option. Transform functions should not mutate the input *data* or *facets*. Likewise options transforms should not mutate the input *options* object. When implementing a custom transform for generic usage, keep in mind that it needs to be compatible with Plot’s [faceting system](./facets.md), which partitions the original dataset into discrete subsets. diff --git a/src/index.js b/src/index.js index 306e579414..8cca826197 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ import {Mark} from "./mark.js"; import {plot} from "./plot.js"; -// Note: This side-effect avoids a circular dependency. +// Note: this side effect avoids a circular dependency. Mark.prototype.plot = function plotThis({marks = [], ...options} = {}) { return plot({...options, marks: [...marks, this]}); }; diff --git a/src/mark.js b/src/mark.js index 0e867689b5..768105025e 100644 --- a/src/mark.js +++ b/src/mark.js @@ -131,7 +131,7 @@ export class Mark { } export function marks(...marks) { - marks.plot = Mark.prototype.plot; // Note: depends on side-effect in plot! + marks.plot = Mark.prototype.plot; return marks; } diff --git a/src/transforms/basic.d.ts b/src/transforms/basic.d.ts index fddc507801..d120ecf917 100644 --- a/src/transforms/basic.d.ts +++ b/src/transforms/basic.d.ts @@ -10,7 +10,7 @@ import type {ScaleFunctions} from "../scales.js"; * the data, *facets*, and the plot’s *options*. The transform function returns * new mark data and facets; the returned **data** defaults to the passed * *data*, and the returned **facets** defaults to the passed *facets*. The mark - * is the *this* context. Transform functions can also trigger side-effects, say + * is the *this* context. Transform functions can also trigger side effects, say * to populate lazily-derived columns; see also Plot.column. */ export type TransformFunction = ( From dee3060690da13320d3a9b6450f4fbb303eb1624 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Mon, 3 Jun 2024 08:22:13 -0700 Subject: [PATCH 4/4] Update src/index.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Philippe Rivière --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 8cca826197..9d8c162260 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import {Mark} from "./mark.js"; import {plot} from "./plot.js"; // Note: this side effect avoids a circular dependency. -Mark.prototype.plot = function plotThis({marks = [], ...options} = {}) { +Mark.prototype.plot = function ({marks = [], ...options} = {}) { return plot({...options, marks: [...marks, this]}); };