@@ -10,16 +10,7 @@ module.exports = is;
1010function 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. */
66108function 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}
0 commit comments