Fix bugs in d3.zip.
* The returned array should be truncated to the length of the shortest argument. * With no arguments it should return an empty array. I've updated the unit tests to be more verbose to detect the array of single-element arrays returned for a single argument.
This commit is contained in:
Родитель
2403293455
Коммит
1baf57be30
|
@ -67,12 +67,13 @@ d3.max = function(array, f) {
|
|||
return a;
|
||||
};
|
||||
d3.zip = function() {
|
||||
var n = arguments.length,
|
||||
length = d3.max(arguments, function(d) { return d.length; }),
|
||||
var n = arguments.length;
|
||||
if (n === 0) return [];
|
||||
var length = d3.min(arguments, function(d) { return d.length; }),
|
||||
results = new Array(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
results[i] = new Array(n);
|
||||
for (var j = 0; j < length; j++) results[i][j] = arguments[j][i];
|
||||
for (var j = 0; j < n; j++) results[i][j] = arguments[j][i];
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -1,10 +1,11 @@
|
|||
d3.zip = function() {
|
||||
var n = arguments.length,
|
||||
length = d3.max(arguments, function(d) { return d.length; }),
|
||||
var n = arguments.length;
|
||||
if (n === 0) return [];
|
||||
var length = d3.min(arguments, function(d) { return d.length; }),
|
||||
results = new Array(length);
|
||||
for (var i = 0; i < length; i++) {
|
||||
results[i] = new Array(n);
|
||||
for (var j = 0; j < length; j++) results[i][j] = arguments[j][i];
|
||||
for (var j = 0; j < n; j++) results[i][j] = arguments[j][i];
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
|
|
@ -1,10 +1,28 @@
|
|||
require("./../lib/env-js/envjs/node");
|
||||
require("./../d3");
|
||||
|
||||
function s(array) {
|
||||
return "[" + array.map(function(d) {
|
||||
return "[" + d.join(",") + "]";
|
||||
}) + "]";
|
||||
}
|
||||
|
||||
console.log("zip [1, 2] [3, 4]:");
|
||||
console.log(" " + d3.zip([1, 2], [3, 4]));
|
||||
console.log(" " + s(d3.zip([1, 2], [3, 4])));
|
||||
console.log("");
|
||||
|
||||
console.log("zip [1, 2] [3, 4] [5, 6, 7]:");
|
||||
console.log(" " + d3.zip([1, 2], [3, 4], [5, 6, 7]));
|
||||
console.log(" " + s(d3.zip([1, 2], [3, 4], [5, 6, 7])));
|
||||
console.log("");
|
||||
|
||||
console.log("zip [1, 2, 3, 4, 5] [2, 4, 6, 8, 10]:");
|
||||
console.log(" " + s(d3.zip([1, 2, 3, 4, 5], [2, 4, 6, 8, 10])));
|
||||
console.log("");
|
||||
|
||||
console.log("zip [1, 2, 3, 4, 5]:");
|
||||
console.log(" " + s(d3.zip([1, 2, 3, 4, 5])));
|
||||
console.log("");
|
||||
|
||||
console.log("zip (no arguments):");
|
||||
console.log(" " + s(d3.zip()));
|
||||
console.log("");
|
||||
|
|
|
@ -1,6 +1,15 @@
|
|||
zip [1, 2] [3, 4]:
|
||||
1,3,2,4
|
||||
[[1,3],[2,4]]
|
||||
|
||||
zip [1, 2] [3, 4] [5, 6, 7]:
|
||||
1,3,5,2,4,6,,,7
|
||||
[[1,3,5],[2,4,6]]
|
||||
|
||||
zip [1, 2, 3, 4, 5] [2, 4, 6, 8, 10]:
|
||||
[[1,2],[2,4],[3,6],[4,8],[5,10]]
|
||||
|
||||
zip [1, 2, 3, 4, 5]:
|
||||
[[1],[2],[3],[4],[5]]
|
||||
|
||||
zip (no arguments):
|
||||
[]
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче