{"id":13716601,"url":"https://github.com/syntax-tree/unist-util-filter","last_synced_at":"2025-07-12T07:37:50.311Z","repository":{"id":57386447,"uuid":"49193486","full_name":"syntax-tree/unist-util-filter","owner":"syntax-tree","description":"utility to create a new tree with nodes that pass a filter","archived":false,"fork":false,"pushed_at":"2023-08-26T15:48:21.000Z","size":131,"stargazers_count":20,"open_issues_count":0,"forks_count":2,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-07-07T23:19:04.921Z","etag":null,"topics":["filter","syntax-tree","unist","unist-util"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"unifiedjs","open_collective":"unified"}},"created_at":"2016-01-07T09:00:14.000Z","updated_at":"2025-06-03T11:34:23.000Z","dependencies_parsed_at":"2024-06-18T15:35:31.108Z","dependency_job_id":"1a806853-f07e-4a6e-b341-7429496bb2c0","html_url":"https://github.com/syntax-tree/unist-util-filter","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/unist-util-filter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/unist-util-filter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Funist-util-filter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264958203,"owners_count":23689012,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["filter","syntax-tree","unist","unist-util"],"created_at":"2024-08-03T00:01:12.329Z","updated_at":"2025-07-12T07:37:50.255Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# unist-util-filter\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[unist][] utility to create a new tree with only nodes that pass a test.\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`filter(tree[, options][, test])`](#filtertree-options-test)\n    *   [`Options`](#options)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Related](#related)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis is a small utility that helps you clean a tree.\n\n## When should I use this?\n\nYou can use this utility to remove things from a tree.\nThis utility is very similar to [`unist-util-remove`][unist-util-remove], which\nchanges the given tree.\nModifying a tree like that utility does is much faster on larger documents.\n\nYou can also walk the tree with [`unist-util-visit`][unist-util-visit] to remove\nnodes.\nTo create trees, use [`unist-builder`][unist-builder].\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install unist-util-filter\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {filter} from 'https://esm.sh/unist-util-filter@5'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {filter} from 'https://esm.sh/unist-util-filter@5?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport {u} from 'unist-builder'\nimport {filter} from 'unist-util-filter'\n\nconst tree = u('root', [\n  u('leaf', '1'),\n  u('parent', [u('leaf', '2'), u('parent', [u('leaf', '3')])]),\n  u('leaf', '4')\n])\n\nconst newTree = filter(tree, node =\u003e node.type !== 'leaf' || node.value % 2 === 0)\n\nconsole.dir(newTree, {depth: null})\n```\n\nYields:\n\n```js\n{\n  type: 'root',\n  children: [\n    {type: 'parent', children: [{type: 'leaf', value: '2'}]},\n    {type: 'leaf', value: '4'}\n  ]\n}\n```\n\n## API\n\nThis package exports the identifier [`filter`][filter].\nThere is no default export.\n\n### `filter(tree[, options][, test])`\n\nCreate a new `tree` of copies of all nodes that pass `test`.\n\nThe tree is walked in *[preorder][]* (NLR), visiting the node itself, then its\nhead, etc.\n\n###### Parameters\n\n*   `tree` ([`Node`][node])\n    — tree to filter\n*   `options` ([`Options`][options], optional)\n    — configuration\n*   `test` ([`Test`][test], optional)\n    — `unist-util-is` compatible test\n\n###### Returns\n\nNew filtered tree ([`Node`][node] or `undefined`).\n\n`undefined` is returned if `tree` itself didn’t pass the test, or is cascaded\naway.\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n###### Fields\n\n*   `cascade` (`boolean`, default: `true`)\n    — whether to drop parent nodes if they had children, but all their\n    children were filtered out\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional type [`Options`][options].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `unist-util-filter@^5`,\ncompatible with Node.js 16.\n\n## Related\n\n*   [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)\n    — walk the tree\n*   [`unist-util-visit-parents`](https://github.com/syntax-tree/unist-util-visit-parents)\n    — walk the tree with a stack of parents\n*   [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)\n    — create a new tree with all nodes mapped by a given function\n*   [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)\n    — create a new tree by mapping (to an array) by a given function\n*   [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove)\n    — remove nodes from a tree that pass a test\n*   [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)\n    — select nodes with CSS-like selectors\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © Eugene Sharygin\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/unist-util-filter/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/unist-util-filter/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-filter.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/unist-util-filter\n\n[downloads-badge]: https://img.shields.io/npm/dm/unist-util-filter.svg\n\n[downloads]: https://www.npmjs.com/package/unist-util-filter\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=unist-util-filter\n\n[size]: https://bundlejs.com/?q=unist-util-filter\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md\n\n[unist]: https://github.com/syntax-tree/unist\n\n[node]: https://github.com/syntax-tree/unist#node\n\n[preorder]: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/\n\n[unist-util-remove]: https://github.com/syntax-tree/unist-util-remove\n\n[unist-util-visit]: https://github.com/syntax-tree/unist-util-visit\n\n[unist-builder]: https://github.com/syntax-tree/unist-builder\n\n[test]: https://github.com/syntax-tree/unist-util-is#test\n\n[filter]: #filtertree-options-test\n\n[options]: #options\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["unist utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Funist-util-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Funist-util-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Funist-util-filter/lists"}