Allow optional row conversion function to d3_dsv.

This commit is contained in:
Mike Bostock 2013-03-02 13:54:44 -08:00
Родитель aeb620b3a8
Коммит 95543f7bab
4 изменённых файлов: 27 добавлений и 12 удалений

7
d3.js поставляемый
Просмотреть файл

@ -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);

4
d3.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -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);
}
}