Skip to content

Commit 13052a8

Browse files
mbostockFil
andauthored
type=module; update dependencies (#342)
* type=module; update dependencies * remove cjs bundle * add the *mode* aggregation (#463) the plot example shows the most common word of length x as a title for each bar closes #420 (cherry-pick of ea9f8cd) * fix path to d3’s package.json * remove erroneous hard wrap Co-authored-by: Philippe Rivière <[email protected]>
1 parent d58a749 commit 13052a8

33 files changed

+2780
-1677
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
strategy:
1717
matrix:
18-
node-version: [12.x]
18+
node-version: [14.x]
1919

2020
steps:
2121
- uses: actions/checkout@v2

CONTRIBUTING.md

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ cd plot
1010
yarn
1111
```
1212

13-
Plot is written in ES modules and uses [Snowpack](https://snowpack.dev/) for
14-
development; this means that you can edit the Plot source code and examples, and
15-
they’ll update live as you save changes. To start Snowpack:
13+
Plot is written in ES modules and uses [Snowpack](https://snowpack.dev/) for development; this means that you can edit the Plot source code and examples, and they’ll update live as you save changes. To start Snowpack:
1614

1715
```
1816
yarn dev
@@ -24,15 +22,9 @@ This should open http://localhost:8008/ where you can browse the tests.
2422

2523
Plot has both unit tests and snapshot tests.
2624

27-
**Unit tests** live in `test` and have the `-test.js` extension. These tests are
28-
written using [Tape](https://github.com/substack/tape) (more precisely,
29-
[tape-await](https://github.com/mbostock/tape-await) for easier async testing).
30-
Generally speaking, unit tests make specific assertions about the behavior of
31-
Plot’s internals and helper methods.
25+
**Unit tests** live in `test` and have the `-test.js` extension. These tests are written using [Mocha](https://mochajs.org). Generally speaking, unit tests make specific assertions about the behavior of Plot’s internals and helper methods.
3226

33-
**Snapshot tests** live in `test/plots`; these also serve as examples of how to
34-
use the Plot API. Each snapshot test defines a plot by exporting a default async
35-
function. For example, here’s a line chart using BLS unemployment data:
27+
**Snapshot tests** live in `test/plots`; these also serve as examples of how to use the Plot API. Each snapshot test defines a plot by exporting a default async function. For example, here’s a line chart using BLS unemployment data:
3628

3729
```js
3830
import * as Plot from "@observablehq/plot";
@@ -49,32 +41,19 @@ export default async function() {
4941
}
5042
```
5143

52-
When a snapshot test is run, its output is compared against the SVG snapshot
53-
saved in the `test/output` folder. This makes it easier to see the effect of
54-
code changes and to catch unintended changes.
44+
When a snapshot test is run, its output is compared against the SVG snapshot saved in the `test/output` folder. This makes it easier to see the effect of code changes and to catch unintended changes.
5545

56-
To add a new snapshot test, create a new JavaScript file in the `test/plots`
57-
folder. Then register your test in the test registry, `test/plots/index.js`.
58-
Once you’ve registered your test, it will also appear automatically in the test
59-
browser (http://localhost:8008), where you can inspect and debug the output.
60-
(Snapshot tests must have deterministic, reproducible behavior; they should not
61-
depend on live data, external servers, the current time, the weather, etc. To
62-
use randomness in a test, use a seeded random number generator such as
63-
[d3.randomLcg](https://github.com/d3/d3-random/blob/master/README.md#randomLcg).)
46+
To add a new snapshot test, create a new JavaScript file in the `test/plots` folder. Then register your test in the test registry, `test/plots/index.js`. Once you’ve registered your test, it will also appear automatically in the test browser (http://localhost:8008), where you can inspect and debug the output. (Snapshot tests must have deterministic, reproducible behavior; they should not depend on live data, external servers, the current time, the weather, etc. To use randomness in a test, use a seeded random number generator such as [d3.randomLcg](https://github.com/d3/d3-random/blob/master/README.md#randomLcg).)
6447

6548
To run the tests:
6649

6750
```
6851
yarn test
6952
```
7053

71-
This will automatically generate any missing snapshots in `test/output`, which
72-
you should remember to `git add` before committing your changes. (If you forget,
73-
your PR will fail in CI, and you’ll get a reminder.)
54+
This will automatically generate any missing snapshots in `test/output`, which you should remember to `git add` before committing your changes. (If you forget, your PR will fail in CI, and you’ll get a reminder.)
7455

75-
If your code intentionally changes some of the existing snapshots, simply blow
76-
away the existing snapshots and run the tests again. You can then review what’s
77-
changed using `git diff`.
56+
If your code intentionally changes some of the existing snapshots, simply blow away the existing snapshots and run the tests again. You can then review what’s changed using `git diff`.
7857

7958
```
8059
rm -rf test/output

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,7 @@ The following aggregation methods are supported:
992992
* *median* - the median value
993993
* *deviation* - the standard deviation
994994
* *variance* - the variance per [Welford’s algorithm](https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_online_algorithm)
995+
* *mode* - the value with the most occurrences
995996
* a function to be passed the array of values for each bin
996997
* an object with a *reduce* method
997998

esm.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

package.json

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
"url": "https://observablehq.com"
88
},
99
"license": "ISC",
10-
"main": "dist/plot.cjs.js",
10+
"type": "module",
11+
"main": "src/index.js",
12+
"module": "src/index.js",
1113
"jsdelivr": "dist/plot.umd.min.js",
1214
"unpkg": "dist/plot.umd.min.js",
13-
"module": "src/index.js",
15+
"exports": {
16+
"umd": "./dist/plot.umd.min.js",
17+
"default": "./src/index.js"
18+
},
1419
"repository": {
1520
"type": "git",
1621
"url": "https://github.com/observablehq/plot.git"
@@ -20,35 +25,38 @@
2025
"src/**/*.js"
2126
],
2227
"scripts": {
23-
"bundle": "rm -rf dist && rollup -c",
24-
"test": "mkdir -p test/output && tape -r ./esm.js -r esm -r module-alias/register 'test/**/*-test.js' | tap-dot && node -r ./esm.js -r esm -r module-alias/register test/plot.js | tap-dot && eslint src test",
25-
"prepublishOnly": "yarn bundle",
28+
"test": "mkdir -p test/output && mocha -r module-alias/register 'test/**/*-test.js' && mocha -r module-alias/register test/plot.js && eslint src test",
29+
"prepublishOnly": "rm -rf dist && rollup -c",
2630
"postpublish": "git push && git push --tags",
2731
"dev": "snowpack dev"
2832
},
2933
"_moduleAliases": {
3034
"@observablehq/plot": "./src/index.js"
3135
},
36+
"sideEffects": false,
3237
"devDependencies": {
3338
"@rollup/plugin-json": "^4.1.0",
34-
"@rollup/plugin-node-resolve": "^11.2.1",
39+
"@rollup/plugin-node-resolve": "^13.0.4",
3540
"clean-css": "^5.1.1",
3641
"eslint": "^7.12.1",
37-
"esm": "^3.2.25",
38-
"htl": "^0.2.5",
42+
"htl": "^0.3.0",
3943
"js-beautify": "^1.13.0",
4044
"jsdom": "^16.4.0",
4145
"jsesc": "^3.0.2",
46+
"mocha": "^9.0.3",
4247
"module-alias": "^2.2.2",
4348
"rollup": "^2.32.1",
4449
"rollup-plugin-terser": "^7.0.2",
45-
"snowpack": "^3.0.11",
46-
"tap-dot": "^2.0.0",
47-
"tape": "^4.13.3",
48-
"tape-await": "^0.1.2"
50+
"snowpack": "^3.0.11"
4951
},
5052
"dependencies": {
51-
"d3": "^6.7.0",
53+
"d3": "^7.0.0",
5254
"isoformat": "^0.1.0"
55+
},
56+
"engines": {
57+
"node": ">=12"
58+
},
59+
"publishConfig": {
60+
"registry": "https://npm.pkg.github.com"
5361
}
5462
}

rollup.config.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as meta from "./package.json";
1010
const filename = meta.name.split("/").pop();
1111

1212
// Resolve D3 dependency.
13-
const d3 = require("d3/package.json");
13+
const d3 = JSON.parse(fs.readFileSync("./node_modules/d3/package.json", "utf-8"));
1414
if (typeof d3.jsdelivr === "undefined") throw new Error("unable to resolve d3");
1515
const d3Path = `d3@${d3.version}/${d3.jsdelivr}`;
1616

@@ -55,14 +55,6 @@ const config = {
5555
};
5656

5757
export default [
58-
{
59-
...config,
60-
output: {
61-
...config.output,
62-
format: "cjs",
63-
file: `dist/${filename}.cjs.js`
64-
}
65-
},
6658
{
6759
...config,
6860
output: {

snowpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
alias: {
33
"@observablehq/plot": "./src/index.js"
44
},

src/transforms/group.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {group as grouper, sort, sum, deviation, min, max, mean, median, variance, InternSet} from "d3";
1+
import {group as grouper, sort, sum, deviation, min, max, mean, median, mode, variance, InternSet} from "d3";
22
import {firstof} from "../defined.js";
33
import {valueof, maybeColor, maybeInput, maybeTransform, maybeTuple, maybeLazyChannel, lazyChannel, first, identity, take, labelof, range} from "../mark.js";
44

@@ -149,6 +149,7 @@ export function maybeReduce(reduce, value) {
149149
case "mean": return reduceAccessor(mean);
150150
case "median": return reduceAccessor(median);
151151
case "variance": return reduceAccessor(variance);
152+
case "mode": return reduceAccessor(mode);
152153
}
153154
throw new Error("invalid reduce");
154155
}

test/.eslintrc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"env": {
3+
"mocha": true
4+
}
5+
}

test/cjs-test.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)