Skip to content

Commit ff3abf8

Browse files
committed
add type and node tests
Use unist-util-is for testing a node.
1 parent 12fb6e5 commit ff3abf8

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Create a new [Unist] tree with all nodes that pass the test implemented by the provided function.
88

99
[unist]: https://github.com/wooorm/unist
10+
[unist-util-is]: https://github.com/wooorm/unist-util-is
1011

1112
[travis]: https://travis-ci.org/eush77/unist-util-filter
1213
[travis-badge]: https://travis-ci.org/eush77/unist-util-filter.svg?branch=master
@@ -78,7 +79,7 @@ filter(ast, (node) => node.type != 'leaf' || node.value % 2 == 0)
7879
### `filter(ast, [opts], predicate, [context])`
7980

8081
- `ast`[Unist] tree.
81-
- `predicate` — Function to test each node. Invoked with arguments `(node, index?, parent?)`. Return `true` to keep the node, `false` otherwise.
82+
- `predicate` — Function invoked with arguments `(node, index?, parent?)` or string (type test) or node (identity test) to test each node. See [unist-util-is] for details. In a function form, return `true` to keep the node, `false` otherwise.
8283
- `context` — Optional. Value to use as `this` when executing `predicate`.
8384

8485
Executes `predicate` for each node in preorder tree traversal. Returns a new tree (or `null`) with nodes for which `predicate` returned `true`.

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
'use strict';
22

3-
var flatmap = require('flatmap');
3+
var flatmap = require('flatmap'),
4+
is = require('unist-util-is');
45

56

67
module.exports = function (ast, opts, predicate, context) {
7-
if (typeof opts == 'function') {
8+
if (arguments.length == 2) {
89
context = predicate;
910
predicate = opts;
1011
opts = {};
1112
}
1213
opts.cascade = opts.cascade || opts.cascade === undefined;
1314

1415
return (function preorder (node, index, parent) {
15-
if (!predicate.call(context, node, index, parent)) {
16+
if (!is(predicate, node, index, parent, context)) {
1617
return null;
1718
}
1819

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"util"
2828
],
2929
"dependencies": {
30-
"flatmap": "0.0.3"
30+
"flatmap": "0.0.3",
31+
"unist-util-is": "^1.0.0"
3132
},
3233
"devDependencies": {
3334
"tape": "^4.4.0",

test/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,25 @@ it('should call iterator in a context passed in as the last arg', function (t) {
121121

122122
t.end();
123123
});
124+
125+
126+
it('should support type and node tests', function (t) {
127+
var ast = u('node', [
128+
u('node', [
129+
u('leaf', 1)
130+
]),
131+
u('leaf', 2)
132+
]);
133+
134+
t.equal(filter(ast, 'node'), null);
135+
136+
t.deepEqual(filter(ast, { cascade: false }, 'node'), u('node', [
137+
u('node', [])
138+
]));
139+
t.equal(filter(ast, { cascade: false }, 'leaf'), null);
140+
141+
t.deepEqual(filter(ast, { cascade: false }, ast), u('node', []));
142+
t.equal(filter(ast, { cascade: false }, ast.children[0]), null);
143+
144+
t.end();
145+
});

0 commit comments

Comments
 (0)