Unist Utility for Direct Child Traversal

3.0.0 · active · verified Sun Apr 19

unist-util-visit-children is a focused utility within the unified ecosystem designed to create a reusable function for iterating over only the direct children of a given unist (Universal Syntax Tree) node. The current stable version is 3.0.0. Releases are tied to major Node.js version support, with new major versions often dropping support for unmaintained Node.js versions. While it provides a specific function for direct child visitation, the package's own documentation recommends using `unist-util-visit` for most general tree traversal needs due to its broader capabilities. It's distinct in its narrow scope, avoiding recursive traversal and providing a lightweight, dedicated mechanism for immediate child processing.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use `visitChildren` to iterate over the direct children of a unist tree node, logging each child's type.

import u from 'unist-builder';
import { visitChildren } from 'unist-util-visit-children';

const visitorFunction = function (node) {
  console.log(`Visiting direct child: ${node.type}`);
  if (node.value) {
    console.log(`  Value: ${node.value}`);
  }
};

const visit = visitChildren(visitorFunction);

const tree = u('root', [
  u('leaf', 'leaf 1'),
  u('parent-node', [
    u('child-leaf', 'child 2'),
    u('child-leaf', 'child 3')
  ]),
  u('leaf', 'leaf 4'),
  u('void-node')
]);

console.log('--- Traversing direct children of the root node ---');
visit(tree);

view raw JSON →