UNPKG

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