# unist-util-remove

> unist utility to remove nodes from a tree

Latest version **4.0.0** (published 2023-07-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install unist-util-remove
pnpm add unist-util-remove
yarn add unist-util-remove
bun add unist-util-remove
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2023-07-07 |
| First published | 2016-01-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 3 |
| Unpacked size | 12.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 25 |
| Author | Eugene Sharygin |
| Maintainers | wooorm, kmck |
| Keywords | unist, unist-util, util, utility, ast, tree, node, cascade, delete, mutable, remove, squeeze, strip |

## Links

- npm: https://www.npmjs.com/package/unist-util-remove
- Repository: https://github.com/syntax-tree/unist-util-remove
- Homepage: https://github.com/syntax-tree/unist-util-remove#readme
- Issues: https://github.com/syntax-tree/unist-util-remove/issues
- Funding: https://opencollective.com/unified
- npm.io page: https://npm.io/package/unist-util-remove

## Dependencies (3)

- [@types/unist](https://npm.io/package/@types/unist.md) ^3.0.0
- [unist-util-is](https://npm.io/package/unist-util-is.md) ^6.0.0
- [unist-util-visit-parents](https://npm.io/package/unist-util-visit-parents.md) ^6.0.0

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2023-07-07
- 3.1.1 — 2023-01-23
- 3.1.0 — 2021-07-30
- 3.0.0 — 2021-04-19
- 2.1.0 — 2021-04-02
- 2.0.1 — 2020-11-01
- 2.0.0 — 2020-02-10
- 1.0.3 — 2019-05-31
- 1.0.2 — 2019-05-21
- 1.0.1 — 2018-06-04
- 1.0.0 — 2017-05-16
- 0.2.1 — 2016-01-23
- 0.2.0 — 2016-01-13
- 0.1.0 — 2016-01-05

## README

# unist-util-remove

[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

[unist][] utility to remove all nodes that pass a test from the tree.

## Contents

*   [What is this?](#what-is-this)
*   [When should I use this?](#when-should-i-use-this)
*   [Install](#install)
*   [Use](#use)
*   [API](#api)
    *   [`remove(tree[, options], test)`](#removetree-options-test)
    *   [`Options`](#options)
*   [Types](#types)
*   [Compatibility](#compatibility)
*   [Related](#related)
*   [Contribute](#contribute)
*   [License](#license)

## What is this?

This is a small utility that helps you clean a tree by removing some stuff.

## When should I use this?

You can use this utility to remove things from a tree.
This utility is very similar to [`unist-util-filter`][unist-util-filter], which
creates a new tree.
Modifying a tree like this utility `unist-util-remove` does is much faster on
larger documents though.

You can also walk the tree with [`unist-util-visit`][unist-util-visit] to remove
nodes.
To create trees, use [`unist-builder`][unist-builder].

## Install

This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:

```sh
npm install unist-util-remove
```

In Deno with [`esm.sh`][esmsh]:

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

In browsers with [`esm.sh`][esmsh]:

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

## Use

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

const tree = u('root', [
  u('leaf', '1'),
  u('parent', [
    u('leaf', '2'),
    u('parent', [u('leaf', '3'), u('other', '4')]),
    u('parent', [u('leaf', '5')])
  ]),
  u('leaf', '6')
])

// Remove all nodes of type `leaf`.
remove(tree, 'leaf')

console.dir(tree, {depth: undefined})
```

Yields:

```js
{
  type: 'root',
  children: [
    {
      type: 'parent',
      children: [{type: 'parent', children: [{type: 'other', value: '4'}]}]
    }
  ]
}
```

> 👉 **Note**: the parent of leaf `5` is also removed, `options.cascade` can
> change that.

## API

This package exports the identifier [`remove`][api-remove].
There is no default export.

### `remove(tree[, options], test)`

Change the given `tree` by removing all nodes that pass `test`.

`tree` itself is never tested.
The tree is walked in *[preorder][]* (NLR), visiting the node itself, then its
head, etc.

###### Parameters

*   `tree` ([`Node`][node])
    — tree to change
*   `options` ([`Options`][api-options], optional)
    — configuration
*   `test` ([`Test`][test], optional)
    — `unist-util-is` compatible test

###### Returns

Nothing (`undefined`).

### `Options`

Configuration (TypeScript type).

###### Fields

*   `cascade` (`boolean`, default: `true`)
    — whether to drop parent nodes if they had children, but all their children
    were filtered out

## Types

This package is fully typed with [TypeScript][].
It exports the additional type [`Options`][api-options].

## Compatibility

Projects maintained by the unified collective are compatible with maintained
versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, `unist-util-remove@^4`,
compatible with Node.js 16.

## Related

*   [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter)
    — create a new tree with all nodes that pass the given function
*   [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
    — create a new tree by expanding a node into many
*   [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
    — create a new tree by mapping nodes
*   [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
    — select nodes with CSS-like selectors
*   [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
    — walk the tree
*   [`unist-builder`](https://github.com/syntax-tree/unist-builder)
    — create trees

## Contribute

See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
ways to get started.
See [`support.md`][support] for ways to get help.

This project has a [Code of Conduct][coc].
By interacting with this repository, organisation, or community you agree to
abide by its terms.

## License

[MIT][license] © Eugene Sharygin

<!-- Definitions -->

[build-badge]: https://github.com/syntax-tree/unist-util-remove/workflows/main/badge.svg

[build]: https://github.com/syntax-tree/unist-util-remove/actions

[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-remove.svg

[coverage]: https://codecov.io/github/syntax-tree/unist-util-remove

[downloads-badge]: https://img.shields.io/npm/dm/unist-util-remove.svg

[downloads]: https://www.npmjs.com/package/unist-util-remove

[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=unist-util-remove

[size]: https://bundlejs.com/?q=unist-util-remove

[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg

[backers-badge]: https://opencollective.com/unified/backers/badge.svg

[collective]: https://opencollective.com/unified

[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg

[chat]: https://github.com/syntax-tree/unist/discussions

[npm]: https://docs.npmjs.com/cli/install

[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

[esmsh]: https://esm.sh

[typescript]: https://www.typescriptlang.org

[license]: license

[health]: https://github.com/syntax-tree/.github

[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md

[support]: https://github.com/syntax-tree/.github/blob/main/support.md

[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md

[unist]: https://github.com/syntax-tree/unist

[node]: https://github.com/syntax-tree/unist#node

[preorder]: https://github.com/syntax-tree/unist#preorder

[test]: https://github.com/syntax-tree/unist-util-is#test

[unist-util-filter]: https://github.com/syntax-tree/unist-util-filter

[unist-util-visit]: https://github.com/syntax-tree/unist-util-visit

[unist-builder]: https://github.com/syntax-tree/unist-builder

[api-remove]: #removetree-options-test

[api-options]: #options

---
_Source: https://npm.io/package/unist-util-remove · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
