Skip to content

Commit ccf792d

Browse files
Filmbostock
andauthored
Adopt type=module (#43)
* Adopt type=module follow changes in d3-format: * type=module * add exports * remove zip * license: ISC * update dependencies * format links to source * es6 and browser rather than no-undef * remove Sublime project * add eslint.json * update dependencies * tweak eslintrc * typeof test * Update README Co-authored-by: Mike Bostock <mbostock@gmail.com>
1 parent 15effa5 commit ccf792d

10 files changed

+967
-768
lines changed

.eslintrc.json

-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
},
77
"env": {
88
"es6": true,
9-
"node": true,
109
"browser": true
11-
},
12-
"rules": {
13-
"no-cond-assign": 0
1410
}
1511
}

.github/eslint.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "eslint-compact",
5+
"pattern": [
6+
{
7+
"regexp": "^(.+):\\sline\\s(\\d+),\\scol\\s(\\d+),\\s(Error|Warning|Info)\\s-\\s(.+)\\s\\((.+)\\)$",
8+
"file": 1,
9+
"line": 2,
10+
"column": 3,
11+
"severity": 4,
12+
"message": 5,
13+
"code": 6
14+
}
15+
]
16+
}
17+
]
18+
}

.github/workflows/node.js.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
2+
3+
name: Node.js CI
4+
5+
on:
6+
push:
7+
branches: [ main ]
8+
pull_request:
9+
branches: [ main ]
10+
11+
jobs:
12+
build:
13+
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
matrix:
18+
node-version: [14.x]
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- name: Use Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v1
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
- run: yarn --frozen-lockfile
27+
- run: |
28+
echo ::add-matcher::.github/eslint.json
29+
yarn run eslint src test --format=compact
30+
- run: yarn test

LICENSE

+13-27
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
Copyright 2016 Mike Bostock
2-
All rights reserved.
3-
4-
Redistribution and use in source and binary forms, with or without modification,
5-
are permitted provided that the following conditions are met:
6-
7-
* Redistributions of source code must retain the above copyright notice, this
8-
list of conditions and the following disclaimer.
9-
10-
* Redistributions in binary form must reproduce the above copyright notice,
11-
this list of conditions and the following disclaimer in the documentation
12-
and/or other materials provided with the distribution.
13-
14-
* Neither the name of the author nor the names of contributors may be used to
15-
endorse or promote products derived from this software without specific prior
16-
written permission.
17-
18-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22-
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25-
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1+
Copyright 2016-2021 Mike Bostock
2+
3+
Permission to use, copy, modify, and/or distribute this software for any purpose
4+
with or without fee is hereby granted, provided that the above copyright notice
5+
and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
9+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
11+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
12+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
13+
THIS SOFTWARE.

README.md

