Skip to content

Commit feee86b

Browse files
committed
Add support for passing arrays
1 parent def7f2e commit feee86b

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

index.js

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,7 @@ module.exports = is;
1010
function is(test, node, index, parent, context) {
1111
var hasParent = parent !== null && parent !== undefined;
1212
var hasIndex = index !== null && index !== undefined;
13-
14-
if (typeof test === 'string') {
15-
test = typeFactory(test);
16-
} else if (test === null || test === undefined) {
17-
test = first;
18-
} else if (typeof test === 'object') {
19-
test = matchesFactory(test);
20-
} else if (typeof test !== 'function') {
21-
throw new Error('Expected function, string, or object as test');
22-
}
13+
var check = convert(test);
2314

2415
if (
2516
hasIndex &&
@@ -40,7 +31,39 @@ function is(test, node, index, parent, context) {
4031
throw new Error('Expected both parent and index');
4132
}
4233

43-
return Boolean(test.call(context, node, index, parent));
34+
return Boolean(check.call(context, node, index, parent));
35+
}
36+
37+
function convert(test) {
38+
if (typeof test === 'string') {
39+
return typeFactory(test);
40+
}
41+
42+
if (test === null || test === undefined) {
43+
return ok;
44+
}
45+
46+
if (typeof test === 'object') {
47+
return ('length' in test ? anyFactory : matchesFactory)(test);
48+
}
49+
50+
if (typeof test === 'function') {
51+
return test;
52+
}
53+
54+
throw new Error('Expected function, string, or object as test');
55+
}
56+
57+
function convertAll(tests) {
58+
var results = [];
59+
var length = tests.length;
60+
var index = -1;
61+
62+
while (++index < length) {
63+
results[index] = convert(tests[index]);
64+
}
65+
66+
return results;
4467
}
4568

4669
/* Utility assert each property in `test` is represented
@@ -61,6 +84,25 @@ function matchesFactory(test) {
6184
}
6285
}
6386

87+
function anyFactory(tests) {
88+
var checks = convertAll(tests);
89+
var length = checks.length;
90+
91+
return matches;
92+
93+
function matches() {
94+
var index = -1;
95+
96+
while (++index < length) {
97+
if (checks[index].apply(this, arguments)) {
98+
return true;
99+
}
100+
}
101+
102+
return false;
103+
}
104+
}
105+
64106
/* Utility to convert a string into a function which checks
65107
* a given node’s type for said string. */
66108
function typeFactory(test) {
@@ -72,6 +114,6 @@ function typeFactory(test) {
72114
}
73115

74116
/* Utility to return true. */
75-
function first() {
117+
function ok() {
76118
return true;
77119
}

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ is(test, node, 5, parent); // true
4141

4242
###### Parameters
4343

44-
* `test` ([`Function`][test], `string`, or `Node`, optional)
44+
* `test` ([`Function`][test], `string`, `Object`, or `Array.<Test>`, optional)
4545
— When not given, checks if `node` is a [`Node`][node].
4646
When `string`, works like passing `function (node) {return
4747
node.type === test}`.
48+
When `array`, checks any one of the subtests pass.
4849
When `object`, checks that all keys in `test` are in `node`,
4950
and that they have (strictly) equal values.
5051
* `node` ([`Node`][node]) — Node to check. `false` is returned;

test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,24 @@ test('unist-util-is', function (t) {
112112
is(test, node, 5, parent, context);
113113
});
114114

115+
t.ok(is(['strong', 'emphasis'], node), 'should match arrays (#1)');
116+
t.notok(is(['b', 'i'], node), 'should match arrays (#2)');
117+
118+
t.test('should match arrays (#3)', function (st) {
119+
var context = {foo: 'bar'};
120+
121+
st.plan(5);
122+
123+
st.ok(is([test, 'strong'], node, 5, parent, context));
124+
125+
function test(a, b, c) {
126+
st.equal(this, context);
127+
st.equal(a, node);
128+
st.equal(b, 5);
129+
st.equal(c, parent);
130+
return false;
131+
}
132+
});
133+
115134
t.end();
116135
});

0 commit comments

Comments
 (0)