Skip to content

Commit ccaa2df

Browse files
committed
Update example
1 parent dbc7fa3 commit ccaa2df

File tree

2 files changed

+41
-54
lines changed

2 files changed

+41
-54
lines changed

README.md

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,32 @@ Remove one or more nodes from [Unist] tree, mutating it.
1717
## Example
1818

1919
```js
20+
var u = require('unist-builder');
2021
var remove = require('unist-util-remove');
21-
22-
ast
23-
//=> {
24-
// "type": "root",
25-
// "children": [
26-
// {
27-
// "type": "leaf",
28-
// "value": "1"
29-
// },
30-
// {
31-
// "type": "node",
32-
// "children": [
33-
// {
34-
// "type": "leaf",
35-
// "value": "2"
36-
// },
37-
// {
38-
// "type": "node",
39-
// "children": [
40-
// {
41-
// "type": "leaf",
42-
// "value": "3"
43-
// }
44-
// ]
45-
// }
46-
// ]
47-
// },
48-
// {
49-
// "type": "leaf",
50-
// "value": "4"
51-
// }
52-
// ]
53-
// }
54-
55-
// Remove node 2 and its sibling.
56-
remove(ast, [ast.children[1].children[0], ast.children[1].children[1]])
57-
//=> {
58-
// "type": "root",
59-
// "children": [
60-
// {
61-
// "type": "leaf",
62-
// "value": "1"
63-
// },
64-
// {
65-
// "type": "leaf",
66-
// "value": "4"
67-
// }
68-
// ]
69-
// }
22+
var inspect = require('unist-util-inspect');
23+
24+
var ast = u('root', [
25+
u('leaf', 1),
26+
u('node', [
27+
u('leaf', 2),
28+
u('node', [
29+
u('leaf', 3),
30+
u('other', 4)
31+
]),
32+
// this node will be removed as well because of `opts.cascade`.
33+
u('node', [
34+
u('leaf', 5),
35+
])
36+
]),
37+
u('leaf', 6)
38+
]);
39+
40+
// Remove all nodes of type `leaf`.
41+
remove(ast, 'leaf')
42+
//=> root[1]
43+
// └─ node[1]
44+
// └─ node[1]
45+
// └─ other: "4"
7046
```
7147

7248
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.

test/example.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,26 @@ test('example from README', function (t) {
1212
u('node', [
1313
u('leaf', 2),
1414
u('node', [
15-
u('leaf', 3)
15+
u('leaf', 3),
16+
u('other', 4)
17+
]),
18+
u('node', [
19+
u('leaf', 5),
1620
])
1721
]),
18-
u('leaf', 4)
22+
u('leaf', 6)
1923
]);
20-
var one = ast.children[0];
21-
var four = ast.children[2];
2224

23-
t.equal(remove(ast, ast), null);
25+
t.deepEqual(
26+
remove(ast, 'leaf'),
27+
u('root', [
28+
u('node', [
29+
u('node', [
30+
u('other', 4)
31+
])
32+
])
33+
])
34+
);
2435

2536
t.end();
2637
});

0 commit comments

Comments
 (0)