Skip to content

Commit

Permalink
Invoke accessor once per value in d3.extent.
Browse files Browse the repository at this point in the history
This allows the use of nondeterministic accessors. Also add a test.
  • Loading branch information
mbostock committed Nov 3, 2011
1 parent 6c8b308 commit 2b42dec
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
5 changes: 3 additions & 2 deletions d3.js
Expand Up @@ -105,8 +105,9 @@ d3.max = function(array, f) {
}
return a;
};
d3.extent = function() {
return [d3.min.apply(d3, arguments), d3.max.apply(d3, arguments)];
d3.extent = function(array, f) {
if (arguments.length > 1) array = array.map(f);
return [d3.min(array), d3.max(array)];
};
d3.random = {
normal: function(mean, deviation) {
Expand Down
2 changes: 1 addition & 1 deletion d3.min.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/core/extent.js
@@ -1,3 +1,4 @@
d3.extent = function() {
return [d3.min.apply(d3, arguments), d3.max.apply(d3, arguments)];
d3.extent = function(array, f) {
if (arguments.length > 1) array = array.map(f);
return [d3.min(array), d3.max(array)];
};
50 changes: 50 additions & 0 deletions test/core/extent-test.js
@@ -0,0 +1,50 @@
require("../env");
require("../../d3");

var vows = require("vows"),
assert = require("assert");

var suite = vows.describe("d3.extent");

suite.addBatch({
"extent": {
topic: function() {
return d3.extent;
},
"returns the numeric extent for numbers": function(extent) {
assert.deepEqual(extent([1]), [1, 1]);
assert.deepEqual(extent([5, 1, 2, 3, 4]), [1, 5]);
assert.deepEqual(extent([20, 3]), [3, 20]);
assert.deepEqual(extent([3, 20]), [3, 20]);
},
"returns the lexicographic extent for strings": function(extent) {
assert.deepEqual(extent(["c", "a", "b"]), ["a", "c"]);
assert.deepEqual(extent(["20", "3"]), ["20", "3"]);
assert.deepEqual(extent(["3", "20"]), ["20", "3"]);
},
"ignores null, undefined and NaN": function(extent) {
assert.deepEqual(extent([NaN, 1, 2, 3, 4, 5]), [1, 5]);
assert.deepEqual(extent([1, 2, 3, 4, 5, NaN]), [1, 5]);
assert.deepEqual(extent([10, null, 3, undefined, 5, NaN]), [3, 10]);
},
"compares heterogenous types as numbers": function(extent) {
assert.deepEqual(extent([20, "3"]), ["3", 20]);
assert.deepEqual(extent(["20", 3]), [3, "20"]);
assert.deepEqual(extent([3, "20"]), [3, "20"]);
assert.deepEqual(extent(["3", 20]), ["3", 20]);
},
"returns undefined for empty array": function(extent) {
assert.deepEqual(extent([]), [undefined, undefined]);
assert.deepEqual(extent([null]), [undefined, undefined]);
assert.deepEqual(extent([undefined]), [undefined, undefined]);
assert.deepEqual(extent([NaN]), [undefined, undefined]);
assert.deepEqual(extent([NaN, NaN]), [undefined, undefined]);
},
"applies the optional accessor function exactly once": function(extent) {
var i = 10;
assert.deepEqual(d3.extent([0,1,2,3], function() { return ++i; }), [11, 14]);
}
}
});

suite.export(module);

0 comments on commit 2b42dec

Please sign in to comment.