Skip to content

Commit

Permalink
Add constants for controlling how to visit
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 10, 2017
1 parent 06b1939 commit d8de3f1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
/* Expose. */
module.exports = visit;

visit.CONTINUE = true;
visit.EXIT = false;

var is = require('unist-util-is');

/* Visit. */
Expand All @@ -25,7 +28,7 @@ function visit(tree, test, visitor, reverse) {
result = visitor(node, index, parent || null);
}

if (node.children && result !== false) {
if (node.children && result !== visit.EXIT) {
return all(node.children, node);
}

Expand All @@ -39,17 +42,19 @@ function visit(tree, test, visitor, reverse) {
var min = -1;
var index = (reverse ? max : min) + step;
var child;
var result;

while (index > min && index < max) {
child = children[index];
result = child && one(child, index, parent);

if (child && one(child, index, parent) === false) {
return false;
if (result === visit.EXIT) {
return result;
}

index += step;
}

return true;
return visit.CONTINUE;
}
}
9 changes: 6 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ since child traversal is determined when the parent is visited.
This **does not** mean checking starts at the deepest node and
continues on to the highest node

#### `stop? = visitor(node, index, parent)`
#### `next? = visitor(node, index, parent)`

Invoked when a node (matching `test`, if given) is found.

Expand All @@ -74,7 +74,10 @@ Invoked when a node (matching `test`, if given) is found.

###### Returns

`boolean?` - When `false`, visiting is immediately stopped.
* `visit.EXIT` (`false`)
— Stop visiting immediately
* `visit.CONTINUE` (`true`)
— Continue visiting as normal (same behaviour as not returning anything)

## Related

Expand Down Expand Up @@ -129,7 +132,7 @@ repository, organisation, or community you agree to abide by its terms.

[is]: https://github.com/syntax-tree/unist-util-is#istest-node-index-parent-context

[visitor]: #stop--visitornode-index-parent
[visitor]: #next--visitornode-index-parent

[gh-9]: https://github.com/syntax-tree/unist-util-visit/issues/9

Expand Down

0 comments on commit d8de3f1

Please sign in to comment.