|
| 1 | +# unist-util-filter [![Build Status][build-badge]][build-page] [![Coverage Status][coverage-badge]][coverage-page] |
| 2 | + |
| 3 | +Create a new [unist][] tree with all nodes that pass the given test. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```sh |
| 8 | +npm install unist-util-filter |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +```js |
| 14 | +var filter = require('unist-util-filter'); |
| 15 | + |
| 16 | +var tree = { |
| 17 | + type: 'root', |
| 18 | + children: [ |
| 19 | + {type: 'leaf', value: '1'}, |
| 20 | + { |
| 21 | + type: 'node', |
| 22 | + children: [ |
| 23 | + {type: 'leaf', value: '2'}, |
| 24 | + {type: 'node', children: [{type: 'leaf', value: '3'}]} |
| 25 | + ] |
| 26 | + }, |
| 27 | + {type: 'leaf', value: '4'} |
| 28 | + ] |
| 29 | +} |
| 30 | + |
| 31 | +console.log(filter(tree, node => node.type != 'leaf' || node.value % 2 == 0)) |
| 32 | +``` |
| 33 | + |
| 34 | +Yields: |
| 35 | + |
| 36 | +```js |
| 37 | +{ |
| 38 | + type: 'root', |
| 39 | + children: [ |
| 40 | + {type: 'node', children: [{type: 'leaf', value: '2'}]}, |
| 41 | + {type: 'leaf', value: '4'} |
| 42 | + ] |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## API |
| 47 | + |
| 48 | +### `filter(tree, [opts], test)` |
| 49 | + |
| 50 | +Creates a copy of `tree` consisting of all nodes that pass `test`. |
| 51 | +The tree is filtered in [preorder][]. |
| 52 | + |
| 53 | +###### Parameters |
| 54 | + |
| 55 | +* `tree` ([`Node?`][node]) |
| 56 | + — Tree to filter |
| 57 | +* `opts.cascade` (`boolean`, default: `true`) |
| 58 | + — Whether to drop parent nodes if they had children, but all their |
| 59 | + children were filtered out |
| 60 | +* `test` |
| 61 | + — See [`unist-util-is`][is] for details |
| 62 | + |
| 63 | +###### Returns |
| 64 | + |
| 65 | +A new tree ([`Node?`][node]) with nodes for which `test` returned `true`. |
| 66 | +`null` is returned if `tree` itself didn’t pass the test, or is cascaded away. |
| 67 | + |
| 68 | +## Contribute |
| 69 | + |
| 70 | +See [`contributing.md` in `syntax-tree/unist`][contributing] for ways to get |
| 71 | +started. |
| 72 | + |
| 73 | +This organisation has a [Code of Conduct][coc]. By interacting with this |
| 74 | +repository, organisation, or community you agree to abide by its terms. |
| 75 | + |
| 76 | +## License |
| 77 | + |
| 78 | +[MIT][] © Eugene Sharygin |
| 79 | + |
| 80 | +[mit]: LICENSE |
| 81 | + |
| 82 | +[unist]: https://github.com/syntax-tree/unist |
| 83 | + |
| 84 | +[node]: https://github.com/syntax-tree/unist#node |
| 85 | + |
| 86 | +[is]: https://github.com/syntax-tree/unist-util-is |
| 87 | + |
| 88 | +[preorder]: https://en.wikipedia.org/wiki/Tree_traversal |
| 89 | + |
| 90 | +[build-page]: https://travis-ci.org/syntax-tree/unist-util-filter |
| 91 | + |
| 92 | +[build-badge]: https://travis-ci.org/syntax-tree/unist-util-filter.svg?branch=master |
| 93 | + |
| 94 | +[coverage-page]: https://codecov.io/github/syntax-tree/unist-util-filter?branch=master |
| 95 | + |
| 96 | +[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-filter.svg?branch=master |
| 97 | + |
| 98 | +[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md |
| 99 | + |
| 100 | +[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md |
0 commit comments