Allow optional row conversion function to d3_dsv.
This commit is contained in:
Родитель
aeb620b3a8
Коммит
95543f7bab
|
@ -5234,14 +5234,15 @@ d3 = function() {
|
|||
}
|
||||
function d3_dsv(delimiter, mimeType) {
|
||||
var reFormat = new RegExp('["' + delimiter + "\n]"), delimiterCode = delimiter.charCodeAt(0);
|
||||
function dsv(url, callback) {
|
||||
var xhr = d3.xhr(url, mimeType, callback), row;
|
||||
function dsv(url, row, callback) {
|
||||
if (arguments.length < 3) callback = row, row = null;
|
||||
var xhr = d3.xhr(url, mimeType, callback);
|
||||
xhr.row = function(_) {
|
||||
if (!arguments.length) return row;
|
||||
xhr.response((row = _) == null ? response : typedResponse(_));
|
||||
return xhr;
|
||||
};
|
||||
return xhr.row(null);
|
||||
return xhr.row(row);
|
||||
}
|
||||
function response(request) {
|
||||
return dsv.parse(request.responseText);
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -2,9 +2,9 @@ function d3_dsv(delimiter, mimeType) {
|
|||
var reFormat = new RegExp("[\"" + delimiter + "\n]"),
|
||||
delimiterCode = delimiter.charCodeAt(0);
|
||||
|
||||
function dsv(url, callback) {
|
||||
var xhr = d3.xhr(url, mimeType, callback),
|
||||
row;
|
||||
function dsv(url, row, callback) {
|
||||
if (arguments.length < 3) callback = row, row = null;
|
||||
var xhr = d3.xhr(url, mimeType, callback);
|
||||
|
||||
xhr.row = function(_) {
|
||||
if (!arguments.length) return row;
|
||||
|
@ -12,7 +12,7 @@ function d3_dsv(delimiter, mimeType) {
|
|||
return xhr;
|
||||
};
|
||||
|
||||
return xhr.row(null);
|
||||
return xhr.row(row);
|
||||
}
|
||||
|
||||
function response(request) {
|
||||
|
|
|
@ -14,19 +14,33 @@ suite.addBatch({
|
|||
});
|
||||
},
|
||||
"invokes the callback with the parsed CSV": function(csv) {
|
||||
assert.deepEqual(csv, [{"Hello":42,"World":"\"fish\""}]);
|
||||
assert.deepEqual(csv, [{"Hello":"42","World":"\"fish\""}]);
|
||||
},
|
||||
"overrides the mime type to text/csv": function(csv) {
|
||||
assert.equal(XMLHttpRequest._last._info.mimeType, "text/csv");
|
||||
},
|
||||
"": {
|
||||
"specifying a row conversion function": {
|
||||
topic: function() {
|
||||
var cb = this.callback;
|
||||
d3.csv("test/data/sample.csv", function(row) {
|
||||
row.Hello = -row.Hello;
|
||||
return row;
|
||||
}, function(error, csv) {
|
||||
cb(null, csv);
|
||||
});
|
||||
},
|
||||
"invokes the callback with the parsed CSV": function(csv) {
|
||||
assert.strictEqual(csv[0].Hello, -42);
|
||||
}
|
||||
},
|
||||
"attempting to load a file that does not exist": {
|
||||
topic: function() {
|
||||
var cb = this.callback;
|
||||
d3.csv("//does/not/exist.csv", function(error, csv) {
|
||||
cb(null, csv);
|
||||
});
|
||||
},
|
||||
"invokes the callback with undefined when an error occurs": function(csv) {
|
||||
"invokes the callback with undefined": function(csv) {
|
||||
assert.isUndefined(csv);
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче