|
1 | 1 | 'use strict' |
2 | 2 |
|
| 3 | +var convert = require('./convert') |
| 4 | + |
3 | 5 | module.exports = is |
4 | 6 |
|
| 7 | +is.convert = convert |
| 8 | + |
5 | 9 | // Assert if `test` passes for `node`. When a `parent` node is known the |
6 | 10 | // `index` of node. |
7 | 11 | // eslint-disable-next-line max-params |
@@ -31,87 +35,3 @@ function is(test, node, index, parent, context) { |
31 | 35 |
|
32 | 36 | return Boolean(check.call(context, node, index, parent)) |
33 | 37 | } |
34 | | - |
35 | | -function convert(test) { |
36 | | - if (typeof test === 'string') { |
37 | | - return typeFactory(test) |
38 | | - } |
39 | | - |
40 | | - if (test === null || test === undefined) { |
41 | | - return ok |
42 | | - } |
43 | | - |
44 | | - if (typeof test === 'object') { |
45 | | - return ('length' in test ? anyFactory : matchesFactory)(test) |
46 | | - } |
47 | | - |
48 | | - if (typeof test === 'function') { |
49 | | - return test |
50 | | - } |
51 | | - |
52 | | - throw new Error('Expected function, string, or object as test') |
53 | | -} |
54 | | - |
55 | | -function convertAll(tests) { |
56 | | - var results = [] |
57 | | - var length = tests.length |
58 | | - var index = -1 |
59 | | - |
60 | | - while (++index < length) { |
61 | | - results[index] = convert(tests[index]) |
62 | | - } |
63 | | - |
64 | | - return results |
65 | | -} |
66 | | - |
67 | | -// Utility assert each property in `test` is represented in `node`, and each |
68 | | -// values are strictly equal. |
69 | | -function matchesFactory(test) { |
70 | | - return matches |
71 | | - |
72 | | - function matches(node) { |
73 | | - var key |
74 | | - |
75 | | - for (key in test) { |
76 | | - if (node[key] !== test[key]) { |
77 | | - return false |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - return true |
82 | | - } |
83 | | -} |
84 | | - |
85 | | -function anyFactory(tests) { |
86 | | - var checks = convertAll(tests) |
87 | | - var length = checks.length |
88 | | - |
89 | | - return matches |
90 | | - |
91 | | - function matches() { |
92 | | - var index = -1 |
93 | | - |
94 | | - while (++index < length) { |
95 | | - if (checks[index].apply(this, arguments)) { |
96 | | - return true |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - return false |
101 | | - } |
102 | | -} |
103 | | - |
104 | | -// Utility to convert a string into a function which checks a given node’s type |
105 | | -// for said string. |
106 | | -function typeFactory(test) { |
107 | | - return type |
108 | | - |
109 | | - function type(node) { |
110 | | - return Boolean(node && node.type === test) |
111 | | - } |
112 | | -} |
113 | | - |
114 | | -// Utility to return true. |
115 | | -function ok() { |
116 | | - return true |
117 | | -} |
0 commit comments