Skip to content

Commit

Permalink
Update README; restore scan test.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 20, 2019
1 parent 17f2cae commit 2709eb1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ Returns the least element of the specified *iterable* according to the specified

```js
const array = [{foo: 42}, {foo: 91}];
d3.scan(array, (a, b) => a.foo - b.foo); // {foo: 42}
d3.scan(array, (a, b) => b.foo - a.foo); // {foo: 91}
d3.least(array, (a, b) => a.foo - b.foo); // {foo: 42}
d3.least(array, (a, b) => b.foo - a.foo); // {foo: 91}
```

This function is similar to [min](#min), except it allows the use of a comparator rather than an accessor.
Expand All @@ -144,8 +144,8 @@ Returns the index of the least element of the specified *iterable* according to

```js
const array = [{foo: 42}, {foo: 91}];
d3.scan(array, (a, b) => a.foo - b.foo); // 0
d3.scan(array, (a, b) => b.foo - a.foo); // 1
d3.leastIndex(array, (a, b) => a.foo - b.foo); // 0
d3.leastIndex(array, (a, b) => b.foo - a.foo); // 1
```

This function is similar to [minIndex](#minIndex), except it allows the use of a comparator rather than an accessor.
Expand Down
46 changes: 46 additions & 0 deletions test/scan-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var tape = require("tape"),
arrays = require("../");

tape("scan(array) compares using natural order", function(test) {
test.strictEqual(arrays.scan([0, 1]), 0);
test.strictEqual(arrays.scan([1, 0]), 1);
test.strictEqual(arrays.scan([0, "1"]), 0);
test.strictEqual(arrays.scan(["1", 0]), 1);
test.strictEqual(arrays.scan(["10", "2"]), 0);
test.strictEqual(arrays.scan(["2", "10"]), 1);
test.strictEqual(arrays.scan(["10", "2", NaN]), 0);
test.strictEqual(arrays.scan([NaN, "10", "2"]), 1);
test.strictEqual(arrays.scan(["2", NaN, "10"]), 2);
test.strictEqual(arrays.scan([2, NaN, 10]), 0);
test.strictEqual(arrays.scan([10, 2, NaN]), 1);
test.strictEqual(arrays.scan([NaN, 10, 2]), 2);
test.end();
});

tape("scan(array, compare) compares using the specified compare function", function(test) {
var a = {name: "a"}, b = {name: "b"};
test.strictEqual(arrays.scan([a, b], function(a, b) { return a.name.localeCompare(b.name); }), 0);
test.strictEqual(arrays.scan([1, 0], arrays.descending), 0);
test.strictEqual(arrays.scan(["1", 0], arrays.descending), 0);
test.strictEqual(arrays.scan(["2", "10"], arrays.descending), 0);
test.strictEqual(arrays.scan(["2", NaN, "10"], arrays.descending), 0);
test.strictEqual(arrays.scan([2, NaN, 10], arrays.descending), 2);
test.end();
});

tape("scan(array) returns undefined if the array is empty", function(test) {
test.strictEqual(arrays.scan([]), undefined);
test.end();
});

tape("scan(array) returns undefined if the array contains only incomparable values", function(test) {
test.strictEqual(arrays.scan([NaN, undefined]), undefined);
test.strictEqual(arrays.scan([NaN, "foo"], function(a, b) { return a - b; }), undefined);
test.end();
});

tape("scan(array) returns the first of equal values", function(test) {
test.strictEqual(arrays.scan([2, 2, 1, 1, 0, 0, 0, 3, 0]), 4);
test.strictEqual(arrays.scan([3, 2, 2, 1, 1, 0, 0, 0, 3, 0], arrays.descending), 0);
test.end();
});

0 comments on commit 2709eb1

Please sign in to comment.