Skip to content

Commit 8ab3fbf

Browse files
committed
Merge branch 'count' of https://github.com/Fil/d3-array into Fil-count
2 parents 7b24eb1 + bb67a7e commit 8ab3fbf

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,16 @@ Map(3) {
341341

342342
To convert a Map to an Array, use [Array.from](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from). See [d3.group](#group) for examples.
343343

344+
<a name="count" href="#count">#</a> d3.<b>count</b>(<i>iterable</i>[, <i>accessor</i>]) · [Source](https://github.com/d3/d3-array/blob/master/src/count.js "Source")
345+
346+
Returns the number of valid number values (*i.e.*, not null, NaN, or undefined) in the specified *iterable*; accepts an accessor.
347+
348+
For example:
349+
350+
```js
351+
d3.count([{n: "Alice", age: NaN}, {n: "Bob", age: 18}, {n: "Other"}], d => d.age) // 1
352+
```
353+
344354
<a name="cross" href="#cross">#</a> d3.<b>cross</b>(<i>...iterables</i>[, <i>reducer</i>]) · [Source](https://github.com/d3/d3-array/blob/master/src/cross.js), [Examples](https://observablehq.com/@d3/d3-cross)
345355

346356
Returns the [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product) of the specified *iterables*. For example, if two iterables *a* and *b* are specified, for each element *i* in the iterable *a* and each element *j* in the iterable *b*, in order, invokes the specified *reducer* function passing the element *i* and element *j*. If a *reducer* is not specified, it defaults to a function which creates a two-element array for each pair:

src/count.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default function count(values, valueof) {
2+
let count = 0;
3+
if (valueof === undefined) {
4+
for (const value of values) {
5+
if (value != null && value >= value) {
6+
++count;
7+
}
8+
}
9+
} else {
10+
let index = -1;
11+
for (let value of values) {
12+
if ((value = valueof(value, ++index, values)) != null && value >= value) {
13+
++count;
14+
}
15+
}
16+
}
17+
return count;
18+
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {default as bisect, bisectRight, bisectLeft} from "./bisect.js";
22
export {default as ascending} from "./ascending.js";
33
export {default as bisector} from "./bisector.js";
4+
export {default as count} from "./count.js";
45
export {default as cross} from "./cross.js";
56
export {default as descending} from "./descending.js";
67
export {default as deviation} from "./deviation.js";

test/count-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var tape = require("tape"),
2+
arrays = require("../");
3+
4+
tape("count() accepts an iterable", function(test) {
5+
test.deepEqual(arrays.count([1, 2]), 2);
6+
test.deepEqual(arrays.count(new Set([1, 2])), 2);
7+
test.deepEqual(arrays.count(generate(1, 2)), 2);
8+
test.end();
9+
});
10+
11+
tape("count() ignores NaN, null", function(test) {
12+
test.deepEqual(arrays.count([NaN, null, 0, 1]), 2);
13+
test.end();
14+
});
15+
16+
tape("count() accepts an accessor", function(test) {
17+
test.deepEqual(arrays.count([{v:NaN}, {}, {v:0}, {v:1}], d => d.v), 2);
18+
test.deepEqual(arrays.count([{n: "Alice", age: NaN}, {n: "Bob", age: 18}, {n: "Other"}], d => d.age), 1);
19+
test.end();
20+
});
21+
22+
function* generate(...values) {
23+
yield* values;
24+
}

0 commit comments

Comments
 (0)