.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / unist-util-is / convert.js
1 'use strict'
2
3 module.exports = convert
4
5 function convert(test) {
6   if (typeof test === 'string') {
7     return typeFactory(test)
8   }
9
10   if (test === null || test === undefined) {
11     return ok
12   }
13
14   if (typeof test === 'object') {
15     return ('length' in test ? anyFactory : matchesFactory)(test)
16   }
17
18   if (typeof test === 'function') {
19     return test
20   }
21
22   throw new Error('Expected function, string, or object as test')
23 }
24
25 function convertAll(tests) {
26   var results = []
27   var length = tests.length
28   var index = -1
29
30   while (++index < length) {
31     results[index] = convert(tests[index])
32   }
33
34   return results
35 }
36
37 // Utility assert each property in `test` is represented in `node`, and each
38 // values are strictly equal.
39 function matchesFactory(test) {
40   return matches
41
42   function matches(node) {
43     var key
44
45     for (key in test) {
46       if (node[key] !== test[key]) {
47         return false
48       }
49     }
50
51     return true
52   }
53 }
54
55 function anyFactory(tests) {
56   var checks = convertAll(tests)
57   var length = checks.length
58
59   return matches
60
61   function matches() {
62     var index = -1
63
64     while (++index < length) {
65       if (checks[index].apply(this, arguments)) {
66         return true
67       }
68     }
69
70     return false
71   }
72 }
73
74 // Utility to convert a string into a function which checks a given node’s type
75 // for said string.
76 function typeFactory(test) {
77   return type
78
79   function type(node) {
80     return Boolean(node && node.type === test)
81   }
82 }
83
84 // Utility to return true.
85 function ok() {
86   return true
87 }