Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invoke accessor once per value in d3.extent.
This allows the use of nondeterministic accessors. Also add a test.
- Loading branch information
Showing
4 changed files
with
57 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |