# unist-util-visit [![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**][unist] utility to visit nodes. ## Install This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): Node 12+ is needed to use it and it must be `import`ed instead of `require`d. [npm][]: ```sh npm install unist-util-visit ``` ## Use ```js import {u} from 'unist-builder' import {visit} from 'unist-util-visit' const tree = u('tree', [ u('leaf', '1'), u('node', [u('leaf', '2')]), u('void'), u('leaf', '3') ]) visit(tree, 'leaf', (node) => { console.log(node) }) ``` Yields: ```js { type: 'leaf', value: '1' } { type: 'leaf', value: '2' } { type: 'leaf', value: '3' } ``` Note: this example also uses `unist-builder`, to run the example ensure both `unist-builder` and `unist-util-visit` are installed: ```sh npm install unist-builder unist-util-visit ``` ## API This package exports the following identifiers: `visit`, `CONTINUE`, `SKIP`, and `EXIT`. There is no default export. ### `visit(tree[, test], visitor[, reverse])` This function works exactly the same as [`unist-util-visit-parents`][vp], but `visitor` has a different signature. #### `next? = visitor(node, index, parent)` Instead of being passed an array of ancestors, `visitor` is called with the `node`’s [`index`][index] and its [`parent`][parent]. The optional return value `next` is documented in [`unist-util-visit-parents`][vp]’s readme. Otherwise the same as [`unist-util-visit-parents`][vp]. ## Related * [`unist-util-visit-parents`][vp] — Like `visit`, but with a stack of parents * [`unist-util-filter`](https://github.com/syntax-tree/unist-util-filter) — Create a new tree with all nodes that pass a test * [`unist-util-map`](https://github.com/syntax-tree/unist-util-map) — Create a new tree with all nodes mapped by a given function * [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap) — Create a new tree by mapping (to an array) with the given function * [`unist-util-remove`](https://github.com/syntax-tree/unist-util-remove) — Remove nodes from a tree that pass a test * [`unist-util-select`](https://github.com/syntax-tree/unist-util-select) — Select nodes with CSS-like selectors ## Contribute See [`contributing.md` in `syntax-tree/.github`][contributing] 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, organization, or community you agree to abide by its terms. ## License [MIT][license] © [Titus Wormer][author] [build-badge]: https://github.com/syntax-tree/unist-util-visit/workflows/main/badge.svg [build]: https://github.com/syntax-tree/unist-util-visit/actions [coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-visit.svg [coverage]: https://codecov.io/github/syntax-tree/unist-util-visit [downloads-badge]: https://img.shields.io/npm/dm/unist-util-visit.svg [downloads]: https://www.npmjs.com/package/unist-util-visit [size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-visit.svg [size]: https://bundlephobia.com/result?p=unist-util-visit [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 [license]: license [author]: https://wooorm.com [contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md [support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md [coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md [unist]: https://github.com/syntax-tree/unist [vp]: https://github.com/syntax-tree/unist-util-visit-parents [index]: https://github.com/syntax-tree/unist#index [parent]: https://github.com/syntax-tree/unist#parent-1