Skip to content

Commit 6b536d2

Browse files
committed
Always test if given a node
Fix inconsistent handling of sometimes throwing when not given a valid node. Now, this module always checks if given a valid node (object with `type` set to a non-empty `string`), and returns `false` if not given a valid node.
1 parent bf056ba commit 6b536d2

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,21 @@ function is(test, node, index, parent, context) {
9292
throw new Error('Expected function, string, or object as test');
9393
}
9494

95-
if (!node || !node.type) {
96-
throw new Error('Expected node');
97-
}
98-
9995
if (
10096
hasIndex &&
10197
(typeof index !== 'number' || index < 0 || index === Infinity)
10298
) {
10399
throw new Error('Expected positive finite index or child node');
104100
}
105101

106-
if (hasParent && (!parent || !parent.type || !parent.children)) {
102+
if (hasParent && (!is(null, parent) || !parent.children)) {
107103
throw new Error('Expected parent node');
108104
}
109105

106+
if (!node || !node.type || typeof node.type !== 'string') {
107+
return false;
108+
}
109+
110110
if (hasParent !== hasIndex) {
111111
throw new Error('Expected both parent and index');
112112
}

readme.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var parent = {type: 'paragraph', children: [node]};
2222

2323
function test(node, n) { return n === 5 }
2424

25+
is(); // false
26+
is(null, {children: []}); // false
2527
is(null, node); // true
2628
is('strong', node); // true
2729
is('emphasis', node); // false
@@ -42,19 +44,20 @@ is(test, node, 5, parent); // true
4244
###### Parameters
4345

4446
* `test` ([`Function`][test], `string`, or `Node`, optional)
45-
— When not given, returns `true` for the first node.
47+
— When not given, checks if `node` is a [`Node`][node].
4648
When `string`, works like passing `function (node) {return
4749
node.type === test}`.
4850
When `object`, checks that all keys in `test` are in `node`,
4951
and that they have (strictly) equal values.
50-
* `node` ([`Node`][node]) — Node to check;
52+
* `node` ([`Node`][node]) — Node to check. `false` is returned;
5153
* `index` (`number`, optional) — Position of `node` in `parent`;
5254
* `parent` (`Node`, optional) — Parent of `node`;
5355
* `context` (`*`, optiona) — Context object to invoke `test` with.
5456

5557
###### Returns
5658

57-
`boolean` — Whether `test` passed.
59+
`boolean` — Whether `test` passed _and_ `node` is a [`Node`][node] (object
60+
with `type` set to non-empty `string`).
5861

5962
#### `function test(node[, index, parent])`
6063

test.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,27 @@ test('unist-util-is', function (t) {
8585
'should throw `parent` xor `index` are given (#2)'
8686
);
8787

88-
t.throws(
89-
function () {
90-
is();
91-
},
92-
/Expected node/,
93-
'should fail without node'
94-
);
95-
96-
t.equal(is(null, node), true, 'should return true without test');
88+
t.notok(is(), 'should not fail without node');
89+
t.ok(is(null, node), 'should check if given a node (#1)');
90+
t.notok(is(null, {children: []}), 'should check if given a node (#2)');
9791

98-
t.equal(is('strong', node), true, 'should match types (#1)');
99-
t.equal(is('emphasis', node), false, 'should match types (#2)');
92+
t.ok(is('strong', node), 'should match types (#1)');
93+
t.notok(is('emphasis', node), 'should match types (#2)');
10094

101-
t.equal(is(node, node), true, 'should match partially (#1)');
102-
t.equal(is({type: 'strong'}, node), true, 'should match partially (#2)');
103-
t.equal(is({type: 'paragraph'}, parent), true, 'should match partially (#3)');
104-
t.equal(is({type: 'paragraph'}, node), false, 'should match partially (#4)');
95+
t.ok(is(node, node), 'should match partially (#1)');
96+
t.ok(is({type: 'strong'}, node), 'should match partially (#2)');
97+
t.ok(is({type: 'paragraph'}, parent), 'should match partially (#3)');
98+
t.notok(is({type: 'paragraph'}, node), 'should match partially (#4)');
10599

106100
t.test('should accept a test', function (st) {
107101
/** Test. */
108102
function test(node, n) {
109103
return n === 5;
110104
}
111105

112-
st.equal(is(test, node), false);
113-
st.equal(is(test, node, 0, parent), false);
114-
st.equal(is(test, node, 5, parent), true);
106+
t.notok(is(test, node));
107+
t.notok(is(test, node, 0, parent));
108+
st.ok(is(test, node, 5, parent));
115109

116110
st.end();
117111
});

0 commit comments

Comments
 (0)