1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const unique = require('array-unique');
|
8 | const toRegex = require('to-regex');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const compilers = require('./lib/compilers');
|
15 | const parsers = require('./lib/parsers');
|
16 | const Extglob = require('./lib/extglob');
|
17 | const utils = require('./lib/utils');
|
18 | const MAX_LENGTH = 1024 * 64;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function extglob(pattern, options) {
|
36 | return extglob.create(pattern, options).output;
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | extglob.match = function(list, pattern, options) {
|
56 | if (typeof pattern !== 'string') {
|
57 | throw new TypeError('expected pattern to be a string');
|
58 | }
|
59 |
|
60 | list = utils.arrayify(list);
|
61 | const isMatch = extglob.matcher(pattern, options);
|
62 | const len = list.length;
|
63 | let idx = -1;
|
64 | const matches = [];
|
65 |
|
66 | while (++idx < len) {
|
67 | const ele = list[idx];
|
68 |
|
69 | if (isMatch(ele)) {
|
70 | matches.push(ele);
|
71 | }
|
72 | }
|
73 |
|
74 |
|
75 | if (typeof options === 'undefined') {
|
76 | return unique(matches);
|
77 | }
|
78 |
|
79 | if (matches.length === 0) {
|
80 | if (options.failglob === true) {
|
81 | throw new Error('no matches found for "' + pattern + '"');
|
82 | }
|
83 | if (options.nonull === true || options.nullglob === true) {
|
84 | return [pattern.split('\\').join('')];
|
85 | }
|
86 | }
|
87 |
|
88 | return options.nodupes !== false ? unique(matches) : matches;
|
89 | };
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | extglob.isMatch = function(str, pattern, options) {
|
111 | if (typeof pattern !== 'string') {
|
112 | throw new TypeError('expected pattern to be a string');
|
113 | }
|
114 |
|
115 | if (typeof str !== 'string') {
|
116 | throw new TypeError('expected a string');
|
117 | }
|
118 |
|
119 | if (pattern === str) {
|
120 | return true;
|
121 | }
|
122 |
|
123 | if (pattern === '' || pattern === ' ' || pattern === '.') {
|
124 | return pattern === str;
|
125 | }
|
126 |
|
127 | const isMatch = utils.memoize('isMatch', pattern, options, extglob.matcher);
|
128 | return isMatch(str);
|
129 | };
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | extglob.contains = function(str, pattern, options) {
|
150 | if (typeof str !== 'string') {
|
151 | throw new TypeError('expected a string');
|
152 | }
|
153 |
|
154 | if (pattern === '' || pattern === ' ' || pattern === '.') {
|
155 | return pattern === str;
|
156 | }
|
157 |
|
158 | const opts = Object.assign({}, options, {contains: true});
|
159 | opts.strictClose = false;
|
160 | opts.strictOpen = false;
|
161 | return extglob.isMatch(str, pattern, opts);
|
162 | };
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | extglob.matcher = function(pattern, options) {
|
184 | if (typeof pattern !== 'string') {
|
185 | throw new TypeError('expected pattern to be a string');
|
186 | }
|
187 |
|
188 | function matcher() {
|
189 | const re = extglob.makeRe(pattern, options);
|
190 | return function(str) {
|
191 | return re.test(str);
|
192 | };
|
193 | }
|
194 |
|
195 | return utils.memoize('matcher', pattern, options, matcher);
|
196 | };
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | extglob.create = function(pattern, options) {
|
214 | if (typeof pattern !== 'string') {
|
215 | throw new TypeError('expected pattern to be a string');
|
216 | }
|
217 |
|
218 | function create() {
|
219 | const ext = new Extglob(options);
|
220 | const ast = ext.parse(pattern, options);
|
221 | return ext.compile(ast, options);
|
222 | }
|
223 |
|
224 | return utils.memoize('create', pattern, options, create);
|
225 | };
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | extglob.capture = function(pattern, str, options) {
|
248 | const re = extglob.makeRe(pattern, Object.assign({capture: true}, options));
|
249 |
|
250 | function match() {
|
251 | return function(string) {
|
252 | const match = re.exec(string);
|
253 | if (!match) {
|
254 | return null;
|
255 | }
|
256 |
|
257 | return match.slice(1);
|
258 | };
|
259 | }
|
260 |
|
261 | const capture = utils.memoize('capture', pattern, options, match);
|
262 | return capture(str);
|
263 | };
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 | extglob.makeRe = function(pattern, options) {
|
281 | if (pattern instanceof RegExp) {
|
282 | return pattern;
|
283 | }
|
284 |
|
285 | if (typeof pattern !== 'string') {
|
286 | throw new TypeError('expected pattern to be a string');
|
287 | }
|
288 |
|
289 | if (pattern.length > MAX_LENGTH) {
|
290 | throw new Error('expected pattern to be less than ' + MAX_LENGTH + ' characters');
|
291 | }
|
292 |
|
293 | function makeRe() {
|
294 | const opts = Object.assign({strictErrors: false}, options);
|
295 | if (opts.strictErrors === true) opts.strict = true;
|
296 | const res = extglob.create(pattern, opts);
|
297 | return toRegex(res.output, opts);
|
298 | }
|
299 |
|
300 | const regex = utils.memoize('makeRe', pattern, options, makeRe);
|
301 | if (regex.source.length > MAX_LENGTH) {
|
302 | throw new SyntaxError('potentially malicious regex detected');
|
303 | }
|
304 |
|
305 | return regex;
|
306 | };
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | extglob.cache = utils.cache;
|
313 | extglob.clearCache = function() {
|
314 | extglob.cache.__data__ = {};
|
315 | };
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 | extglob.Extglob = Extglob;
|
322 | extglob.compilers = compilers;
|
323 | extglob.parsers = parsers;
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 | module.exports = extglob;
|