Skip to content

Commit

Permalink
Merge branch 'minmax' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 30, 2011
2 parents a08625d + d8c8b5d commit c7c2af6
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ tests: \
tests/test-insert.test \
tests/test-interpolate.test \
tests/test-keys.test \
tests/test-max.test \
tests/test-min.test \
tests/test-nest.test \
tests/test-permute.test \
tests/test-zip.test \
Expand Down
18 changes: 8 additions & 10 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,26 @@ d3.descending = function(a, b) {
return b < a ? -1 : b > a ? 1 : 0;
};
d3.min = function(array, f) {
var i = 0,
var i = -1,
n = array.length,
a = array[0],
a = Infinity,
b;
if (arguments.length === 1) {
while (++i < n) if (a > (b = array[i])) a = b;
while (++i < n) if ((b = array[i]) != null && a > b) a = b;
} else {
a = f(array[0]);
while (++i < n) if (a > (b = f(array[i]))) a = b;
while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
}
return a;
};
d3.max = function(array, f) {
var i = 0,
var i = -1,
n = array.length,
a = array[0],
a = -Infinity,
b;
if (arguments.length === 1) {
while (++i < n) if (a < (b = array[i])) a = b;
while (++i < n) if ((b = array[i]) != null && b > a) a = b;
} else {
a = f(a);
while (++i < n) if (a < (b = f(array[i]))) a = b;
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
}
return a;
};
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions src/core/max.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
d3.max = function(array, f) {
var i = 0,
var i = -1,
n = array.length,
a = array[0],
a = -Infinity,
b;
if (arguments.length === 1) {
while (++i < n) if (a < (b = array[i])) a = b;
while (++i < n) if ((b = array[i]) != null && b > a) a = b;
} else {
a = f(a);
while (++i < n) if (a < (b = f(array[i]))) a = b;
while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
}
return a;
};
9 changes: 4 additions & 5 deletions src/core/min.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
d3.min = function(array, f) {
var i = 0,
var i = -1,
n = array.length,
a = array[0],
a = Infinity,
b;
if (arguments.length === 1) {
while (++i < n) if (a > (b = array[i])) a = b;
while (++i < n) if ((b = array[i]) != null && a > b) a = b;
} else {
a = f(array[0]);
while (++i < n) if (a > (b = f(array[i]))) a = b;
while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
}
return a;
};
30 changes: 30 additions & 0 deletions tests/test-max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require("./../lib/env-js/envjs/node");
require("./../d3");

console.log("max:");
console.log(" " + d3.max([1, 2, 3, 4, 5]));
console.log("");

console.log("max with accessor function:");
console.log(" " + d3.max([[1, 2, 3, 4, 5], [2, 4, 6, 8, 10]], function(d) {
return d3.max(d);
}));
console.log("");

console.log("max index:");
console.log(" " + d3.max([1, 2, 3, 4, 5], function(d, i) {
return i;
}));
console.log("");

console.log("max with first element NaN:");
console.log(" " + d3.max([NaN, 1, 2, 3, 4, 5]));
console.log("");

console.log("max with last element NaN:");
console.log(" " + d3.max([1, 2, 3, 4, 5, NaN]));
console.log("");

console.log("max with null and undefined elements:");
console.log(" " + d3.max([-5, null, -3, undefined, -10, NaN]));
console.log("");
18 changes: 18 additions & 0 deletions tests/test-max.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
max:
5

max with accessor function:
10

max index:
4

max with first element NaN:
5

max with last element NaN:
5

max with null and undefined elements:
-3

30 changes: 30 additions & 0 deletions tests/test-min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require("./../lib/env-js/envjs/node");
require("./../d3");

console.log("min:");
console.log(" " + d3.min([1, 2, 3, 4, 5]));
console.log("");

console.log("min with accessor function:");
console.log(" " + d3.min([[1, 2, 3, 4, 5], [2, 4, 6, 8, 10]], function(d) {
return d3.max(d);
}));
console.log("");

console.log("min index:");
console.log(" " + d3.min([1, 2, 3, 4, 5], function(d, i) {
return i;
}));
console.log("");

console.log("min with first element NaN:");
console.log(" " + d3.min([NaN, 1, 2, 3, 4, 5]));
console.log("");

console.log("min with last element NaN:");
console.log(" " + d3.min([1, 2, 3, 4, 5, NaN]));
console.log("");

console.log("min with null and undefined elements:");
console.log(" " + d3.min([10, null, 3, undefined, 5, NaN]));
console.log("");
18 changes: 18 additions & 0 deletions tests/test-min.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
min:
1

min with accessor function:
5

min index:
0

min with first element NaN:
1

min with last element NaN:
1

min with null and undefined elements:
3

0 comments on commit c7c2af6

Please sign in to comment.