# unist-util-is 中文文档

规范地址：https://npmcn.com/package/unist-util-is/

版本：6\.0\.1
npm 元数据更新时间：2025-10-16T18:01:48.561Z
中文译文更新时间：2026-11-05T15:45:59.116Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/unist-util-is
原文对照：https://npmcn.com/package/unist-util-is/?lang=original
项目主页：https://github.com/syntax-tree/unist-util-is#readme
源码仓库：https://github.com/syntax-tree/unist-util-is

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：31.5 kB，9 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2015-09-02T10:28:52.097Z
- 最新版本发布：2025-10-16T18:01:47.398Z
- 近 30 天下载量：297205607（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：20（近 12 个月 0 个）

## 中文简介

用于检查节点是否通过测试的无源工具

## 安装

```sh
npm install unist-util-is
```

也可以使用：`pnpm add unist-util-is`、`yarn add unist-util-is`、`bun add unist-util-is`

## README

# 非单式用法

[![Build](https://github.com/syntax-tree/unist-util-is/workflows/main/badge.svg)](https://github.com/syntax-tree/unist-util-is/actions) [![Coverage](https://img.shields.io/codecov/c/github/syntax-tree/unist-util-is.svg)](https://codecov.io/github/syntax-tree/unist-util-is) [![Downloads](https://img.shields.io/npm/dm/unist-util-is.svg)](https://www.npmjs.com/package/unist-util-is) [![Size](https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-util-is)](https://bundlejs.com/?q=unist-util-is) [![Sponsors](https://opencollective.com/unified/sponsors/badge.svg)](https://opencollective.com/unified) [![Backers](https://opencollective.com/unified/backers/badge.svg)](https://opencollective.com/unified) [![Chat](https://img.shields.io/badge/chat-discussions-success.svg)](https://github.com/syntax-tree/unist/discussions)

[无符号](https://github.com/syntax-tree/unist) 用于检查节点是否通过测试.

## 目录

-   [这是什么?](https://npmcn.com/package/unist-util-is/#readme-what-is-this)
-   [我什么时候用这个?](https://npmcn.com/package/unist-util-is/#readme-when-should-i-use-this)
-   [安装](https://npmcn.com/package/unist-util-is/#readme-install)
-   [使用](https://npmcn.com/package/unist-util-is/#readme-use)
-   [API 密码](https://npmcn.com/package/unist-util-is/#readme-api)
    -   [`is(node[, test[, index, parent[, context]]])`](https://npmcn.com/package/unist-util-is/#readme-isnode-test-index-parent-context)
    -   [`convert(test)`](https://npmcn.com/package/unist-util-is/#readme-converttest)
    -   [`Check`](https://npmcn.com/package/unist-util-is/#readme-check)
    -   [`Test`](https://npmcn.com/package/unist-util-is/#readme-test)
    -   [`TestFunction`](https://npmcn.com/package/unist-util-is/#readme-testfunction)
-   [实例](https://npmcn.com/package/unist-util-is/#readme-examples)
    -   [实例 `convert`](https://npmcn.com/package/unist-util-is/#readme-example-of-convert)
-   [类型](https://npmcn.com/package/unist-util-is/#readme-types)
-   [兼容性](https://npmcn.com/package/unist-util-is/#readme-compatibility)
-   [相关](https://npmcn.com/package/unist-util-is/#readme-related)
-   [贡献](https://npmcn.com/package/unist-util-is/#readme-contribute)
-   [许可证](https://npmcn.com/package/unist-util-is/#readme-license)

## 这是什么?

这个软件包是一个小的功能,可以检查一个节点是某个节点.

## 我什么时候用这个?

如果您发现自己重复检查什么的代码, 请使用这个小工具 节点是.

一个类似的包裹, [`hast-util-is-element`](https://github.com/syntax-tree/hast-util-is-element),工作在匆忙中 要素.

对于更先进的测试, [`unist-util-select`](https://github.com/syntax-tree/unist-util-select) 可用于 以匹配 CSS 选择器.

## 安装

这个软件包是 [仅限无害环境管理](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)。 。 。 . 在Node.js(第16版+)中,安装为 [无](https://docs.npmjs.com/cli/install)数字 :

```sh
npm install unist-util-is
```

在德诺与 [`esm.sh`](https://esm.sh/)数字 :

```js
import {is} from 'https://esm.sh/unist-util-is@6'
```

在浏览器中 [`esm.sh`](https://esm.sh/)数字 :

```html
<script type="module">
  import {is} from 'https://esm.sh/unist-util-is@6?bundle'
</script>
```

## 使用

```js
import {is} from 'unist-util-is'

const node = {type: 'strong'}
const parent = {type: 'paragraph', children: [node]}

is() // => false
is({children: []}) // => false
is(node) // => true
is(node, 'strong') // => true
is(node, 'emphasis') // => false

is(node, node) // => true
is(parent, {type: 'paragraph'}) // => true
is(parent, {type: 'strong'}) // => false

is(node, test) // => false
is(node, test, 4, parent) // => false
is(node, test, 5, parent) // => true

function test(node, n) {
  return n === 5
}
```

## API 密码

此软件包导出标识符 [`convert`](https://npmcn.com/package/unist-util-is/#readme-converttest) 财务报告和审定财务报表 [`is`](https://npmcn.com/package/unist-util-is/#readme-isnode-test-index-parent-context)。 。 。 . 没有默认导出 .

### `is(node[, test[, index, parent[, context]]])`

检查是否 `node` 是一个 `Node` 以及它是否通过 给定的测试.

###### 参数

-   `node` (单位:千美元)`unknown`,可选) 有事情要查 典型的 [`Node`](https://github.com/syntax-tree/unist#node)
-   `test` (单位:千美元)[`Test`](https://npmcn.com/package/unist-util-is/#readme-test),可选) - 特定要素的测试
-   `index` (单位:千美元)`number`,可选) - 节点在母节点的地位
-   `parent` (单位:千美元)[`Node`](https://github.com/syntax-tree/unist#node),可选) - 节点的家长
-   `context` (单位:千美元)`unknown`,可选) - 上下文对象(`this`打电话 `test` 与

###### 回返

是否为 `node` 是一个 [`Node`](https://github.com/syntax-tree/unist#node) 并且通过测试(`boolean`) (中文(简体) ).

###### 掷出

当错误时 `test`, (中文(简体) ), `index`,或 `parent` 给出. 当 `node` 不是一个节点.

### `convert(test)`

从测试中生成支票 .

如果您要测试许多节点, 例如创建一个节点时, 有用 用于通过兼容的测试.

创建的函数要快一点, 因为它只期望有效的输入 : (单位:千美元) `node`, (中文(简体) ), `index`,以及 `parent`。 。 。 .

###### 参数

-   `test` (单位:千美元)[`Test`](https://npmcn.com/package/unist-util-is/#readme-test),可选) - 特定节点的测试

###### 回返

支票([`Check`](https://npmcn.com/package/unist-util-is/#readme-check)) (中文(简体) ).

### `Check`

选中任意值为节点( TypeScript 类型) .

###### 参数

-   `this` (单位:千美元)`unknown`,可选) - 上下文对象(`this`打电话 `test` 与
-   `node` (单位:千美元)`unknown`(中文(简体) ) - 任何东西(典型的节点)
-   `index` (单位:千美元)`number`,可选) - 节点在母节点的地位
-   `parent` (单位:千美元)[`Node`](https://github.com/syntax-tree/unist#node),可选) - 节点的家长

###### 回返

这是否是一个节点和通过一个测试(`boolean`) (中文(简体) ).

### `Test`

检查任意节点( TypeScript 类型) .

###### 类型

```ts
type Test =
  | Array<Record<string, unknown> | TestFunction | string>
  | Record<string, unknown>
  | TestFunction
  | string
  | null
  | undefined
```

检查给定的东西是一个节点,然后:

-   何时 `string`,检查节点是否有该标签名称
-   何时 `function`,参见 [`TestFunction`](https://npmcn.com/package/unist-util-is/#readme-testfunction)
-   何时 `object`,检查测试中的所有密钥都在节点中,并且它们有 (严格)等值
-   何时 `Array`,检查是否有一个子测试通过

### `TestFunction`

检查节点是否通过测试(TypeScript类型).

###### 参数

-   `node` (单位:千美元)[`Node`](https://github.com/syntax-tree/unist#node)(中文(简体) ) 一个节点
-   `index` (单位:千美元)`number` 或者说 `undefined`(中文(简体) ) - 节点在母节点的地位
-   `parent` (单位:千美元)[`Node`](https://github.com/syntax-tree/unist#node) 或者说 `undefined`(中文(简体) ) - 节点的家长

###### 回返

此节点是否通过测试( W)`boolean`,可选).

## 实例

### 实例 `convert`

```js
import {u} from 'unist-builder'
import {convert} from 'unist-util-is'

const test = convert('leaf')

const tree = u('tree', [
  u('node', [u('leaf', '1')]),
  u('leaf', '2'),
  u('node', [u('leaf', '3'), u('leaf', '4')]),
  u('leaf', '5')
])

const leafs = tree.children.filter(function (child, index) {
  return test(child, index, tree)
})

console.log(leafs)
```

仪:

```js
[{type: 'leaf', value: '2'}, {type: 'leaf', value: '5'}]
```

## 类型

此软件包已全部输入 [类型脚本](https://www.typescriptlang.org/)。 。 。 . 出口其他类型 [`Check`](https://npmcn.com/package/unist-util-is/#readme-check), (中文(简体) ), [`Test`](https://npmcn.com/package/unist-util-is/#readme-test), (中文(简体) ), [`TestFunction`](https://npmcn.com/package/unist-util-is/#readme-testfunction)。 。 。 .

## 兼容性

由统一集体管理的项目与维持的项目兼容 (原始内容存档于2018-10-21). Executive of Node.js.

当我们剪下新的主要版本时,我们放弃了对未维护版本的支持 节点. 这意味着我们试图保持目前的释放线, `unist-util-is@^6`, (中文(简体) ), 与 Node.js 16相兼容.

## 相关

-   [`unist-util-find-after`](https://github.com/syntax-tree/unist-util-find-after) 在另一个节点之后找到一个节点
-   [`unist-util-find-before`](https://github.com/syntax-tree/unist-util-find-before) 在另一个节点之前找到节点
-   [`unist-util-find-all-after`](https://github.com/syntax-tree/unist-util-find-all-after) ——在另一个节点后找到所有节点
-   [`unist-util-find-all-before`](https://github.com/syntax-tree/unist-util-find-all-before) 在另一个节点之前找到所有节点
-   [`unist-util-find-all-between`](https://github.com/mrzmmr/unist-util-find-all-between) ——在两个节点之间找到所有节点
-   [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter) ——创建有节点的新树,通过检查
-   [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove) ——从树上去掉节点

## 贡献

见 [`contributing.md`](https://github.com/syntax-tree/.github/blob/main/contributing.md) 内 [`syntax-tree/.github`](https://github.com/syntax-tree/.github) 页:1 开始的方法. 见 [`support.md`](https://github.com/syntax-tree/.github/blob/main/support.md) 为了寻求帮助.

这个项目有一个 [行为守则](https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md)。 。 。 . 通过与这个寄存器,组织或社区的互动,你同意 遵守它的规定.

## 许可证

[麻省理工学院](https://npmcn.com/package/unist-util-is/license) © [Titus 虫类](https://wooorm.com/)

## 常见问题

### unist-util-is 是什么？

用于检查节点是否通过测试的无源工具

### 如何安装 unist-util-is？

在项目目录运行 npm install unist-util-is，或使用 pnpm add / yarn add / bun add。

### unist-util-is 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### unist-util-is 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### unist-util-is 可以商用吗？

unist-util-is 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### unist-util-is 的下载量有多少？

根据 npm 官方下载统计，unist-util-is 在 2026/10/05 – 2026/11/03 这 30 天内被下载 297,205,607 次，日均约 9,906,854 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### unist-util-is 还在维护吗？

最新版本 6\.0\.1 发布于 2025/10/17（1\.1 年前）；至今共发布 20 个版本，其中过去 12 个月发布了 0 个；首次发布于 2015/09/02。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

