|
1 |
| -[](https://npmjs.com/package/unist-util-remove) |
| 1 | +# unist-util-remove [![Build Status][build-badge]][build-page] [![Coverage Status][coverage-badge]][coverage-page] |
2 | 2 |
|
3 |
| -# unist-util-remove |
| 3 | +Modify the given [unist][] tree to remove all nodes that pass the given test. |
4 | 4 |
|
5 |
| -[![Build Status][travis-badge]][travis] [![Dependency Status][david-badge]][david] |
6 |
| - |
7 |
| -Remove one or more nodes from [Unist] tree, mutating it. |
8 |
| - |
9 |
| -[unist]: https://github.com/wooorm/unist |
10 |
| -[is]: https://github.com/syntax-tree/unist-util-is#api |
| 5 | +## Install |
11 | 6 |
|
12 |
| -[travis]: https://travis-ci.org/eush77/unist-util-remove |
13 |
| -[travis-badge]: https://travis-ci.org/eush77/unist-util-remove.svg?branch=master |
14 |
| -[david]: https://david-dm.org/eush77/unist-util-remove |
15 |
| -[david-badge]: https://david-dm.org/eush77/unist-util-remove.png |
| 7 | +```sh |
| 8 | +npm install unist-util-remove |
| 9 | +``` |
16 | 10 |
|
17 |
| -## Example |
| 11 | +## Usage |
18 | 12 |
|
19 | 13 | ```js
|
20 | 14 | var u = require('unist-builder');
|
@@ -45,39 +39,59 @@ remove(ast, 'leaf')
|
45 | 39 | // └─ other: "4"
|
46 | 40 | ```
|
47 | 41 |
|
48 |
| -If the root node gets removed, the entire tree is destroyed and `remove` returns `null`. That's the only case in which `remove` doesn't return the original root node. |
49 |
| - |
50 |
| -```js |
51 |
| -remove(ast, ast) |
52 |
| -//=> null |
53 |
| -``` |
54 |
| - |
55 | 42 | ## API
|
56 | 43 |
|
57 |
| -### `remove(ast, [opts], predicate)` |
| 44 | +### `remove(tree, [opts], test)` |
58 | 45 |
|
59 |
| -- `ast` — [Unist] tree. |
60 |
| -- `predicate` — any `test` as given to [`unist-util-is`][is]. |
| 46 | +Mutate `tree` to remove all nodes that pass `test`. |
| 47 | +The tree is filtered in [preorder][]. |
61 | 48 |
|
62 |
| -Iterates over `ast` in preorder traversal and removes all nodes matching `predicate` from `ast`. Returns a modified [Unist] tree. |
| 49 | +###### Parameters |
63 | 50 |
|
64 |
| -> Note: `unist-util-is` used to remove a node when given as `test`. |
65 |
| -> This no longer works. To migrate, pass a function as a predicate, like so: |
66 |
| -> `remove(ast, function (node) { return node === predicate; })`. |
| 51 | +* `tree` ([`Node?`][node]) |
| 52 | + — Tree to filter |
| 53 | +* `opts.cascade` (`boolean`, default: `true`) |
| 54 | + — Whether to drop parent nodes if they had children, but all their |
| 55 | + children were filtered out |
| 56 | +* `test` |
| 57 | + — See [`unist-util-is`][is] for details |
67 | 58 |
|
68 |
| -##### `opts.cascade` |
| 59 | +###### Returns |
69 | 60 |
|
70 |
| -Type: `Boolean`<br> |
71 |
| -Default: `true` |
| 61 | +The given `tree` ([`Node?`][node]) with nodes for which `test` returned `true` |
| 62 | +removed. `null` is returned if `tree` itself didn’t pass the test, or is |
| 63 | +cascaded away. |
72 | 64 |
|
73 |
| -If `true`, removing of the last child triggers removal of its parent node. |
| 65 | +## Contribute |
74 | 66 |
|
75 |
| -## Install |
| 67 | +See [`contributing.md` in `syntax-tree/unist`][contributing] for ways to get |
| 68 | +started. |
76 | 69 |
|
77 |
| -``` |
78 |
| -npm install unist-util-remove |
79 |
| -``` |
| 70 | +This organisation has a [Code of Conduct][coc]. By interacting with this |
| 71 | +repository, organisation, or community you agree to abide by its terms. |
80 | 72 |
|
81 | 73 | ## License
|
82 | 74 |
|
83 |
| -MIT |
| 75 | +[MIT][] © Eugene Sharygin |
| 76 | + |
| 77 | +[mit]: LICENSE |
| 78 | + |
| 79 | +[unist]: https://github.com/syntax-tree/unist |
| 80 | + |
| 81 | +[node]: https://github.com/syntax-tree/unist#node |
| 82 | + |
| 83 | +[is]: https://github.com/syntax-tree/unist-util-is |
| 84 | + |
| 85 | +[preorder]: https://en.wikipedia.org/wiki/Tree_traversal |
| 86 | + |
| 87 | +[build-page]: https://travis-ci.org/syntax-tree/unist-util-remove |
| 88 | + |
| 89 | +[build-badge]: https://travis-ci.org/syntax-tree/unist-util-remove.svg?branch=master |
| 90 | + |
| 91 | +[coverage-page]: https://codecov.io/github/syntax-tree/unist-util-remove?branch=master |
| 92 | + |
| 93 | +[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-remove.svg?branch=master |
| 94 | + |
| 95 | +[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md |
| 96 | + |
| 97 | +[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md |
0 commit comments