UNPKG

22.6 kBTypeScriptView Raw
1// TypeScript Version: 2.2
2
3import * as braces from "braces";
4declare namespace micromatch {
5 interface Item {
6 glob: string;
7 regex: RegExp;
8 input: string;
9 output: string;
10 }
11 interface Options {
12 /**
13 * Allow glob patterns without slashes to match a file path based on its basename. Same behavior as [minimatch](https://github.com/isaacs/minimatch) option `matchBase`.
14 *
15 * @default false
16 *
17 * @example
18 * ```js
19 * mm(['a/b.js', 'a/c.md'], '*.js');
20 * //=> []
21 *
22 * mm(['a/b.js', 'a/c.md'], '*.js', {matchBase: true});
23 * //=> ['a/b.js']
24 * ```
25 */
26 basename?: boolean | undefined;
27 /**
28 * Enabled by default, this option enforces bash-like behavior with stars immediately following a bracket expression.
29 * Bash bracket expressions are similar to regex character classes, but unlike regex, a star following a bracket expression **does not repeat the bracketed characters**.
30 * Instead, the star is treated the same as an other star.
31 *
32 * @default true
33 *
34 * @example
35 * ```js
36 * var files = ['abc', 'ajz'];
37 * console.log(mm(files, '[a-c]*'));
38 * //=> ['abc', 'ajz']
39 *
40 * console.log(mm(files, '[a-c]*', {bash: false}));
41 * ```
42 */
43 bash?: boolean | undefined;
44 /**
45 * Return regex matches in supporting methods.
46 *
47 * @default undefined
48 */
49 capture?: boolean | undefined;
50 /**
51 * Allows glob to match any part of the given string(s).
52 *
53 * @default undefined
54 */
55 contains?: boolean | undefined;
56 /**
57 * Current working directory. Used by `picomatch.split()`
58 *
59 * @default process.cwd()
60 */
61 cwd?: string | undefined;
62 /**
63 * Debug regular expressions when an error is thrown.
64 *
65 * @default undefined
66 */
67 debug?: boolean | undefined;
68 /**
69 * Match dotfiles. Otherwise dotfiles are ignored unless a `.` is explicitly defined in the pattern.
70 *
71 * @default false
72 */
73 dot?: boolean | undefined;
74 /**
75 * Custom function for expanding ranges in brace patterns, such as `{a..z}`.
76 * The function receives the range values as two arguments, and it must return a string to be used in the generated regex.
77 * It's recommended that returned strings be wrapped in parentheses. This option is overridden by the expandBrace option.
78 *
79 * @default undefined
80 */
81 expandRange?: ((left: string, right: string, options: Options) => string) | undefined;
82 /**
83 * Similar to the `--failglob` behavior in Bash, throws an error when no matches are found.
84 *
85 * @default false
86 */
87 failglob?: boolean | undefined;
88 /**
89 * To speed up processing, full parsing is skipped for a handful common glob patterns. Disable this behavior by setting this option to false.
90 *
91 * @default true
92 */
93 fastpaths?: boolean | undefined;
94 /**
95 * Regex flags to use in the generated regex. If defined, the `nocase` option will be overridden.
96 *
97 * @default undefined
98 */
99 flags?: boolean | undefined;
100 /**
101 * Custom function for formatting the returned string. This is useful for removing leading slashes, converting Windows paths to Posix paths, etc.
102 *
103 * @default undefined
104 */
105 format?: ((returnedString: string) => string) | undefined;
106 /**
107 * One or more glob patterns for excluding strings that should not be matched from the result.
108 *
109 * @default undefined
110 */
111 ignore?: string | readonly string[] | undefined;
112 /**
113 * Retain quotes in the generated regex, since quotes may also be used as an alternative to backslashes.
114 *
115 * @default false
116 */
117 keepQuotes?: boolean | undefined;
118 /**
119 * When `true`, brackets in the glob pattern will be escaped so that only literal brackets will be matched.
120 *
121 * @default undefined
122 */
123 literalBrackets?: boolean | undefined;
124 /**
125 * Support regex positive and negative lookbehinds. Note that you must be using Node 8.1.10 or higher to enable regex lookbehinds.
126 *
127 * @default true
128 */
129 lookbehinds?: boolean | undefined;
130 /**
131 * Alias for `basename`.
132 *
133 * @default false
134 */
135 matchBase?: boolean | undefined;
136 /**
137 * Limit the max length of the input string. An error is thrown if the input string is longer than this value.
138 *
139 * @default 65536
140 */
141 maxLength?: number | undefined;
142 /**
143 * Disable brace matching, so that `{a,b}` and `{1..3}` would be treated as literal characters.
144 *
145 * @default false
146 */
147 nobrace?: boolean | undefined;
148 /**
149 * Disable matching with regex brackets.
150 *
151 * @default undefined
152 */
153 nobracket?: boolean | undefined;
154 /**
155 * Perform case-insensitive matching. Equivalent to the regex `i` flag.
156 * Note that this option is ignored when the `flags` option is defined.
157 *
158 * @default false
159 */
160 nocase?: boolean | undefined;
161 /**
162 * Alias for `noextglob`
163 *
164 * @default false
165 */
166 noext?: boolean | undefined;
167 /**
168 * Disable support for matching with extglobs (like `+(a|b)`)
169 *
170 * @default false
171 */
172 noextglob?: boolean | undefined;
173 /**
174 * Disable matching with globstars (`**`).
175 *
176 * @default undefined
177 */
178 noglobstar?: boolean | undefined;
179 /**
180 * Disallow negation (`!`) patterns, and treat leading `!` as a literal character to match.
181 *
182 * @default undefined
183 */
184 nonegate?: boolean | undefined;
185 /**
186 * Disable support for regex quantifiers (like `a{1,2}`) and treat them as brace patterns to be expanded.
187 *
188 * @default false
189 */
190 noquantifiers?: boolean | undefined;
191 /**
192 * Function to be called on ignored items.
193 *
194 * @default undefined
195 */
196 onIgnore?: ((item: Item) => void) | undefined;
197 /**
198 * Function to be called on matched items.
199 *
200 * @default undefined
201 */
202 onMatch?: ((item: Item) => void) | undefined;
203 /**
204 * Function to be called on all items, regardless of whether or not they are matched or ignored.
205 *
206 * @default undefined
207 */
208 onResult?: ((item: Item) => void) | undefined;
209 /**
210 * Support POSIX character classes ("posix brackets").
211 *
212 * @default false
213 */
214 posix?: boolean | undefined;
215 /**
216 * String to prepend to the generated regex used for matching.
217 *
218 * @default undefined
219 */
220 prepend?: boolean | undefined;
221 /**
222 * Use regular expression rules for `+` (instead of matching literal `+`), and for stars that follow closing parentheses or brackets (as in `)*` and `]*`).
223 *
224 * @default false
225 */
226 regex?: boolean | undefined;
227 /**
228 * Throw an error if brackets, braces, or parens are imbalanced.
229 *
230 * @default undefined
231 */
232 strictBrackets?: boolean | undefined;
233 /**
234 * When true, picomatch won't match trailing slashes with single stars.
235 *
236 * @default undefined
237 */
238 strictSlashes?: boolean | undefined;
239 /**
240 * Remove backslashes from returned matches.
241 *
242 * @default undefined
243 *
244 * @example
245 * In this example we want to match a literal `*`:
246 *
247 * ```js
248 * mm.match(['abc', 'a\\*c'], 'a\\*c');
249 * //=> ['a\\*c']
250 *
251 * mm.match(['abc', 'a\\*c'], 'a\\*c', {unescape: true});
252 * //=> ['a*c']
253 * ```
254 */
255 unescape?: boolean | undefined;
256 /**
257 * Convert all slashes in file paths to forward slashes. This does not convert slashes in the glob pattern itself
258 *
259 * @default undefined
260 */
261 windows?: boolean | undefined;
262 }
263
264 interface ScanOptions extends Options {
265 /**
266 * When `true`, the returned object will include an array of `tokens` (objects), representing each path "segment" in the scanned glob pattern.
267 *
268 * @default false
269 */
270 tokens?: boolean | undefined;
271 /**
272 * When `true`, the returned object will include an array of strings representing each path "segment" in the scanned glob pattern.
273 * This is automatically enabled when `options.tokens` is `true`.
274 *
275 * @default false
276 */
277 parts?: boolean | undefined;
278 }
279
280 interface ScanInfo {
281 prefix: string;
282 input: string;
283 start: number;
284 base: string;
285 glob: string;
286 isBrace: boolean;
287 isBracket: boolean;
288 isGlob: boolean;
289 isExtglob: boolean;
290 isGlobstar: boolean;
291 negated: boolean;
292 }
293
294 interface ScanInfoToken {
295 value: string;
296 depth: number;
297 isGlob: boolean;
298
299 backslashes?: boolean | undefined;
300 isBrace?: boolean | undefined;
301 isBracket?: boolean | undefined;
302 isExtglob?: boolean | undefined;
303 isGlobstar?: boolean | undefined;
304 isPrefix?: boolean | undefined;
305 negated?: boolean | undefined;
306 }
307
308 interface ScanInfoWithParts extends ScanInfo {
309 slashes: number[];
310 parts: string[];
311 }
312
313 interface ScanInfoWithTokens extends ScanInfoWithParts {
314 maxDepth: number;
315 tokens: ScanInfoToken[];
316 }
317}
318
319interface Micromatch {
320 /**
321 * The main function takes a list of strings and one or more glob patterns to use for matching.
322 *
323 * @param list A list of strings to match
324 * @param patterns One or more glob patterns to use for matching.
325 * @param options See available options for changing how matches are performed
326 * @returns Returns an array of matches
327 *
328 * @example
329 * ```js
330 * var mm = require('micromatch');
331 * mm(list, patterns[, options]);
332 *
333 * console.log(mm(['a.js', 'a.txt'], ['*.js']));
334 * //=> [ 'a.js' ]
335 * ```
336 */
337 (list: readonly string[], patterns: string | readonly string[], options?: micromatch.Options): string[];
338
339 /**
340 * Similar to the main function, but `pattern` must be a string.
341 *
342 * @param list Array of strings to match
343 * @param pattern Glob pattern to use for matching.
344 * @param options See available options for changing how matches are performed
345 * @returns Returns an array of matches
346 *
347 * @example
348 * ```js
349 * var mm = require('micromatch');
350 * mm.match(list, pattern[, options]);
351 *
352 * console.log(mm.match(['a.a', 'a.aa', 'a.b', 'a.c'], '*.a'));
353 * //=> ['a.a', 'a.aa']
354 * ```
355 */
356 match(list: readonly string[], pattern: string, options?: micromatch.Options): string[];
357
358 /**
359 * Returns true if the specified `string` matches the given glob `pattern`.
360 *
361 * @param string String to match
362 * @param pattern Glob pattern to use for matching.
363 * @param options See available options for changing how matches are performed
364 * @returns Returns true if the string matches the glob pattern.
365 *
366 * @example
367 * ```js
368 * var mm = require('micromatch');
369 * mm.isMatch(string, pattern[, options]);
370 *
371 * console.log(mm.isMatch('a.a', '*.a'));
372 * //=> true
373 * console.log(mm.isMatch('a.b', '*.a'));
374 * //=> false
375 * ```
376 */
377 isMatch(string: string, pattern: string | readonly string[], options?: micromatch.Options): boolean;
378
379 /**
380 * Returns true if some of the strings in the given `list` match any of the given glob `patterns`.
381 *
382 * @param list The string or array of strings to test. Returns as soon as the first match is found.
383 * @param patterns One or more glob patterns to use for matching.
384 * @param options See available options for changing how matches are performed
385 * @returns Returns true if any patterns match `str`
386 *
387 * @example
388 * ```js
389 * var mm = require('micromatch');
390 * mm.some(list, patterns[, options]);
391 *
392 * console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
393 * // true
394 * console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
395 * // false
396 * ```
397 */
398 some(
399 list: string | readonly string[],
400 patterns: string | readonly string[],
401 options?: micromatch.Options,
402 ): boolean;
403
404 /**
405 * Returns true if every string in the given `list` matches any of the given glob `patterns`.
406 *
407 * @param list The string or array of strings to test.
408 * @param patterns One or more glob patterns to use for matching.
409 * @param options See available options for changing how matches are performed
410 * @returns Returns true if any patterns match `str`
411 *
412 * @example
413 * ```js
414 * var mm = require('micromatch');
415 * mm.every(list, patterns[, options]);
416 *
417 * console.log(mm.every('foo.js', ['foo.js']));
418 * // true
419 * console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
420 * // true
421 * console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
422 * // false
423 * console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
424 * // false
425 * ```
426 */
427 every(
428 list: string | readonly string[],
429 patterns: string | readonly string[],
430 options?: micromatch.Options,
431 ): boolean;
432
433 /**
434 * Returns true if **any** of the given glob `patterns` match the specified `string`.
435 *
436 * @param str The string to test.
437 * @param patterns One or more glob patterns to use for matching.
438 * @param options See available options for changing how matches are performed
439 * @returns Returns true if any patterns match `str`
440 *
441 * @example
442 * ```js
443 * var mm = require('micromatch');
444 * mm.any(string, patterns[, options]);
445 *
446 * console.log(mm.any('a.a', ['b.*', '*.a']));
447 * //=> true
448 * console.log(mm.any('a.a', 'b.*'));
449 * //=> false
450 * ```
451 */
452 any(
453 str: string | readonly string[],
454 patterns: string | readonly string[],
455 options?: micromatch.Options,
456 ): boolean;
457
458 /**
459 * Returns true if **all** of the given `patterns` match the specified string.
460 *
461 * @param str The string to test.
462 * @param patterns One or more glob patterns to use for matching.
463 * @param options See available options for changing how matches are performed
464 * @returns Returns true if any patterns match `str`
465 *
466 * @example
467 * ```js
468 * var mm = require('micromatch');
469 * mm.all(string, patterns[, options]);
470 *
471 * console.log(mm.all('foo.js', ['foo.js']));
472 * // true
473 *
474 * console.log(mm.all('foo.js', ['*.js', '!foo.js']));
475 * // false
476 *
477 * console.log(mm.all('foo.js', ['*.js', 'foo.js']));
478 * // true
479 *
480 * console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
481 * // true
482 * ```
483 */
484 all(
485 str: string | readonly string[],
486 patterns: string | readonly string[],
487 options?: micromatch.Options,
488 ): boolean;
489
490 /**
491 * Returns a list of strings that _**do not match any**_ of the given `patterns`.
492 *
493 * @param list Array of strings to match.
494 * @param patterns One or more glob pattern to use for matching.
495 * @param options See available options for changing how matches are performed
496 * @returns Returns an array of strings that **do not match** the given patterns.
497 *
498 * @example
499 * ```js
500 * var mm = require('micromatch');
501 * mm.not(list, patterns[, options]);
502 *
503 * console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
504 * //=> ['b.b', 'c.c']
505 * ```
506 */
507 not(list: readonly string[], patterns: string | readonly string[], options?: micromatch.Options): string[];
508
509 /**
510 * Returns true if the given `string` contains the given pattern. Similar to [.isMatch](#isMatch) but the pattern can match any part of the string.
511 *
512 * @param str The string to match.
513 * @param patterns Glob pattern to use for matching.
514 * @param options See available options for changing how matches are performed
515 * @returns Returns true if the patter matches any part of `str`.
516 *
517 * @example
518 * ```js
519 * var mm = require('micromatch');
520 * mm.contains(string, pattern[, options]);
521 *
522 * console.log(mm.contains('aa/bb/cc', '*b'));
523 * //=> true
524 * console.log(mm.contains('aa/bb/cc', '*d'));
525 * //=> false
526 * ```
527 */
528 contains(str: string, patterns: string | readonly string[], options?: micromatch.Options): boolean;
529
530 /**
531 * Filter the keys of the given object with the given `glob` pattern and `options`. Does not attempt to match nested keys.
532 * If you need this feature, use [glob-object](https://github.com/jonschlinkert/glob-object) instead.
533 *
534 * @param object The object with keys to filter.
535 * @param patterns One or more glob patterns to use for matching.
536 * @param options See available options for changing how matches are performed
537 * @returns Returns an object with only keys that match the given patterns.
538 *
539 * @example
540 * ```js
541 * var mm = require('micromatch');
542 * mm.matchKeys(object, patterns[, options]);
543 *
544 * var obj = { aa: 'a', ab: 'b', ac: 'c' };
545 * console.log(mm.matchKeys(obj, '*b'));
546 * //=> { ab: 'b' }
547 * ```
548 */
549 matchKeys<T>(object: T, patterns: string | readonly string[], options?: micromatch.Options): Partial<T>;
550
551 /**
552 * Returns a memoized matcher function from the given glob `pattern` and `options`. The returned function takes a string to match as its only argument and returns true if the string is a match.
553 *
554 * @param pattern Glob pattern
555 * @param options See available options for changing how matches are performed.
556 * @returns Returns a matcher function.
557 *
558 * @example
559 * ```js
560 * var mm = require('micromatch');
561 * mm.matcher(pattern[, options]);
562 *
563 * var isMatch = mm.matcher('*.!(*a)');
564 * console.log(isMatch('a.a'));
565 * //=> false
566 * console.log(isMatch('a.b'));
567 * //=> true
568 * ```
569 */
570 matcher(pattern: string, options?: micromatch.Options): (str: string) => boolean;
571
572 /**
573 * Returns an array of matches captured by `pattern` in `string, or`null` if the pattern did not match.
574 *
575 * @param pattern Glob pattern to use for matching.
576 * @param string String to match
577 * @param options See available options for changing how matches are performed
578 * @returns Returns an array of captures if the string matches the glob pattern, otherwise `null`.
579 *
580 * @example
581 * ```js
582 * var mm = require('micromatch');
583 * mm.capture(pattern, string[, options]);
584 *
585 * console.log(mm.capture('test/*.js', 'test/foo.js'));
586 * //=> ['foo']
587 * console.log(mm.capture('test/*.js', 'foo/bar.css'));
588 * //=> null
589 * ```
590 */
591 capture(pattern: string, string: string, options?: micromatch.Options): string[] | null;
592
593 /**
594 * Create a regular expression from the given glob `pattern`.
595 *
596 * @param pattern A glob pattern to convert to regex.
597 * @param options See available options for changing how matches are performed.
598 * @returns Returns a regex created from the given pattern.
599 *
600 * @example
601 * ```js
602 * var mm = require('micromatch');
603 * mm.makeRe(pattern[, options]);
604 *
605 * console.log(mm.makeRe('*.js'));
606 * //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
607 * ```
608 */
609 makeRe(pattern: string, options?: micromatch.Options): RegExp;
610
611 /**
612 * Expand the given brace `pattern`.
613 *
614 * @param pattern String with brace pattern to expand.
615 * @param options Any options to change how expansion is performed. See the [braces](https://github.com/micromatch/braces) library for all available options.
616 *
617 * @example
618 * ```js
619 * var mm = require('micromatch');
620 * console.log(mm.braces('foo/{a,b}/bar'));
621 * //=> ['foo/(a|b)/bar']
622 *
623 * console.log(mm.braces('foo/{a,b}/bar', {expand: true}));
624 * //=> ['foo/(a|b)/bar']
625 * ```
626 */
627 braces(pattern: string, options?: braces.Options): string[];
628
629 /**
630 * Parse a glob pattern to create the source string for a regular expression.
631 *
632 * @returns Returns an AST
633 *
634 * @example
635 * ```js
636 * var mm = require('micromatch');
637 * mm.parse(pattern[, options]);
638 *
639 * var ast = mm.parse('a/{b,c}/d');
640 * console.log(ast);
641 * // { type: 'root',
642 * // errors: [],
643 * // input: 'a/{b,c}/d',
644 * // nodes:
645 * // [ { type: 'bos', val: '' },
646 * // { type: 'text', val: 'a/' },
647 * // { type: 'brace',
648 * // nodes:
649 * // [ { type: 'brace.open', val: '{' },
650 * // { type: 'text', val: 'b,c' },
651 * // { type: 'brace.close', val: '}' } ] },
652 * // { type: 'text', val: '/d' },
653 * // { type: 'eos', val: '' } ] }
654 * ```
655 */
656 parse(glob: string, options?: micromatch.Options): object;
657
658 /**
659 * Scan a glob pattern to separate the pattern into segments.
660 */
661 scan(pattern: string, options: { parts: true } & micromatch.ScanOptions): micromatch.ScanInfoWithParts;
662 scan(pattern: string, options: { tokens: true } & micromatch.ScanOptions): micromatch.ScanInfoWithTokens;
663 scan(pattern: string, options?: micromatch.ScanOptions): micromatch.ScanInfo;
664}
665
666export as namespace micromatch;
667
668declare const micromatch: Micromatch;
669export = micromatch;