+34-27
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,42 @@
33
This module provides convenient parsing on top of [Fetch](https://fetch.spec.whatwg.org/). For example, to load a text file:
44

55
```js
6-
d3.text("/path/to/file.txt").then(function(text) {
7-
console.log(text); // Hello, world!
8-
});
6+
const text = await d3.text("/path/to/file.txt");
7+
console.log(text); // Hello, world!
98
```
109

1110
To load and parse a CSV file:
1211

1312
```js
14-
d3.csv("/path/to/file.csv").then(function(data) {
15-
console.log(data); // [{"Hello": "world"}, …]
16-
});
13+
const data = await d3.csv("/path/to/file.csv");
14+
console.log(data); // [{"Hello": "world"}, …]
1715
```
1816

1917
This module has built-in support for parsing [JSON](#json), [CSV](#csv), and [TSV](#tsv). You can parse additional formats by using [text](#text) directly. (This module replaced [d3-request](https://github.com/d3/d3-request).)
2018

2119
## Installing
2220

23-
If you use NPM, `npm install d3-fetch`. Otherwise, download the [latest release](https://github.com/d3/d3-fetch/releases/latest). You can also load directly from [d3js.org](https://d3js.org) as a [standalone library](https://d3js.org/d3-fetch.v1.min.js). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:
21+
If you use npm, `npm install d3-fetch`. You can also download the [latest release on GitHub](https://github.com/d3/d3-fetch/releases/latest). For vanilla HTML in modern browsers, import d3-fetch from Skypack:
22+
23+
```html
24+
<script type="module">
25+
26+
import {csv} from "https://cdn.skypack.dev/d3-fetch@3";
27+
28+
csv("/path/to/file.csv").then((data) => {
29+
console.log(data); // [{"Hello": "world"}, …]
30+
});
31+
32+
</script>
33+
```
34+
35+
For legacy environments, you can load d3-fetch’s UMD bundle from an npm-based CDN such as jsDelivr; a `d3` global is exported:
2436

2537
```html
26-
<script src="https://d3js.org/d3-dsv.v2.min.js"></script>
27-
<script src="https://d3js.org/d3-fetch.v2.min.js"></script>
38+
<script src="https://cdn.jsdelivr.net/npm/d3-fetch@3"></script>
2839
<script>
2940
30-
d3.csv("/path/to/file.csv").then(function(data) {
41+
d3.csv("/path/to/file.csv").then((data) => {
3142
console.log(data); // [{"Hello": "world"}, …]
3243
});
3344
@@ -36,69 +47,65 @@ d3.csv("/path/to/file.csv").then(function(data) {
3647

3748
## API Reference
3849

39-
<a name="blob" href="#blob">#</a> d3.<b>blob</b>(<i>input</i>[, <i>init</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/blob.js "Source")
50+
<a name="blob" href="#blob">#</a> d3.<b>blob</b>(<i>input</i>[, <i>init</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/blob.js "Source")
4051

4152
Fetches the binary file at the specified *input* URL as a Blob. If *init* is specified, it is passed along to the underlying call to [fetch](https://fetch.spec.whatwg.org/#fetch-method); see [RequestInit](https://fetch.spec.whatwg.org/#requestinit) for allowed fields.
4253

43-
<a name="buffer" href="#buffer">#</a> d3.<b>buffer</b>(<i>input</i>[, <i>init</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/buffer.js "Source")
54+
<a name="buffer" href="#buffer">#</a> d3.<b>buffer</b>(<i>input</i>[, <i>init</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/buffer.js "Source")
4455

4556
Fetches the binary file at the specified *input* URL as an ArrayBuffer. If *init* is specified, it is passed along to the underlying call to [fetch](https://fetch.spec.whatwg.org/#fetch-method); see [RequestInit](https://fetch.spec.whatwg.org/#requestinit) for allowed fields.
4657

47-
<a name="csv" href="#csv">#</a> d3.<b>csv</b>(<i>input</i>[, <i>init</i>][, <i>row</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/dsv.js "Source")
58+
<a name="csv" href="#csv">#</a> d3.<b>csv</b>(<i>input</i>[, <i>init</i>][, <i>row</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/dsv.js "Source")
4859

4960
Equivalent to [d3.dsv](#dsv) with the comma character as the delimiter.
5061

51-
<a name="dsv" href="#dsv">#</a> d3.<b>dsv</b>(<i>delimiter</i>, <i>input</i>[, <i>init</i>][, <i>row</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/dsv.js "Source")
62+
<a name="dsv" href="#dsv">#</a> d3.<b>dsv</b>(<i>delimiter</i>, <i>input</i>[, <i>init</i>][, <i>row</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/dsv.js "Source")
5263

5364
Fetches the [DSV](https://github.com/d3/d3-dsv) file at the specified *input* URL. If *init* is specified, it is passed along to the underlying call to [fetch](https://fetch.spec.whatwg.org/#fetch-method); see [RequestInit](https://fetch.spec.whatwg.org/#requestinit) for allowed fields. An optional *row* conversion function may be specified to map and filter row objects to a more-specific representation; see [*dsv*.parse](https://github.com/d3/d3-dsv#dsv_parse) for details. For example:
5465

5566
```js
56-
d3.dsv(",", "test.csv", function(d) {
67+
const data = await d3.dsv(",", "test.csv", (d) => {
5768
return {
5869
year: new Date(+d.Year, 0, 1), // convert "Year" column to Date
5970
make: d.Make,
6071
model: d.Model,
6172
length: +d.Length // convert "Length" column to number
6273
};
63-
}).then(function(data) {
64-
console.log(data);
6574
});
6675
```
6776

6877
If only one of *init* and *row* is specified, it is interpreted as the *row* conversion function if it is a function, and otherwise an *init* object.
6978

7079
See also [d3.csv](#csv) and [d3.tsv](#tsv).
7180

72-
<a name="html" href="#html">#</a> d3.<b>html</b>(<i>input</i>[, <i>init</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/xml.js "Source")
81+
<a name="html" href="#html">#</a> d3.<b>html</b>(<i>input</i>[, <i>init</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/xml.js "Source")
7382

7483
Fetches the file at the specified *input* URL as [text](#text) and then [parses it](https://developer.mozilla.org/docs/Web/API/DOMParser) as HTML. If *init* is specified, it is passed along to the underlying call to [fetch](https://fetch.spec.whatwg.org/#fetch-method); see [RequestInit](https://fetch.spec.whatwg.org/#requestinit) for allowed fields.
7584

76-
<a name="image" href="#image">#</a> d3.<b>image</b>(<i>input</i>[, <i>init</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/image.js "Source")
85+
<a name="image" href="#image">#</a> d3.<b>image</b>(<i>input</i>[, <i>init</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/image.js "Source")
7786

7887
Fetches the image at the specified *input* URL. If *init* is specified, sets any additional properties on the image before loading. For example, to enable an anonymous [cross-origin request](https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image):
7988

8089
```js
81-
d3.image("https://example.com/test.png", {crossOrigin: "anonymous"}).then(function(img) {
82-
console.log(img);
83-
});
90+
const img = await d3.image("https://example.com/test.png", {crossOrigin: "anonymous"});
8491
```
8592

86-
<a name="json" href="#json">#</a> d3.<b>json</b>(<i>input</i>[, <i>init</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/json.js "Source")
93+
<a name="json" href="#json">#</a> d3.<b>json</b>(<i>input</i>[, <i>init</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/json.js "Source")
8794

8895
Fetches the [JSON](http://json.org) file at the specified *input* URL. If *init* is specified, it is passed along to the underlying call to [fetch](https://fetch.spec.whatwg.org/#fetch-method); see [RequestInit](https://fetch.spec.whatwg.org/#requestinit) for allowed fields. If the server returns a status code of [204 No Content](https://developer.mozilla.org/docs/Web/HTTP/Status/204) or [205 Reset Content](https://developer.mozilla.org/docs/Web/HTTP/Status/205), the promise resolves to `undefined`.
8996

90-
<a name="svg" href="#svg">#</a> d3.<b>svg</b>(<i>input</i>[, <i>init</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/xml.js "Source")
97+
<a name="svg" href="#svg">#</a> d3.<b>svg</b>(<i>input</i>[, <i>init</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/xml.js "Source")
9198

9299
Fetches the file at the specified *input* URL as [text](#text) and then [parses it](https://developer.mozilla.org/docs/Web/API/DOMParser) as SVG. If *init* is specified, it is passed along to the underlying call to [fetch](https://fetch.spec.whatwg.org/#fetch-method); see [RequestInit](https://fetch.spec.whatwg.org/#requestinit) for allowed fields.
93100

94-
<a name="text" href="#text">#</a> d3.<b>text</b>(<i>input</i>[, <i>init</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/text.js "Source")
101+
<a name="text" href="#text">#</a> d3.<b>text</b>(<i>input</i>[, <i>init</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/text.js "Source")
95102

96103
Fetches the text file at the specified *input* URL. If *init* is specified, it is passed along to the underlying call to [fetch](https://fetch.spec.whatwg.org/#fetch-method); see [RequestInit](https://fetch.spec.whatwg.org/#requestinit) for allowed fields.
97104

98-
<a name="tsv" href="#tsv">#</a> d3.<b>tsv</b>(<i>input</i>[, <i>init</i>][, <i>row</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/dsv.js "Source")
105+
<a name="tsv" href="#tsv">#</a> d3.<b>tsv</b>(<i>input</i>[, <i>init</i>][, <i>row</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/dsv.js "Source")
99106

100107
Equivalent to [d3.dsv](#dsv) with the tab character as the delimiter.
101108

102-
<a name="xml" href="#xml">#</a> d3.<b>xml</b>(<i>input</i>[, <i>init</i>]) [<>](https://github.com/d3/d3-fetch/blob/master/src/xml.js "Source")
109+
<a name="xml" href="#xml">#</a> d3.<b>xml</b>(<i>input</i>[, <i>init</i>]) · [Source](https://github.com/d3/d3-fetch/blob/master/src/xml.js "Source")
103110

104111
Fetches the file at the specified *input* URL as [text](#text) and then [parses it](https://developer.mozilla.org/docs/Web/API/DOMParser) as XML. If *init* is specified, it is passed along to the underlying call to [fetch](https://fetch.spec.whatwg.org/#fetch-method); see [RequestInit](https://fetch.spec.whatwg.org/#requestinit) for allowed fields.

d3-fetch.sublime-project

-17
This file was deleted.

package.json

+28-21
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,52 @@
22
"name": "d3-fetch",
33
"version": "2.0.0",
44
"description": "Convenient parsing for Fetch.",
5+
"homepage": "https://d3js.org/d3-fetch/",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/d3/d3-fetch.git"
9+
},
510
"keywords": [
611
"d3",
712
"d3-module",
813
"fetch",
914
"ajax",
1015
"XMLHttpRequest"
1116
],
12-
"homepage": "https://d3js.org/d3-fetch/",
13-
"license": "BSD-3-Clause",
17+
"license": "ISC",
1418
"author": {
1519
"name": "Mike Bostock",
1620
"url": "http://bost.ocks.org/mike"
1721
},
18-
"main": "dist/d3-fetch.js",
19-
"unpkg": "dist/d3-fetch.min.js",
20-
"jsdelivr": "dist/d3-fetch.min.js",
21-
"module": "src/index.js",
22-
"repository": {
23-
"type": "git",
24-
"url": "https://github.com/d3/d3-fetch.git"
25-
},
22+
"type": "module",
2623
"files": [
2724
"dist/**/*.js",
2825
"src/**/*.js"
2926
],
30-
"scripts": {
31-
"pretest": "rollup -c",
32-
"test": "tape 'test/**/*-test.js' && eslint src",
33-
"prepublishOnly": "rm -rf dist && yarn test",
34-
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../${npm_package_name}/dist/${npm_package_name}.js ${npm_package_name}.v${npm_package_version%%.*}.js && cp ../${npm_package_name}/dist/${npm_package_name}.min.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git add ${npm_package_name}.v${npm_package_version%%.*}.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git commit -m \"${npm_package_name} ${npm_package_version}\" && git push && cd - && zip -j dist/${npm_package_name}.zip -- LICENSE README.md dist/${npm_package_name}.js dist/${npm_package_name}.min.js"
27+
"module": "src/index.js",
28+
"main": "src/index.js",
29+
"jsdelivr": "dist/d3-fetch.min.js",
30+
"unpkg": "dist/d3-fetch.min.js",
31+
"exports": {
32+
"umd": "./dist/d3-fetch.min.js",
33+
"default": "./src/index.js"
3534
},
35+
"sideEffects": false,
3636
"dependencies": {
37-
"d3-dsv": "1 - 2"
37+
"d3-dsv": "1 - 3"
3838
},
39-
"sideEffects": false,
4039
"devDependencies": {
41-
"eslint": "6",
42-
"rollup": "1",
43-
"rollup-plugin-terser": "5",
44-
"tape": "4"
40+
"eslint": "7",
41+
"mocha": "8",
42+
"rollup": "2",
43+
"rollup-plugin-terser": "7"
44+
},
45+
"scripts": {
46+
"test": "mocha 'test/**/*-test.js' && eslint src test",
47+
"prepublishOnly": "rm -rf dist && yarn test && rollup -c",
48+
"postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../${npm_package_name}/dist/${npm_package_name}.js ${npm_package_name}.v${npm_package_version%%.*}.js && cp ../${npm_package_name}/dist/${npm_package_name}.min.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git add ${npm_package_name}.v${npm_package_version%%.*}.js ${npm_package_name}.v${npm_package_version%%.*}.min.js && git commit -m \"${npm_package_name} ${npm_package_version}\" && git push && cd -"
49+
},
50+
"engines": {
51+
"node": ">=12"
4552
}
4653
}

test/.eslintrc.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "eslint:recommended",
3+
"parserOptions": {
4+
"sourceType": "module",
5+
"ecmaVersion": 8
6+
},
7+
"env": {
8+
"es6": true,
9+
"browser": true,
10+
"mocha": true
11+
}
12+
}

test/export-test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import assert from "assert";
2+
import * as d3 from "../src/index.js";
3+
4+
it("fetch methods", () => {
5+
assert.strictEqual(typeof d3.blob, "function");
6+
assert.strictEqual(typeof d3.buffer, "function");
7+
assert.strictEqual(typeof d3.csv, "function");
8+
assert.strictEqual(typeof d3.dsv, "function");
9+
assert.strictEqual(typeof d3.html, "function");
10+
assert.strictEqual(typeof d3.image, "function");
11+
assert.strictEqual(typeof d3.json, "function");
12+
assert.strictEqual(typeof d3.svg, "function");
13+
assert.strictEqual(typeof d3.text, "function");
14+
assert.strictEqual(typeof d3.tsv, "function");
15+
assert.strictEqual(typeof d3.xml, "function");
16+
});

0 commit comments

Comments
 (0)