unist-util-visit

  • Version 5.2.0
  • Published
  • 36 kB
  • 3 dependencies
  • MIT license

Install

npm i unist-util-visit
yarn add unist-util-visit
pnpm add unist-util-visit

Overview

unist utility to visit nodes

Index

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 reverse is given, in *reverse preorder* (**NRL**). An exit visitor is called in *postorder* (**LRN**) or if reverse is 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-is to 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 Visitor and VisitorExit for 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); for exit, 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); for exit, reverse postorder (RLN) instead of postorder (LRN).

    Parameter tree

    Tree to traverse.

    Parameter testOrEnterOrVisitors

    unist-util-is-compatible test, enter, or visitors.

    Parameter enterOrVisitorsOrReverse

    enter, visitors, or reverse after a test.

    Parameter maybeReverse

    Walk children from last to first; for enter, reverse preorder (NRL) instead of preorder (NLR); for exit, 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 Visitor function from a tree and a test.

    It will infer which values are passed as node and which as parent.

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 Visitors from a tree and a test.

    It will infer which values are passed as node and which as parent.

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 Visitor in unist-util-visit-parents, but with index and parent instead of parents.

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 VisitorExit in unist-util-visit-parents, but with index and parent instead of parents.

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 Visitors in unist-util-visit-parents.

Package Files (2)

Dependencies (3)

Dev Dependencies (13)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto 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
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](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>