unist-util-visit

  • Version 5.1.0
  • Published
  • 33.8 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

Type Aliases

Functions

function visit

visit: {
<Tree extends UnistNode, Check extends unknown>(
tree: Tree,
check: Check,
visitor: BuildVisitor<Tree, Check>,
reverse?: boolean | null | undefined
): undefined;
<Tree extends UnistNode, Check extends unknown>(
tree: Tree,
visitor: BuildVisitor<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**).

    You can choose for which nodes visitor is called by passing a test. For complex tests, you should test yourself in 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. See Visitor for more info.

    Parameter tree

    Parameter check

    Parameter visitor

    Parameter reverse

    Parameter tree

    Parameter visitor

    Parameter reverse

    Parameter tree

    Tree to traverse.

    Parameter testOrVisitor

    unist-util-is-compatible test (optional, omit to pass a visitor).

    Parameter visitorOrReverse

    Handle each node (when test is omitted, pass reverse).

    Parameter maybeReverse

    Traverse in reverse preorder (NRL) instead of the default preorder (NLR).

    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 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).

    Visitors are free to transform node. They can also transform parent.

    Replacing node itself, if SKIP is not returned, still causes its descendants to be walked (which is a bug).

    When adding or removing previous siblings of node (or next siblings, in case of reverse), the Visitor should return a new Index to specify the sibling to traverse after node is traversed. Adding or removing next siblings of node (or previous siblings, in case of reverse) is handled as expected without needing to return a new Index.

    Removing the children property of parent still results in them being traversed.

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>