Registry / serialization / unist-util-is

unist-util-is

JSON →
library6.0.1jsnpmunverified

unist-util-is is a lightweight utility within the unified (remark/rehype) ecosystem designed to check if a unist (Universal Syntax Tree) node satisfies a given condition. It provides the `is` function to test nodes against various criteria, including their `type`, specific properties, or custom predicate functions. Additionally, it offers `convert` to pre-process tests, which is beneficial for performance when repeatedly checking multiple nodes. The package is currently stable at version 6.0.1 and maintains an active development pace, releasing updates for new Node.js versions and refining type definitions, generally aligning with the broader unified ecosystem's release cadence. It serves as a focused tool for basic node matching, distinguishing itself from `hast-util-is-element` (which targets HAST elements) and providing a simpler alternative to the more complex CSS selector-based matching of `unist-util-select`. Full TypeScript type definitions are included.

npm install unist-util-is
INSTALL
IMPORT
SIG · UNIST-UTIL-IS
U
unist-util-is
serializationjavascriptv6.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

is
import { is } from 'unist-util-is'
const is = require('unist-util-is'); import is from 'unist-util-is'
The package is ESM-only since v5.0.0 and does not have a default export. Always use named imports.
convert
import { convert } from 'unist-util-is'
const convert = require('unist-util-is').convert;
ESM-only since v5.0.0. `convert` is a named export, like `is`.
Test, Check
import type { Test, Check } from 'unist-util-is'
import { Test, Check } from 'unist-util-is'
These are TypeScript types. Use `import type` for clarity and to avoid runtime overhead.

This quickstart demonstrates the `is` function with various test types: no test, object checks, string types, direct node comparison, and a custom predicate function with index and parent context.

import {is} from 'unist-util-is' const node = {type: 'strong'} const parent = {type: 'paragraph', children: [node]} console.log('is()', is()) // => false console.log('is({children: []})', is({children: []})) // => false console.log('is(node)', is(node)) // => true console.log("is(node, 'strong')", is(node, 'strong')) // => true console.log("is(node, 'emphasis')", is(node, 'emphasis')) // => false console.log('is(node, node)', is(node, node)) // => true console.log("is(parent, {type: 'paragraph'})", is(parent, {type: 'paragraph'})) // => true console.log("is(parent, {type: 'strong'})", is(parent, {type: 'strong'})) // => false function customTest(node, n) { // In a real scenario, `n` would be the index, `parent` the parent node // For this example, we just check a condition that 'n' (simulated index) is 5 return n === 5 } // Demonstrating custom test function with index and parent context (simulated here) console.log('is(node, customTest)', is(node, customTest)) // => false (because n is undefined) console.log('is(node, customTest, 4, parent)', is(node, customTest, 4, parent)) // => false console.log('is(node, customTest, 5, parent)', is(node, customTest, 5, parent)) // => true
Debug
Known issues
breakingVersion 6.0.0 introduced a minimum Node.js requirement of 16. Older Node.js versions (e.g., 14) are no longer supported and will result in errors.
fix
Upgrade your Node.js environment to version 16 or newer.
affects: >=6.0.0
breakingStarting with version 5.0.0, unist-util-is is an ES Module (ESM) exclusively. Attempting to use `require()` for CommonJS imports will result in a runtime error.
fix
Migrate your codebase to use ES Module `import` syntax. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).
affects: >=5.0.0
breakingIn version 6.0.0, the type definitions for `unist-util-is` underwent significant changes. Explicit type parameters are generally no longer needed, and types like `AssertAnything`, `AssertPredicate`, `TestFunctionAnything`, `TestFunctionPredicate`, and `PredicateTest` have been renamed or removed. The new primary types are `Check` and `TestFunction`.
fix
Review your TypeScript code and update type annotations. Replace deprecated types with `Check` and `TestFunction` as appropriate. Ensure `@types/unist` is also updated to its latest compatible version.
affects: >=6.0.0
gotchaThis package does not provide a default export. Using `import is from 'unist-util-is'` will fail. All public API functions (`is`, `convert`) are named exports.
fix
Always use named imports: `import { is, convert } from 'unist-util-is'`.
affects: >=5.0.0
gotchaThe `is` function performs validation on `test`, `index`, and `parent` parameters, throwing an error if they are incorrect. However, if the `node` parameter itself is not a valid unist node, `is` will simply return `false` without throwing an error.
fix
When debugging, be aware that `is(null)` or `is(undefined)` will return `false`, whereas `is(node, null, 'invalid-index')` would throw an error. Handle `node` parameter validity separately if an explicit error is desired.
affects: >=4.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Attempting to `require('unist-util-is')` in a CommonJS module context.
fix
Change your import statement to use `import { is } from 'unist-util-is'` and ensure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json`).
TypeError: is is not a function
Incorrectly attempting a default import (e.g., `import is from 'unist-util-is'`) when `is` is a named export.
fix
Use a named import: `import { is } from 'unist-util-is'`.
Type 'TestFunctionAnything' is not assignable to type 'TestFunction'.
Using deprecated type aliases from `unist-util-is` v5 or earlier in a v6 project, often related to updating `@types/unist` as well.
fix
Update your code to use the new type aliases introduced in v6.0.0, specifically `Check` and `TestFunction`. Ensure `@types/unist` is also up-to-date.
Error: Cannot find module 'unist-util-is' or its corresponding type declarations.
This can stem from several issues: incorrect Node.js version, missing `@types/unist`, or improper ESM configuration.
fix
1. Verify Node.js version is 16+. 2. Ensure `@types/unist` is installed: `npm install @types/unist`. 3. Confirm your `tsconfig.json` (if applicable) and `package.json` are correctly configured for ES Modules.
Upgrade
Version history
6.0.1latest on npm
Audit
Dependencies
@types/unistrequiredProvides TypeScript type definitions for unist nodes, which unist-util-is heavily relies on for its own type safety and functionality. Required for full TypeScript support.
Agent activity
16 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
unist-util-is — npm install unist-util-is · libregistry