forked from micromatch/is-glob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (120 loc) · 3.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*!
* is-glob <https://github.com/jonschlinkert/is-glob>
*
* Copyright (c) 2014-2017, Jon Schlinkert.
* Released under the MIT License.
*/
var isExtglob = require('is-extglob');
var chars = { '{': '}', '(': ')', '[': ']'};
var strictCheck = function (str) {
if (str[0] === '!') {
return true;
}
var index = 0;
while (index < str.length) {
if (str[index] === '*') {
return true;
}
if (str[index + 1] === '?' && /[\].+)]/.test(str[index])) {
return true;
}
if (str[index] === '[' && str[index + 1] !== ']') {
var closeIndex = str.indexOf(']', index);
if (closeIndex > index) {
var slashIndex = str.indexOf('\\', index);
if (slashIndex === -1 || slashIndex > closeIndex) {
return true;
}
}
}
if (str[index] === '{' && str[index + 1] !== '}') {
closeIndex = str.indexOf('}', index);
if (closeIndex > index) {
slashIndex = str.indexOf('\\', index);
if (slashIndex === -1 || slashIndex > closeIndex) {
return true;
}
}
}
if (str[index] === '(' && str[index + 1] === '?' && /[:!=]/.test(str[index + 2]) && str[index + 3] !== ')') {
closeIndex = str.indexOf(')', index);
if (closeIndex > index) {
slashIndex = str.indexOf('\\', index);
if (slashIndex === -1 || slashIndex > closeIndex) {
return true;
}
}
}
if (str[index] === '(' && str[index + 1] !== '|') {
var pipeIndex = str.indexOf('|', index);
if (pipeIndex > index && str[pipeIndex + 1] !== ')') {
closeIndex = str.indexOf(')', pipeIndex);
if (closeIndex > pipeIndex) {
slashIndex = str.indexOf('\\', pipeIndex);
if (slashIndex === -1 || slashIndex > closeIndex) {
return true;
}
}
}
}
if (str[index] === '\\') {
var open = str[index+1];
index += 2;
var close = chars[open];
if (close) {
var n = str.indexOf(close, index);
if (n !== -1) {
index = n + 1;
}
}
if (str[index] === '!') {
return true;
}
} else {
index++;
}
}
return false;
}
var relaxedCheck = function (str) {
if (str[0] === '!') {
return true;
}
var index = 0;
while (index < str.length) {
if (/[*?{}()[\]]/.test(str[index])) {
return true;
}
if (str[index] === '\\') {
var open = str[index+1];
index += 2;
var close = chars[open];
if (close) {
var n = str.indexOf(close, index);
if (n !== -1) {
index = n + 1;
}
}
if (str[index] === '!') {
return true;
}
} else {
index++;
}
}
return false;
}
module.exports = function isGlob(str, options) {
if (typeof str !== 'string' || str === '') {
return false;
}
if (isExtglob(str)) {
return true;
}
var check = strictCheck;
// optionally relax check
if (options && options.strict === false) {
check = relaxedCheck;
}
return check(str);
}