Skip to content

Commit af2c3bb

Browse files
mbostockFil
andauthored
lift side effect up to index.js (#2069)
* add index.js to sideEffects * lift side effect up * side effect, not side-effect * Update src/index.js Co-authored-by: Philippe Rivière <[email protected]> --------- Co-authored-by: Philippe Rivière <[email protected]>
1 parent 373d4f5 commit af2c3bb

File tree

6 files changed

+12
-11
lines changed

6 files changed

+12
-11
lines changed

docs/features/transforms.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ The **transform** function is passed three arguments, *data*, *facets*, and *opt
181181

182182
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.
183183

184-
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.
184+
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.
185185

186186
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.
187187

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@observablehq/plot": "./src/index.js"
4545
},
4646
"sideEffects": [
47-
"./src/plot.js"
47+
"./src/index.js"
4848
],
4949
"devDependencies": {
5050
"@observablehq/runtime": "^5.7.3",

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
import {Mark} from "./mark.js";
2+
import {plot} from "./plot.js";
3+
4+
// Note: this side effect avoids a circular dependency.
5+
Mark.prototype.plot = function ({marks = [], ...options} = {}) {
6+
return plot({...options, marks: [...marks, this]});
7+
};
8+
19
export {plot} from "./plot.js";
210
export {Mark, marks} from "./mark.js";
311
export {Area, area, areaX, areaY} from "./marks/area.js";

src/mark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class Mark {
131131
}
132132

133133
export function marks(...marks) {
134-
marks.plot = Mark.prototype.plot; // Note: depends on side-effect in plot!
134+
marks.plot = Mark.prototype.plot;
135135
return marks;
136136
}
137137

src/plot.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,6 @@ function createFigcaption(document, caption) {
368368
return e;
369369
}
370370

371-
function plotThis({marks = [], ...options} = {}) {
372-
return plot({...options, marks: [...marks, this]});
373-
}
374-
375-
// Note: This side-effect avoids a circular dependency.
376-
Mark.prototype.plot = plotThis;
377-
378371
function flatMarks(marks) {
379372
return marks
380373
.flat(Infinity)

src/transforms/basic.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {ScaleFunctions} from "../scales.js";
1010
* the data, *facets*, and the plot’s *options*. The transform function returns
1111
* new mark data and facets; the returned **data** defaults to the passed
1212
* *data*, and the returned **facets** defaults to the passed *facets*. The mark
13-
* is the *this* context. Transform functions can also trigger side-effects, say
13+
* is the *this* context. Transform functions can also trigger side effects, say
1414
* to populate lazily-derived columns; see also Plot.column.
1515
*/
1616
export type TransformFunction = (

0 commit comments

Comments
 (0)