From d8de3f16ffcf802959548e1b2fd610702d701660 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sun, 10 Dec 2017 15:37:52 +0100 Subject: [PATCH] Add constants for controlling how to visit --- index.js | 13 +++++++++---- readme.md | 9 ++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index d51a975..06a1b97 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,9 @@ /* Expose. */ module.exports = visit; +visit.CONTINUE = true; +visit.EXIT = false; + var is = require('unist-util-is'); /* Visit. */ @@ -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); } @@ -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; } } diff --git a/readme.md b/readme.md index c752d68..0899bc5 100644 --- a/readme.md +++ b/readme.md @@ -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. @@ -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 @@ -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