unist-util-filter is a utility for the Unist ecosystem that creates a new, immutable tree containing only the nodes that pass a provided test function. Unlike `unist-util-remove`, which modifies the original tree in place, `unist-util-filter` ensures immutability, making it suitable for functional programming paradigms or scenarios where the original tree must be preserved. The current stable version is 5.0.1. Major releases are not on a fixed cadence but often coincide with updates to Node.js LTS lines and significant changes in TypeScript definitions or module resolution. It integrates seamlessly with `unist-util-is` for defining complex testing conditions and offers a `cascade` option to control whether parent nodes should be removed if all their children are filtered out, with `true` as the default. This utility provides a clean, predictable way to prune ASTs without side effects.
npm install unist-util-filterVerified import paths — ran on the pinned version, not inferred.
This example demonstrates filtering a Unist tree to retain only parent nodes and leaf nodes with even values, showing the immutable nature of the operation.
Upgrade your Node.js environment to version 16 or newer, or pin your dependency to `unist-util-filter@^4`.
Migrate your project to use ECMAScript Modules (ESM) or consider using dynamic `import()` for CommonJS contexts. Alternatively, use `unist-util-filter@^2` for full CommonJS compatibility.
Update your code to expect and handle `undefined` as a possible return value when the entire tree is filtered out.
Update your TypeScript code to use the `Options` type where `FilterOptions` was previously used.
Review and update TypeScript definitions related to the return type of `filter` to align with the type of the `tree` input.
If you need to preserve parent nodes when their children are filtered, explicitly set `{ cascade: false }` in the options object: `filter(tree, { cascade: false }, test)`.Update your project to use ES modules (`import`) or switch to a dynamic import statement (`import('unist-util-filter')`) if remaining in CommonJS.Ensure you are using `import { filter } from 'unist-util-filter'` and verify that the result of the `filter` call is not `undefined` before attempting to access properties or methods on it.Replace all instances of `FilterOptions` with `Options` in your TypeScript code, ensuring you use `import type { Options } from 'unist-util-filter'`.No dependency data recorded yet.