Skip to content

Commit

Permalink
Add node.find, node[Symbol.iterator].
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 17, 2019
1 parent 62a6211 commit 5e8701c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 18 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ root

You must call *node*.sort before invoking a hierarchical layout if you want the new sort order to affect the layout; see [*node*.sum](#node_sum) for an example.

<a name="node_iterator" href="#node_iterator">#</a> <i>node</i>[<b>Symbol.iterator</b>]() [<>](https://github.com/d3/d3-hierarchy/blob/master/src/hierarchy/iterator.js "Source")

Returns an iterator over the *node*’s descendants in breadth-first order. For example:

```js
for (const descendant of node) {
console.log(descendant);
}
```

<a name="node_each" href="#node_each">#</a> <i>node</i>.<b>each</b>(<i>function</i>[, <i>that</i>]) · [Source](https://github.com/d3/d3-hierarchy/blob/master/src/hierarchy/each.js), [Examples](https://observablehq.com/@d3/visiting-a-d3-hierarchy)

Invokes the specified *function* for *node* and each descendant in [breadth-first order](https://en.wikipedia.org/wiki/Breadth-first_search), such that a given *node* is only visited if all nodes of lesser depth have already been visited, as well as all preceding nodes of the same depth. The specified function is passed the current *descendant*, the zero-based traversal *index*, and this *node*. If *that* is specified, it is the this context of the callback.
Expand Down
6 changes: 1 addition & 5 deletions src/hierarchy/descendants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export default function() {
var nodes = [];
this.each(function(node) {
nodes.push(node);
});
return nodes;
return Array.from(this);
}
16 changes: 4 additions & 12 deletions src/hierarchy/each.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
export default function(callback, that) {
var node = this, current, next = [node], children, i, n, index = -1;
do {
current = next.reverse(), next = [];
while (node = current.pop()) {
callback.call(that, node, ++index, this);
if (children = node.children) {
for (i = 0, n = children.length; i < n; ++i) {
next.push(children[i]);
}
}
}
} while (next.length);
let index = -1;
for (const node of this) {
callback.call(that, node, ++index, this);
}
return this;
}
9 changes: 9 additions & 0 deletions src/hierarchy/find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function(callback, that) {
if (typeof callback !== "function") throw new Error("not a function");
let index = -1;
for (const node of this) {
if (callback.call(that, node, ++index, this)) {
return node;
}
}
}
6 changes: 5 additions & 1 deletion src/hierarchy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import node_count from "./count.js";
import node_each from "./each.js";
import node_eachBefore from "./eachBefore.js";
import node_eachAfter from "./eachAfter.js";
import node_find from "./find.js";
import node_sum from "./sum.js";
import node_sort from "./sort.js";
import node_path from "./path.js";
import node_ancestors from "./ancestors.js";
import node_descendants from "./descendants.js";
import node_leaves from "./leaves.js";
import node_links from "./links.js";
import node_iterator from "./iterator.js";

export default function hierarchy(data, children) {
var root = new Node(data),
Expand Down Expand Up @@ -68,12 +70,14 @@ Node.prototype = hierarchy.prototype = {
each: node_each,
eachAfter: node_eachAfter,
eachBefore: node_eachBefore,
find: node_find,
sum: node_sum,
sort: node_sort,
path: node_path,
ancestors: node_ancestors,
descendants: node_descendants,
leaves: node_leaves,
links: node_links,
copy: node_copy
copy: node_copy,
[Symbol.iterator]: node_iterator
};
14 changes: 14 additions & 0 deletions src/hierarchy/iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default function*() {
var node = this, current, next = [node], children, i, n;
do {
current = next.reverse(), next = [];
while (node = current.pop()) {
yield node;
if (children = node.children) {
for (i = 0, n = children.length; i < n; ++i) {
next.push(children[i]);
}
}
}
} while (next.length);
}

0 comments on commit 5e8701c

Please sign in to comment.