Unist Utility to Find All Nodes Between Two Points

2.1.0 · active · verified Sun Apr 19

Unist-util-find-all-between is a JavaScript utility for the Unist (Universal Syntax Tree) ecosystem, designed to locate and return a collection of child nodes positioned strictly between a specified start and end point within a given parent node. These start and end points can be defined either by their numerical index within the parent's children array or by a Unist node object itself. When node objects are provided, the utility internally relies on `unist-util-find` to locate them and `unist-util-is` for robust node testing. The current stable version is 2.1.0, indicating a mature and relatively stable API. As a specialized utility within the Unist ecosystem, it typically follows a stable release cadence, aligning with broader Unist updates rather than frequent independent feature releases. It offers a precise, range-based selection mechanism, differentiating itself from other `unist-util-find-*` tools by specifically focusing on the 'between' segment, excluding the start and end boundaries. This makes it ideal for operations requiring contextual analysis of AST segments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to find nodes between specified indices or node objects within a Unist tree, showing both basic and advanced usage with TypeScript.

import { between } from 'unist-util-find-all-between';
import { u } from 'unist-builder';
import type { Node } from 'unist';

const parent: Node = u('tree', [
  u('leaf', 'leaf 1'),
  u('node', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
  u('leaf', 'leaf 4'),
  u('node', [u('leaf', 'leaf 5')]),
  u('leaf', 'leaf 6'),
  u('void'),
  u('leaf', 'leaf 7')
]);

// Example 1: Find 'leaf' nodes between index 0 and 4 (exclusive)
const resultByIndex = between(parent, 0, 4, 'leaf');
console.log('Nodes between index 0 and 4 (type: leaf):', resultByIndex); // Expected: [ { type: 'leaf', value: 'leaf 4' } ]

// Example 2: Find 'node' nodes between specific node objects
const startNode: Node = { type: 'leaf', value: 'leaf 4' };
const endNode: Node = { type: 'leaf', value: 'leaf 6' };

const resultByNodes = between(parent, startNode, endNode, 'node');
console.log('Nodes between specific leaf nodes (type: node):', resultByNodes); // Expected: [ { type: 'node', children: [ { type: 'leaf', value: 'leaf 5' } ] } ]

view raw JSON →