-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
275 lines (246 loc) · 8.06 KB
/
mod.ts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import * as deno from "deno";
const isWin = deno.platform.os === 'win';
const SEP = isWin ? `\\\\+` : `\\/`;
const SEP_ESC = isWin ? `\\\\` : `/`;
const GLOBSTAR = `((?:[^/]*(?:/|$))*)`;
const WILDCARD = `([^/]*)`;
const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}]*(?:${SEP_ESC}|$))*)`;
const WILDCARD_SEGMENT = `([^${SEP_ESC}]*)`;
/**
* Convert any glob pattern to a JavaScript Regexp object
* @param {String} glob Glob pattern to convert
* @param {Object} opts Configuration object
* @param {Boolean} [opts.extended=false] Support advanced ext globbing
* @param {Boolean} [opts.globstar=false] Support globstar
* @param {Boolean} [opts.strict=true] be laissez faire about mutiple slashes
* @param {Boolean} [opts.filepath=''] Parse as filepath for extra path related features
* @param {String} [opts.flags=''] RegExp globs
* @returns {Object} converted object with string, segments and RegExp object
*/
export function globrex(glob, {extended = false, globstar = false, strict = false, filepath = false, flags = ''} = {}) {
let regex = '';
let segment = '';
let path: { regex: string | RegExp, segments: Array<RegExp>, globstar?: RegExp } = { regex: '', segments: [] };
// If we are doing extended matching, this boolean is true when we are inside
// a group (eg {*.html,*.js}), and false otherwise.
let inGroup = false;
let inRange = false;
// extglob stack. Keep track of scope
const ext = [];
interface AddOptions { split?: boolean; last?: boolean; only?: string};
// Helper function to build string and segments
function add(str, options: AddOptions={split: false, last: false, only: ''}) {
const {split, last, only} = options;
if (only !== 'path') regex += str;
if (filepath && only !== 'regex') {
path.regex += (str === '\\/' ? SEP : str);
if (split) {
if (last) segment += str;
if (segment !== '') {
if (!flags.includes('g')) segment = `^${segment}$`; // change it 'includes'
path.segments.push(new RegExp(segment, flags));
}
segment = '';
} else {
segment += str;
}
}
}
let c, n;
for (let i = 0; i < glob.length; i++) {
c = glob[i];
n = glob[i + 1];
if (['\\', '$', '^', '.', '='].includes(c)) {
add(`\\${c}`);
continue;
}
if (c === '/') {
add(`\\${c}`, {split: true});
if (n === '/' && !strict) regex += '?';
continue;
}
if (c === '(') {
if (ext.length) {
add(c);
continue;
}
add(`\\${c}`);
continue;
}
if (c === ')') {
if (ext.length) {
add(c);
let type = ext.pop();
if (type === '@') {
add('{1}');
} else if (type === '!') {
add('([^\/]*)');
} else {
add(type);
}
continue;
}
add(`\\${c}`);
continue;
}
if (c === '|') {
if (ext.length) {
add(c);
continue;
}
add(`\\${c}`);
continue;
}
if (c === '+') {
if (n === '(' && extended) {
ext.push(c);
continue;
}
add(`\\${c}`);
continue;
}
if (c === '@' && extended) {
if (n === '(') {
ext.push(c);
continue;
}
}
if (c === '!') {
if (extended) {
if (inRange) {
add('^');
continue
}
if (n === '(') {
ext.push(c);
add('(?!');
i++;
continue;
}
add(`\\${c}`);
continue;
}
add(`\\${c}`);
continue;
}
if (c === '?') {
if (extended) {
if (n === '(') {
ext.push(c);
} else {
add('.');
}
continue;
}
add(`\\${c}`);
continue;
}
if (c === '[') {
if (inRange && n === ':') {
i++; // skip [
let value = '';
while(glob[++i] !== ':') value += glob[i];
if (value === 'alnum') add('(\\w|\\d)');
else if (value === 'space') add('\\s');
else if (value === 'digit') add('\\d');
i++; // skip last ]
continue;
}
if (extended) {
inRange = true;
add(c);
continue;
}
add(`\\${c}`);
continue;
}
if (c === ']') {
if (extended) {
inRange = false;
add(c);
continue;
}
add(`\\${c}`);
continue;
}
if (c === '{') {
if (extended) {
inGroup = true;
add('(');
continue;
}
add(`\\${c}`);
continue;
}
if (c === '}') {
if (extended) {
inGroup = false;
add(')');
continue;
}
add(`\\${c}`);
continue;
}
if (c === ',') {
if (inGroup) {
add('|');
continue;
}
add(`\\${c}`);
continue;
}
if (c === '*') {
if (n === '(' && extended) {
ext.push(c);
continue;
}
// Move over all consecutive "*"'s.
// Also store the previous and next characters
let prevChar = glob[i - 1];
let starCount = 1;
while (glob[i + 1] === '*') {
starCount++;
i++;
}
let nextChar = glob[i + 1];
if (!globstar) {
// globstar is disabled, so treat any number of "*" as one
add('.*');
} else {
// globstar is enabled, so determine if this is a globstar segment
let isGlobstar =
starCount > 1 && // multiple "*"'s
(prevChar === '/' || prevChar === undefined) && // from the start of the segment
(nextChar === '/' || nextChar === undefined); // to the end of the segment
if (isGlobstar) {
// it's a globstar, so match zero or more path segments
add(GLOBSTAR, {only:'regex'});
add(GLOBSTAR_SEGMENT, {only:'path', last:true, split:true});
i++; // move over the "/"
} else {
// it's not a globstar, so only match one path segment
add(WILDCARD, {only:'regex'});
add(WILDCARD_SEGMENT, {only:'path'});
}
}
continue;
}
add(c);
}
// When regexp 'g' flag is specified don't
// constrain the regular expression with ^ & $
if (!flags.includes('g')) {
regex = `^${regex}$`;
segment = `^${segment}$`;
if (filepath) path.regex = `^${path.regex}$`;
}
const result: { regex: RegExp, path?: { regex: string | RegExp, segments: Array<RegExp>, globstar?: RegExp } } = {regex: new RegExp(regex, flags)};
// Push the last segment
if (filepath) {
path.segments.push(new RegExp(segment, flags));
path.regex = new RegExp(path.regex.toString(), flags);
path.globstar = new RegExp(!flags.includes('g') ? `^${GLOBSTAR_SEGMENT}$` : GLOBSTAR_SEGMENT, flags);
result.path = path;
}
return result;
}