Skip to content

Commit 9f00159

Browse files
committed
Add partial property tests
This removes the identity test (by passing one node), but adds a new test (when given an object), which tests that all keys in `test` are in the comparator `node`, and that they have strictly equal values. Closes GH-1.
1 parent a02740c commit 9f00159

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,23 @@ function typeFactory(test) {
4343
}
4444

4545
/**
46-
* Utility to convert a node into a function which checks
47-
* a given node for strict equality.
46+
* Utility assert each property in `test` is represented
47+
* in `node`, and each values are strictly equal.
4848
*
49-
* @param {Node} test - Node to test.
49+
* @param {Object} test - Spec.
5050
* @return {is~test} - Tester.
5151
*/
52-
function nodeFactory(test) {
52+
function matchesFactory(test) {
5353
return function (node) {
54-
return node === test;
54+
var key;
55+
56+
for (key in test) {
57+
if (node[key] !== test[key]) {
58+
return false;
59+
}
60+
}
61+
62+
return true;
5563
}
5664
}
5765

@@ -102,12 +110,12 @@ function is(test, node, index, parent, context) {
102110

103111
if (typeof test === 'string') {
104112
test = typeFactory(test);
105-
} else if (test && test.type) {
106-
test = nodeFactory(test);
107113
} else if (test === null || test === undefined) {
108114
test = first;
115+
} else if (typeof test === 'object') {
116+
test = matchesFactory(test);
109117
} else if (typeof test !== 'function') {
110-
throw new Error('Expected function, string, or node as test');
118+
throw new Error('Expected function, string, or object as test');
111119
}
112120

113121
if (!node || !node.type) {

readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ is('strong', node); // true
3939
is('emphasis', node); // false
4040

4141
is(node, node) // true
42-
is(node, {type: 'strong'}) // false
42+
is({type: 'paragraph'}, parent) // true
43+
is({type: 'strong'}, parent) // false
4344

4445
is(test, node); // false
4546
is(test, node, 4, parent); // false
@@ -58,11 +59,11 @@ Utility to check if a node passes a test.
5859
`Node`, optional)
5960
— When not given, return is return `true`.
6061

61-
Passing a `string` is equal to passing
62+
Passing a `string` is like passing
6263
`function (node) {return node.type === test}`.
6364

64-
Passing a `node` is equal to passing
65-
`function (node) {return node === test}`.
65+
Passing an `object` checks that all keys in `test` are in `node`,
66+
and that they have (strictly) equal values.
6667

6768
* `node` (`Node`)
6869
[Node](https://github.com/wooorm/unist#unist-nodes) to test;

test.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('unist-util-is', function () {
4141
it('should throw when `test` is invalid', function () {
4242
throws(function () {
4343
is(false);
44-
}, /Expected function, string, or node as test/);
44+
}, /Expected function, string, or object as test/);
4545
});
4646

4747
it('should throw when `index` is invalid', function () {
@@ -95,11 +95,15 @@ describe('unist-util-is', function () {
9595
equal(is('emphasis', node), false);
9696
});
9797

98-
it('should match nodes', function () {
98+
it('should match partially', function () {
9999
equal(is(node, node), true);
100-
equal(is(node, {
100+
equal(is({
101101
'type': 'strong'
102-
}), false);
102+
}, node), true);
103+
104+
equal(is({
105+
'type': 'paragraph'
106+
}, parent), true);
103107
});
104108

105109
it('should accept a test', function () {

0 commit comments

Comments
 (0)