Skip to content

Commit 14717ea

Browse files
committed
Shorter names for convenience methods!
And backwards-compatible with 3.x. Renamed: d3.requestCsv ↦ d3.csv d3.requestHtml ↦ d3.html d3.requestJson ↦ d3.json d3.requestText ↦ d3.text d3.requestTsv ↦ d3.tsv d3.requestXml ↦ d3.xml See d3/d3-dsv#14.
1 parent 7cd123a commit 14717ea

15 files changed

+77
-77
lines changed

README.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This module provides a convenient alternative to XMLHttpRequest. For example, to load a text file:
44

55
```js
6-
d3.requestText("/path/to/file.txt", function(error, text) {
6+
d3.text("/path/to/file.txt", function(error, text) {
77
if (error) throw error;
88
console.log(text); // Hello, world!
99
});
@@ -12,7 +12,7 @@ d3.requestText("/path/to/file.txt", function(error, text) {
1212
To load and parse a CSV file:
1313

1414
```js
15-
d3.requestCsv("/path/to/file.csv", function(error, data) {
15+
d3.csv("/path/to/file.csv", function(error, data) {
1616
if (error) throw error;
1717
console.log(data); // [{"Hello": "world"}, …]
1818
});
@@ -26,7 +26,7 @@ d3.request("/path/to/resource")
2626
.post("a=2&b=3", callback);
2727
```
2828

29-
This module has built-in support for parsing [JSON](#requestJson), [XML](#requestXml), [CSV](#requestCsv) and [TSV](#requestTsv). You can parse additional formats by using [request](#request) or [requestText](#requestText) directly.
29+
This module has built-in support for parsing [JSON](#json), [XML](#xml), [CSV](#csv) and [TSV](#tsv). You can parse additional formats by using [request](#request) or [text](#text) directly.
3030

3131
## Installing
3232

@@ -35,8 +35,8 @@ If you use NPM, `npm install d3-request`. Otherwise, download the [latest releas
3535
```html
3636
<script src="https://d3js.org/d3-collection.v0.1.min.js"></script>
3737
<script src="https://d3js.org/d3-dispatch.v0.2.min.js"></script>
38-
<script src="https://d3js.org/d3-dsv.v0.1.min.js"></script>
39-
<script src="https://d3js.org/d3-request.v0.3.min.js"></script>
38+
<script src="https://d3js.org/d3-dsv.v0.2.min.js"></script>
39+
<script src="https://d3js.org/d3-request.v0.4.min.js"></script>
4040
```
4141

4242
In a vanilla environment, a `d3_request` global is exported.
@@ -88,7 +88,7 @@ If *type* is specified, sets the [response type](http://www.w3.org/TR/XMLHttpReq
8888

8989
<a name="request_response" href="#request_response">#</a> <i>request</i>.<b>response</b>(<i>value</i>)
9090

91-
Sets the response value function to the specified function and returns this request instance. The response value function is used to map the response XMLHttpRequest object to a useful data value. See the convenience methods [requestJson](#requestJson) and [requestText](#requestText) for examples.
91+
Sets the response value function to the specified function and returns this request instance. The response value function is used to map the response XMLHttpRequest object to a useful data value. See the convenience methods [json](#json) and [text](#text) for examples.
9292

9393
<a name="request_get" href="#request_get">#</a> <i>request</i>.<b>get</b>([<i>callback</i>])
9494

@@ -136,7 +136,7 @@ The type must be one of the following:
136136

137137
To register multiple listeners for the same *type*, the type may be followed by an optional name, such as `"load.foo"` and `"load.bar"`. See [d3-dispatch](https://github.com/d3/d3-dispatch) for details.
138138

139-
<a name="requestCsv" href="#requestCsv">#</a> d3.<b>requestCsv</b>(<i>url</i>[, <i>row</i>][, <i>callback</i>])
139+
<a name="csv" href="#csv">#</a> d3.<b>csv</b>(<i>url</i>[, <i>row</i>][, <i>callback</i>])
140140

141141
Creates a request for the [CSV](https://github.com/d3/d3-dsv#csv) file at the specified *url* with the default mime type `"text/csv"`. 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:
142142

@@ -154,13 +154,13 @@ function row(d) {
154154
The *row* conversion function can be changed by calling *request*.row on the returned instance. For example, this:
155155

156156
```js
157-
d3.requestCsv(url, row, callback);
157+
d3.csv(url, row, callback);
158158
```
159159

160160
Is equivalent to this:
161161

162162
```js
163-
d3.requestCsv(url)
163+
d3.csv(url)
164164
.row(row)
165165
.get(callback);
166166
```
@@ -174,7 +174,7 @@ d3.request(url)
174174
.get(callback);
175175
```
176176

177-
<a name="requestHtml" href="#requestHtml">#</a> d3.<b>requestHtml</b>(<i>url</i>[, <i>callback</i>])
177+
<a name="html" href="#html">#</a> d3.<b>html</b>(<i>url</i>[, <i>callback</i>])
178178

179179
Creates a request for the HTML file at the specified *url* with the default mime type "text/html". The HTML file is returned as a [document fragment](https://developer.mozilla.org/en-US/docs/DOM/range.createContextualFragment).
180180

@@ -187,7 +187,7 @@ d3.request(url)
187187
.get(callback);
188188
```
189189

190-
<a name="requestJson" href="#requestJson">#</a> d3.<b>requestJson</b>(<i>url</i>[, <i>callback</i>])
190+
<a name="json" href="#json">#</a> d3.<b>json</b>(<i>url</i>[, <i>callback</i>])
191191

192192
Creates a request for the [JSON](http://json.org) file at the specified *url* with the default mime type `"application/json"`.
193193

@@ -200,7 +200,7 @@ d3.request(url)
200200
.get(callback);
201201
```
202202

203-
<a name="requestText" href="#requestText">#</a> d3.<b>requestText</b>(<i>url</i>[, <i>callback</i>])
203+
<a name="text" href="#text">#</a> d3.<b>text</b>(<i>url</i>[, <i>callback</i>])
204204

205205
Creates a request for the text file at the specified *url* with the default mime type `"text/plain"`.
206206

@@ -213,7 +213,7 @@ d3.request(url)
213213
.get(callback);
214214
```
215215

216-
<a name="requestTsv" href="#requestTsv">#</a> d3.<b>requestTsv</b>(<i>url</i>[, <i>row</i>][, <i>callback</i>])
216+
<a name="tsv" href="#tsv">#</a> d3.<b>tsv</b>(<i>url</i>[, <i>row</i>][, <i>callback</i>])
217217

218218
Creates a request for the [TSV](https://github.com/d3/d3-dsv#tsv) file at the specified *url* with the default mime type `"text/tab-separated-values"`. 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:
219219

@@ -231,13 +231,13 @@ function row(d) {
231231
The *row* conversion function can be changed by calling *request*.row on the returned instance. For example, this:
232232

233233
```js
234-
d3.requestTsv(url, row, callback);
234+
d3.tsv(url, row, callback);
235235
```
236236

237237
Is equivalent to this:
238238

239239
```js
240-
d3.requestTsv(url)
240+
d3.tsv(url)
241241
.row(row)
242242
.get(callback);
243243
```
@@ -251,7 +251,7 @@ d3.request(url)
251251
.get(callback);
252252
```
253253

254-
<a name="requestXml" href="#requestXml">#</a> d3.<b>requestXml</b>(<i>url</i>[, <i>callback</i>])
254+
<a name="xml" href="#xml">#</a> d3.<b>xml</b>(<i>url</i>[, <i>callback</i>])
255255

256256
Creates a request for the XML file at the specified *url* with the default mime type `"application/xml"`.
257257

index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export {default as request} from "./src/request";
2-
export {default as requestHtml} from "./src/html";
3-
export {default as requestJson} from "./src/json";
4-
export {default as requestText} from "./src/text";
5-
export {default as requestXml} from "./src/xml";
6-
export {default as requestCsv} from "./src/csv";
7-
export {default as requestTsv} from "./src/tsv";
2+
export {default as html} from "./src/html";
3+
export {default as json} from "./src/json";
4+
export {default as text} from "./src/text";
5+
export {default as xml} from "./src/xml";
6+
export {default as csv} from "./src/csv";
7+
export {default as tsv} from "./src/tsv";

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "d3-request",
3-
"version": "0.3.2",
3+
"version": "0.4.0",
44
"description": "A convenient alternative to XMLHttpRequest.",
55
"keywords": [
66
"d3",
@@ -24,12 +24,12 @@
2424
"pretest": "mkdir -p build && node -e 'process.stdout.write(\"var version = \\\"\" + require(\"./package.json\").version + \"\\\"; export * from \\\"../index\\\"; export {version};\");' > build/bundle.js && rollup -f umd -g d3-collection:d3_collection,d3-dispatch:d3_dispatch,d3-dsv:d3_dsv -n d3_request -o build/d3-request.js -- build/bundle.js",
2525
"test": "faucet `find test -name '*-test.js'` && eslint index.js src",
2626
"prepublish": "npm run test && uglifyjs build/d3-request.js -c -m -o build/d3-request.min.js && rm -f build/d3-request.zip && zip -j build/d3-request.zip -- LICENSE README.md build/d3-request.js build/d3-request.min.js",
27-
"postpublish": "VERSION=`node -e 'console.log(require(\"./package.json\").version)'`; git push && git tag -am \"Release $VERSION.\" v${VERSION} && git push --tags && cp build/d3-request.js ../d3.github.com/d3-request.v0.3.js && cp build/d3-request.min.js ../d3.github.com/d3-request.v0.3.min.js && cd ../d3.github.com && git add d3-request.v0.3.js d3-request.v0.3.min.js && git commit -m \"d3-request ${VERSION}\" && git push"
27+
"postpublish": "VERSION=`node -e 'console.log(require(\"./package.json\").version)'`; git push && git tag -am \"Release $VERSION.\" v${VERSION} && git push --tags && cp build/d3-request.js ../d3.github.com/d3-request.v0.4.js && cp build/d3-request.min.js ../d3.github.com/d3-request.v0.4.min.js && cd ../d3.github.com && git add d3-request.v0.4.js d3-request.v0.4.min.js && git commit -m \"d3-request ${VERSION}\" && git push"
2828
},
2929
"dependencies": {
3030
"d3-collection": "~0.1.1",
3131
"d3-dispatch": "~0.2.6",
32-
"d3-dsv": "~0.1.14"
32+
"d3-dsv": "~0.2.0"
3333
},
3434
"devDependencies": {
3535
"faucet": "0.0",

src/csv.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {csv} from "d3-dsv";
2-
import requestDsv from "./requestDsv";
1+
import {csvParse} from "d3-dsv";
2+
import dsv from "./dsv";
33

4-
export default requestDsv("text/csv", csv);
4+
export default dsv("text/csv", csvParse);

src/requestDsv.js src/dsv.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import request from "./request";
22

3-
export default function(defaultMimeType, dsv) {
3+
export default function(defaultMimeType, parse) {
44
return function(url, row, callback) {
55
if (arguments.length < 3) callback = row, row = null;
66
var r = request(url).mimeType(defaultMimeType);
7-
r.row = function(_) { return arguments.length ? r.response(responseOf(dsv, row = _)) : row; };
7+
r.row = function(_) { return arguments.length ? r.response(responseOf(parse, row = _)) : row; };
88
r.row(row);
99
return callback ? r.get(callback) : r;
1010
};
1111
}
1212

13-
function responseOf(dsv, row) {
13+
function responseOf(parse, row) {
1414
return function(request) {
15-
return dsv.parse(request.responseText, row);
15+
return parse(request.responseText, row);
1616
};
1717
}

src/html.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import requestType from "./requestType";
1+
import type from "./type";
22

3-
export default requestType("text/html", function(xhr) {
3+
export default type("text/html", function(xhr) {
44
return document.createRange().createContextualFragment(xhr.responseText);
55
});

src/json.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import requestType from "./requestType";
1+
import type from "./type";
22

3-
export default requestType("application/json", function(xhr) {
3+
export default type("application/json", function(xhr) {
44
return JSON.parse(xhr.responseText);
55
});

src/text.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import requestType from "./requestType";
1+
import type from "./type";
22

3-
export default requestType("text/plain", function(xhr) {
3+
export default type("text/plain", function(xhr) {
44
return xhr.responseText;
55
});

src/tsv.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {tsv} from "d3-dsv";
2-
import requestDsv from "./requestDsv";
1+
import {tsvParse} from "d3-dsv";
2+
import dsv from "./dsv";
33

4-
export default requestDsv("text/tab-separated-values", tsv);
4+
export default dsv("text/tab-separated-values", tsvParse);

src/requestType.js src/type.js

File renamed without changes.

src/xml.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import requestType from "./requestType";
1+
import type from "./type";
22

3-
export default requestType("application/xml", function(xhr) {
3+
export default type("application/xml", function(xhr) {
44
var xml = xhr.responseXML;
55
if (!xml) throw new Error("parse error");
66
return xml;

test/csv-test.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ var tape = require("tape"),
44

55
require("./XMLHttpRequest");
66

7-
tape("requestCsv(url, callback) makes an asynchronous GET request for a CSV file", function(test) {
8-
request.requestCsv("test/data/sample.csv", function(error, data) {
7+
tape("csv(url, callback) makes an asynchronous GET request for a CSV file", function(test) {
8+
request.csv("test/data/sample.csv", function(error, data) {
99
if (error) throw error;
1010
test.equal(XMLHttpRequest._last._info.url, "test/data/sample.csv");
1111
test.equal(XMLHttpRequest._last._info.method, "GET");
@@ -16,8 +16,8 @@ tape("requestCsv(url, callback) makes an asynchronous GET request for a CSV file
1616
});
1717
});
1818

19-
tape("requestCsv(url, callback) is an alias csv(url).get(callback)", function(test) {
20-
request.requestCsv("test/data/sample.csv").get(function(error, data) {
19+
tape("csv(url, callback) is an alias csv(url).get(callback)", function(test) {
20+
request.csv("test/data/sample.csv").get(function(error, data) {
2121
if (error) throw error;
2222
test.equal(XMLHttpRequest._last._info.url, "test/data/sample.csv");
2323
test.equal(XMLHttpRequest._last._info.method, "GET");
@@ -28,24 +28,24 @@ tape("requestCsv(url, callback) is an alias csv(url).get(callback)", function(te
2828
});
2929
});
3030

31-
tape("requestCsv(url, row, callback) observes the specified row conversion function", function(test) {
32-
request.requestCsv("test/data/sample.csv", function(d) { d.Hello = -d.Hello; return d; }, function(error, data) {
31+
tape("csv(url, row, callback) observes the specified row conversion function", function(test) {
32+
request.csv("test/data/sample.csv", function(d) { d.Hello = -d.Hello; return d; }, function(error, data) {
3333
if (error) throw error;
3434
test.deepEqual(data, table([{Hello: -42, World: "\"fish\""}], ["Hello", "World"]));
3535
test.end();
3636
});
3737
});
3838

39-
tape("requestCsv(url, row, callback) is an alias for csv(url).row(row).get(callback)", function(test) {
40-
request.requestCsv("test/data/sample.csv").row(function(d) { d.Hello = -d.Hello; return d; }).get(function(error, data) {
39+
tape("csv(url, row, callback) is an alias for csv(url).row(row).get(callback)", function(test) {
40+
request.csv("test/data/sample.csv").row(function(d) { d.Hello = -d.Hello; return d; }).get(function(error, data) {
4141
if (error) throw error;
4242
test.deepEqual(data, table([{Hello: -42, World: "\"fish\""}], ["Hello", "World"]));
4343
test.end();
4444
});
4545
});
4646

47-
tape("requestCsv(url).mimeType(type).get(callback) observes the specified mime type", function(test) {
48-
request.requestCsv("test/data/sample.csv").mimeType("text/plain").get(function(error, data) {
47+
tape("csv(url).mimeType(type).get(callback) observes the specified mime type", function(test) {
48+
request.csv("test/data/sample.csv").mimeType("text/plain").get(function(error, data) {
4949
if (error) throw error;
5050
test.deepEqual(data, table([{Hello: "42", World: "\"fish\""}], ["Hello", "World"]));
5151
test.equal(XMLHttpRequest._last._info.mimeType, "text/plain");

test/json-test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var tape = require("tape"),
33

44
require("./XMLHttpRequest");
55

6-
tape("requestJson(url, callback) makes an asynchronous GET request for a JSON file", function(test) {
7-
request.requestJson("test/data/sample.json", function(error, json) {
6+
tape("json(url, callback) makes an asynchronous GET request for a JSON file", function(test) {
7+
request.json("test/data/sample.json", function(error, json) {
88
if (error) throw error;
99
test.equal(XMLHttpRequest._last._info.url, "test/data/sample.json");
1010
test.equal(XMLHttpRequest._last._info.method, "GET");
@@ -17,15 +17,15 @@ tape("requestJson(url, callback) makes an asynchronous GET request for a JSON fi
1717
});
1818
});
1919

20-
tape("requestJson(url, callback) returns an error when given invalid JSON", function(test) {
21-
request.requestJson("test/data/sample.tsv", function(error, json) {
20+
tape("json(url, callback) returns an error when given invalid JSON", function(test) {
21+
request.json("test/data/sample.tsv", function(error, json) {
2222
test.ok(error instanceof SyntaxError);
2323
test.end();
2424
});
2525
});
2626

27-
tape("requestJson(url, callback) is an alias for json(url).get(callback)", function(test) {
28-
request.requestJson("test/data/sample.json").get(function(error, json) {
27+
tape("json(url, callback) is an alias for json(url).get(callback)", function(test) {
28+
request.json("test/data/sample.json").get(function(error, json) {
2929
if (error) throw error;
3030
test.equal(XMLHttpRequest._last._info.url, "test/data/sample.json");
3131
test.equal(XMLHttpRequest._last._info.method, "GET");
@@ -38,8 +38,8 @@ tape("requestJson(url, callback) is an alias for json(url).get(callback)", funct
3838
});
3939
});
4040

41-
tape("requestJson(url).mimeType(type).get(callback) observes the specified mime type", function(test) {
42-
request.requestJson("test/data/sample.json").mimeType("applicatin/json+test").get(function(error, json) {
41+
tape("json(url).mimeType(type).get(callback) observes the specified mime type", function(test) {
42+
request.json("test/data/sample.json").mimeType("applicatin/json+test").get(function(error, json) {
4343
if (error) throw error;
4444
test.equal(XMLHttpRequest._last._info.mimeType, "applicatin/json+test");
4545
test.deepEqual(json, {message: "Hello, world!"});

test/text-test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ var tape = require("tape"),
33

44
require("./XMLHttpRequest");
55

6-
tape("requestText(url, callback) makes an asynchronous GET request for a plain text file", function(test) {
7-
request.requestText("test/data/sample.txt", function(error, text) {
6+
tape("text(url, callback) makes an asynchronous GET request for a plain text file", function(test) {
7+
request.text("test/data/sample.txt", function(error, text) {
88
if (error) throw error;
99
test.equal(XMLHttpRequest._last._info.url, "test/data/sample.txt");
1010
test.equal(XMLHttpRequest._last._info.method, "GET");
@@ -17,8 +17,8 @@ tape("requestText(url, callback) makes an asynchronous GET request for a plain t
1717
});
1818
});
1919

20-
tape("requestText(url, callback) is an alias for text(url).get(callback)", function(test) {
21-
request.requestText("test/data/sample.txt").get(function(error, text) {
20+
tape("text(url, callback) is an alias for text(url).get(callback)", function(test) {
21+
request.text("test/data/sample.txt").get(function(error, text) {
2222
if (error) throw error;
2323
test.equal(XMLHttpRequest._last._info.url, "test/data/sample.txt");
2424
test.equal(XMLHttpRequest._last._info.method, "GET");
@@ -31,8 +31,8 @@ tape("requestText(url, callback) is an alias for text(url).get(callback)", funct
3131
});
3232
});
3333

34-
tape("requestText(url).mimeType(type).get(callback) observes the specified mime type", function(test) {
35-
request.requestText("test/data/sample.txt").mimeType("text/plain+special").get(function(error, text) {
34+
tape("text(url).mimeType(type).get(callback) observes the specified mime type", function(test) {
35+
request.text("test/data/sample.txt").mimeType("text/plain+special").get(function(error, text) {
3636
if (error) throw error;
3737
test.equal(XMLHttpRequest._last._info.mimeType, "text/plain+special");
3838
test.equal(text, "Hello, world!\n");

0 commit comments

Comments
 (0)