UNPKG

22.7 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 negatedExtglob: boolean;
293 }
294
295 interface ScanInfoToken {
296 value: string;
297 depth: number;
298 isGlob: boolean;
299
300 backslashes?: boolean | undefined;
301 isBrace?: boolean | undefined;
302 isBracket?: boolean | undefined;
303 isExtglob?: boolean | undefined;
304 isGlobstar?: boolean | undefined;
305 isPrefix?: boolean | undefined;
306 negated?: boolean | undefined;
307 }
308
309 interface ScanInfoWithParts extends ScanInfo {
310 slashes: number[];
311 parts: string[];
312 }
313
314 interface ScanInfoWithTokens extends ScanInfoWithParts {
315 maxDepth: number;
316 tokens: ScanInfoToken[];
317 }
318}
319
320interface Micromatch {
321 /**
322 * The main function takes a list of strings and one or more glob patterns to use for matching.
323 *
324 * @param list A list of strings to match
325 * @param patterns One or more glob patterns to use for matching.
326 * @param options See available options for changing how matches are performed
327 * @returns Returns an array of matches
328 *
329 * @example
330 * ```js
331 * var mm = require('micromatch');
332 * mm(list, patterns[, options]);
333 *
334 * console.log(mm(['a.js', 'a.txt'], ['*.js']));
335 * //=> [ 'a.js' ]
336 * ```
337 */
338 (list: readonly string[], patterns: string | readonly string[], options?: micromatch.Options): string[];
339
340 /**
341 * Similar to the main function, but `pattern` must be a string.
342 *
343 * @param list Array of strings to match
344 * @param pattern Glob pattern to use for matching.
345 * @param options See available options for changing how matches are performed
346 * @returns Returns an array of matches
347 *
348 * @example
349 * ```js
350 * var mm = require('micromatch');
351 * mm.match(list, pattern[, options]);
352 *
353 * console.log(mm.match(['a.a', 'a.aa', 'a.b', 'a.c'], '*.a'));
354 * //=> ['a.a', 'a.aa']
355 * ```
356 */
357 match(list: readonly string[], pattern: string | readonly string[], options?: micromatch.Options): string[];
358
359 /**
360 * Returns true if the specified `string` matches the given glob `pattern`.
361 *
362 * @param string String to match
363 * @param pattern Glob pattern to use for matching.
364 * @param options See available options for changing how matches are performed
365 * @returns Returns true if the string matches the glob pattern.
366 *
367 * @example
368 * ```js
369 * var mm = require('micromatch');
370 * mm.isMatch(string, pattern[, options]);
371 *
372 * console.log(mm.isMatch('a.a', '*.a'));
373 * //=> true
374 * console.log(mm.isMatch('a.b', '*.a'));
375 * //=> false
376 * ```
377 */
378 isMatch(string: string, pattern: string | readonly string[], options?: micromatch.Options): boolean;
379
380 /**
381 * Returns true if some of the strings in the given `list` match any of the given glob `patterns`.
382 *
383 * @param list The string or array of strings to test. Returns as soon as the first match is found.
384 * @param patterns One or more glob patterns to use for matching.
385 * @param options See available options for changing how matches are performed
386 * @returns Returns true if any patterns match `str`
387 *
388 * @example
389 * ```js
390 * var mm = require('micromatch');
391 * mm.some(list, patterns[, options]);
392 *
393 * console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
394 * // true
395 * console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
396 * // false
397 * ```
398 */
399 some(
400 list: string | readonly string[],
401 patterns: string | readonly string[],
402 options?: micromatch.Options,
403 ): boolean;
404
405 /**
406 * Returns true if every string in the given `list` matches any of the given glob `patterns`.
407 *
408 * @param list The string or array of strings to test.
409 * @param patterns One or more glob patterns to use for matching.
410 * @param options See available options for changing how matches are performed
411 * @returns Returns true if any patterns match `str`
412 *
413 * @example
414 * ```js
415 * var mm = require('micromatch');
416 * mm.every(list, patterns[, options]);
417 *
418 * console.log(mm.every('foo.js', ['foo.js']));
419 * // true
420 * console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
421 * // true
422 * console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
423 * // false
424 * console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
425 * // false
426 * ```
427 */
428 every(
429 list: string | readonly string[],
430 patterns: string | readonly string[],
431 options?: micromatch.Options,
432 ): boolean;
433
434 /**
435 * Returns true if **any** of the given glob `patterns` match the specified `string`.
436 *
437 * @param str The string to test.
438 * @param patterns One or more glob patterns to use for matching.
439 * @param options See available options for changing how matches are performed
440 * @returns Returns true if any patterns match `str`
441 *
442 * @example
443 * ```js
444 * var mm = require('micromatch');
445 * mm.any(string, patterns[, options]);
446 *
447 * console.log(mm.any('a.a', ['b.*', '*.a']));
448 * //=> true
449 * console.log(mm.any('a.a', 'b.*'));
450 * //=> false
451 * ```
452 */
453 any(
454 str: string | readonly string[],
455 patterns: string | readonly string[],
456 options?: micromatch.Options,
457 ): boolean;
458
459 /**
460 * Returns true if **all** of the given `patterns` match the specified string.
461 *
462 * @param str The string to test.
463 * @param patterns One or more glob patterns to use for matching.
464 * @param options See available options for changing how matches are performed
465 * @returns Returns true if any patterns match `str`
466 *
467 * @example
468 * ```js
469 * var mm = require('micromatch');
470 * mm.all(string, patterns[, options]);
471 *
472 * console.log(mm.all('foo.js', ['foo.js']));
473 * // true
474 *
475 * console.log(mm.all('foo.js', ['*.js', '!foo.js']));
476 * // false
477 *
478 * console.log(mm.all('foo.js', ['*.js', 'foo.js']));
479 * // true
480 *
481 * console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
482 * // true
483 * ```
484 */
485 all(
486 str: string | readonly string[],
487 patterns: string | readonly string[],
488 options?: micromatch.Options,
489 ): boolean;
490
491 /**
492 * Returns a list of strings that _**do not match any**_ of the given `patterns`.
493 *
494 * @param list Array of strings to match.
495 * @param patterns One or more glob pattern to use for matching.
496 * @param options See available options for changing how matches are performed
497 * @returns Returns an array of strings that **do not match** the given patterns.
498 *
499 * @example
500 * ```js
501 * var mm = require('micromatch');
502 * mm.not(list, patterns[, options]);
503 *
504 * console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
505 * //=> ['b.b', 'c.c']
506 * ```
507 */
508 not(list: readonly string[], patterns: string | readonly string[], options?: micromatch.Options): string[];
509
510 /**
511 * Returns true if the given `string` contains the given pattern. Similar to [.isMatch](#isMatch) but the pattern can match any part of the string.
512 *
513 * @param str The string to match.
514 * @param patterns Glob pattern to use for matching.
515 * @param options See available options for changing how matches are performed
516 * @returns Returns true if the patter matches any part of `str`.
517 *
518 * @example
519 * ```js
520 * var mm = require('micromatch');
521 * mm.contains(string, pattern[, options]);
522 *
523 * console.log(mm.contains('aa/bb/cc', '*b'));
524 * //=> true
525 * console.log(mm.contains('aa/bb/cc', '*d'));
526 * //=> false
527 * ```
528 */
529 contains(str: string, patterns: string | readonly string[], options?: micromatch.Options): boolean;
530
531 /**
532 * Filter the keys of the given object with the given `glob` pattern and `options`. Does not attempt to match nested keys.
533 * If you need this feature, use [glob-object](https://github.com/jonschlinkert/glob-object) instead.
534 *
535 * @param object The object with keys to filter.
536 * @param patterns One or more glob patterns to use for matching.
537 * @param options See available options for changing how matches are performed
538 * @returns Returns an object with only keys that match the given patterns.
539 *
540 * @example
541 * ```js
542 * var mm = require('micromatch');
543 * mm.matchKeys(object, patterns[, options]);
544 *
545 * var obj = { aa: 'a', ab: 'b', ac: 'c' };
546 * console.log(mm.matchKeys(obj, '*b'));
547 * //=> { ab: 'b' }
548 * ```
549 */
550 matchKeys<T>(object: T, patterns: string | readonly string[], options?: micromatch.Options): Partial<T>;
551
552 /**
553 * 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.
554 *
555 * @param pattern Glob pattern
556 * @param options See available options for changing how matches are performed.
557 * @returns Returns a matcher function.
558 *
559 * @example
560 * ```js
561 * var mm = require('micromatch');
562 * mm.matcher(pattern[, options]);
563 *
564 * var isMatch = mm.matcher('*.!(*a)');
565 * console.log(isMatch('a.a'));
566 * //=> false
567 * console.log(isMatch('a.b'));
568 * //=> true
569 * ```
570 */
571 matcher(pattern: string, options?: micromatch.Options): (str: string) => boolean;
572
573 /**
574 * Returns an array of matches captured by `pattern` in `string, or`null` if the pattern did not match.
575 *
576 * @param pattern Glob pattern to use for matching.
577 * @param string String to match
578 * @param options See available options for changing how matches are performed
579 * @returns Returns an array of captures if the string matches the glob pattern, otherwise `null`.
580 *
581 * @example
582 * ```js
583 * var mm = require('micromatch');
584 * mm.capture(pattern, string[, options]);
585 *
586 * console.log(mm.capture('test/*.js', 'test/foo.js'));
587 * //=> ['foo']
588 * console.log(mm.capture('test/*.js', 'foo/bar.css'));
589 * //=> null
590 * ```
591 */
592 capture(pattern: string, string: string, options?: micromatch.Options): string[] | null;
593
594 /**
595 * Create a regular expression from the given glob `pattern`.
596 *
597 * @param pattern A glob pattern to convert to regex.
598 * @param options See available options for changing how matches are performed.
599 * @returns Returns a regex created from the given pattern.
600 *
601 * @example
602 * ```js
603 * var mm = require('micromatch');
604 * mm.makeRe(pattern[, options]);
605 *
606 * console.log(mm.makeRe('*.js'));
607 * //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
608 * ```
609 */
610 makeRe(pattern: string, options?: micromatch.Options): RegExp;
611
612 /**
613 * Expand the given brace `pattern`.
614 *
615 * @param pattern String with brace pattern to expand.
616 * @param options Any options to change how expansion is performed. See the [braces](https://github.com/micromatch/braces) library for all available options.
617 *
618 * @example
619 * ```js
620 * var mm = require('micromatch');
621 * console.log(mm.braces('foo/{a,b}/bar'));
622 * //=> ['foo/(a|b)/bar']
623 *
624 * console.log(mm.braces('foo/{a,b}/bar', {expand: true}));
625 * //=> ['foo/(a|b)/bar']
626 * ```
627 */
628 braces(pattern: string, options?: braces.Options): string[];
629
630 /**
631 * Parse a glob pattern to create the source string for a regular expression.
632 *
633 * @returns Returns an AST
634 *
635 * @example
636 * ```js
637 * var mm = require('micromatch');
638 * mm.parse(pattern[, options]);
639 *
640 * var ast = mm.parse('a/{b,c}/d');
641 * console.log(ast);
642 * // { type: 'root',
643 * // errors: [],
644 * // input: 'a/{b,c}/d',
645 * // nodes:
646 * // [ { type: 'bos', val: '' },
647 * // { type: 'text', val: 'a/' },
648 * // { type: 'brace',
649 * // nodes:
650 * // [ { type: 'brace.open', val: '{' },
651 * // { type: 'text', val: 'b,c' },
652 * // { type: 'brace.close', val: '}' } ] },
653 * // { type: 'text', val: '/d' },
654 * // { type: 'eos', val: '' } ] }
655 * ```
656 */
657 parse(glob: string, options?: micromatch.Options): object;
658
659 /**
660 * Scan a glob pattern to separate the pattern into segments.
661 */
662 scan(pattern: string, options: { parts: true } & micromatch.ScanOptions): micromatch.ScanInfoWithParts;
663 scan(pattern: string, options: { tokens: true } & micromatch.ScanOptions): micromatch.ScanInfoWithTokens;
664 scan(pattern: string, options?: micromatch.ScanOptions): micromatch.ScanInfo;
665}
666
667export as namespace micromatch;
668
669declare const micromatch: Micromatch;
670export = micromatch;