unist-util-visit
- Version 5.2.0
- Published
- 36 kB
- 3 dependencies
- MIT license
Install
npm i unist-util-visityarn add unist-util-visitpnpm add unist-util-visitOverview
unist utility to visit nodes
Index
Functions
Type Aliases
Functions
function visit
visit: { <Tree extends UnistNode, Check extends unknown>( tree: Tree, check: Check, visitors: BuildVisitor<Tree, Check> | BuildVisitors<Tree, Check>, reverse?: boolean | null | undefined ): undefined; <Tree extends UnistNode, Check extends unknown>( tree: Tree, visitors: BuildVisitor<Tree, any> | BuildVisitors<Tree, any>, reverse?: boolean ): undefined;};Visit nodes.
This algorithm performs *depth-first* *tree traversal* in *preorder* (**NLR**) or if
reverseis given, in *reverse preorder* (**NRL**). Anexitvisitor is called in *postorder* (**LRN**) or ifreverseis given, in *reverse postorder* (**RLN**).You can choose which nodes are visited by passing a
test. For complex tests, you should test yourself in a visitor, as it will be faster and will have improved type information.Walking the tree is an intensive task. Make use of the return values of the visitor when possible. Instead of walking a tree multiple times, walk it once, use
unist-util-isto check if a node matches, and then perform different operations.You can change the tree. Changes inside a node made when entering it are walked, changes made when exiting it are not. See
VisitorandVisitorExitfor more info.Parameter tree
Tree to traverse.
Parameter check
unist-util-is-compatible test.Parameter visitors
Handle each node when entering it, or
{enter, exit}to also handle it when exiting it.Parameter reverse
Walk children from last to first; for
enter, reverse preorder (NRL) instead of preorder (NLR); forexit, reverse postorder (RLN) instead of postorder (LRN).Parameter tree
Tree to traverse.
Parameter visitors
Handle each node when entering it, or
{enter, exit}to also handle it when exiting it.Parameter reverse
Walk children from last to first; for
enter, reverse preorder (NRL) instead of preorder (NLR); forexit, reverse postorder (RLN) instead of postorder (LRN).Parameter tree
Tree to traverse.
Parameter testOrEnterOrVisitors
unist-util-is-compatible test,enter, orvisitors.Parameter enterOrVisitorsOrReverse
enter,visitors, orreverseafter a test.Parameter maybeReverse
Walk children from last to first; for
enter, reverse preorder (NRL) instead of preorder (NLR); forexit, reverse postorder (RLN) instead of postorder (LRN).Returns
{undefined} Nothing.
{UnistNode} Tree Node type. {Test} Check
unist-util-is-compatible test.
Type Aliases
type BuildVisitor
type BuildVisitor< Tree extends UnistNode = UnistNode, Check extends Test = Test> = BuildVisitorFromDescendants<InclusiveDescendant<Tree>, Check>;Build a typed
Visitorfunction from a tree and a test.It will infer which values are passed as
nodeand which asparent.
type BuildVisitors
type BuildVisitors< Tree extends UnistNode = UnistNode, Check extends Test = Test> = Visitors< Matches<InclusiveDescendant<Tree>, Check>, Parent< Extract<InclusiveDescendant<Tree>, UnistParent>, Matches<InclusiveDescendant<Tree>, Check> >>;Build typed
Visitorsfrom a tree and a test.It will infer which values are passed as
nodeand which asparent.
type Visitor
type Visitor< Visited extends UnistNode = UnistNode, Ancestor extends UnistParent = UnistParent> = ( node: Visited, index: Visited extends UnistNode ? number | undefined : never, parent: Ancestor extends UnistParent ? Ancestor | undefined : never) => VisitorResult;Handle a node (matching
test, if given) before its children.Like
Visitorinunist-util-visit-parents, but withindexandparentinstead ofparents.
type VisitorExit
type VisitorExit< Visited extends UnistNode = UnistNode, Ancestor extends UnistParent = UnistParent> = ( node: Visited, index: Visited extends UnistNode ? number | undefined : never, parent: Ancestor extends UnistParent ? Ancestor | undefined : never) => VisitorResultExit;Handle a node (matching
test, if given) after its children.Like
VisitorExitinunist-util-visit-parents, but withindexandparentinstead ofparents.
type Visitors
type Visitors< Visited extends UnistNode = UnistNode, Ancestor extends UnistParent = UnistParent> = { /** * Handle a node before its children (optional). */ enter?: Visitor<Visited, Ancestor> | null | undefined; /** * Handle a node after its children. */ exit: VisitorExit<Visited, Ancestor>;};Handle nodes when entering and exiting them.
Like
Visitorsinunist-util-visit-parents.
Package Files (2)
Dependencies (3)
Dev Dependencies (13)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/unist-util-visit.
- Markdown[](https://www.jsdocs.io/package/unist-util-visit)
- HTML<a href="https://www.jsdocs.io/package/unist-util-visit"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 2613 ms. - Missing or incorrect documentation? Open an issue for this package.
