Unist Utility: Find Node After

5.0.0 · active · verified Sun Apr 19

This package, `unist-util-find-after`, provides a lightweight utility for navigating unist (Universal Syntax Tree) trees by locating a node immediately following a specified node or index within a parent. Currently at stable version 5.0.0, it adheres to semantic versioning with major releases introducing breaking changes, and maintains active development within the unified collective. A key differentiator is its seamless integration with other `unist` and `unified` ecosystem tools, simplifying tree manipulation tasks that could otherwise be implemented manually. It is explicitly an ESM-only package, requiring Node.js 16 or newer, and ships with full TypeScript type definitions to ensure robust development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `findAfter` to locate a node based on an index or a reference node, with an optional test, within a Unist tree.

import { u } from 'unist-builder';
import { findAfter } from 'unist-util-find-after';

// Example Unist tree structure
const tree = u('tree', [
  u('leaf', 'leaf 1'),
  u('parent', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
  u('leaf', 'leaf 4'),
  u('parent', [u('leaf', 'leaf 5')]),
  u('leaf', 'leaf 6'),
  u('empty'),
  u('leaf', 'leaf 7')
]);

// Find the first 'parent' node after the node at index 1 (which is 'parent' with 'leaf 2', 'leaf 3')
console.log('Finding parent after index 1:', findAfter(tree, 1, 'parent'));
// Expected output: { type: 'parent', children: [{ type: 'leaf', value: 'leaf 5' }]}

// Find the first 'leaf' node after 'leaf 4' (which is at index 2)
const leaf4 = tree.children[2];
console.log('Finding leaf after "leaf 4":', findAfter(tree, leaf4, 'leaf'));
// Expected output: { type: 'leaf', value: 'leaf 6' }

// Find a node after 'leaf 7' (the last child) - should be undefined
const leaf7 = tree.children[6];
console.log('Finding node after "leaf 7":', findAfter(tree, leaf7));
// Expected output: undefined

view raw JSON →