Skip to content

Commit 95543f7

Browse files
committed
Allow optional row conversion function to d3_dsv.
1 parent aeb620b commit 95543f7

4 files changed

Lines changed: 27 additions & 12 deletions

File tree

d3.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5234,14 +5234,15 @@ d3 = function() {
52345234
}
52355235
function d3_dsv(delimiter, mimeType) {
52365236
var reFormat = new RegExp('["' + delimiter + "\n]"), delimiterCode = delimiter.charCodeAt(0);
5237-
function dsv(url, callback) {
5238-
var xhr = d3.xhr(url, mimeType, callback), row;
5237+
function dsv(url, row, callback) {
5238+
if (arguments.length < 3) callback = row, row = null;
5239+
var xhr = d3.xhr(url, mimeType, callback);
52395240
xhr.row = function(_) {
52405241
if (!arguments.length) return row;
52415242
xhr.response((row = _) == null ? response : typedResponse(_));
52425243
return xhr;
52435244
};
5244-
return xhr.row(null);
5245+
return xhr.row(row);
52455246
}
52465247
function response(request) {
52475248
return dsv.parse(request.responseText);

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dsv/dsv.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ function d3_dsv(delimiter, mimeType) {
22
var reFormat = new RegExp("[\"" + delimiter + "\n]"),
33
delimiterCode = delimiter.charCodeAt(0);
44

5-
function dsv(url, callback) {
6-
var xhr = d3.xhr(url, mimeType, callback),
7-
row;
5+
function dsv(url, row, callback) {
6+
if (arguments.length < 3) callback = row, row = null;
7+
var xhr = d3.xhr(url, mimeType, callback);
88

99
xhr.row = function(_) {
1010
if (!arguments.length) return row;
1111
xhr.response((row = _) == null ? response : typedResponse(_));
1212
return xhr;
1313
};
1414

15-
return xhr.row(null);
15+
return xhr.row(row);
1616
}
1717

1818
function response(request) {

test/dsv/csv-test.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,33 @@ suite.addBatch({
1414
});
1515
},
1616
"invokes the callback with the parsed CSV": function(csv) {
17-
assert.deepEqual(csv, [{"Hello":42,"World":"\"fish\""}]);
17+
assert.deepEqual(csv, [{"Hello":"42","World":"\"fish\""}]);
1818
},
1919
"overrides the mime type to text/csv": function(csv) {
2020
assert.equal(XMLHttpRequest._last._info.mimeType, "text/csv");
2121
},
22-
"": {
22+
"specifying a row conversion function": {
23+
topic: function() {
24+
var cb = this.callback;
25+
d3.csv("test/data/sample.csv", function(row) {
26+
row.Hello = -row.Hello;
27+
return row;
28+
}, function(error, csv) {
29+
cb(null, csv);
30+
});
31+
},
32+
"invokes the callback with the parsed CSV": function(csv) {
33+
assert.strictEqual(csv[0].Hello, -42);
34+
}
35+
},
36+
"attempting to load a file that does not exist": {
2337
topic: function() {
2438
var cb = this.callback;
2539
d3.csv("//does/not/exist.csv", function(error, csv) {
2640
cb(null, csv);
2741
});
2842
},
29-
"invokes the callback with undefined when an error occurs": function(csv) {
43+
"invokes the callback with undefined": function(csv) {
3044
assert.isUndefined(csv);
3145
}
3246
}

0 commit comments

Comments
 (0)