source: node_modules/minimatch/README.md@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 16.6 KB
RevLine 
[81bc7da]1# minimatch
2
3A minimal matching utility.
4
5This is the matching library used internally by npm.
6
7It works by converting glob expressions into JavaScript `RegExp`
8objects.
9
10## Usage
11
12```js
13// hybrid module, load with require() or import
14import { minimatch } from 'minimatch'
15// or:
16const { minimatch } = require('minimatch')
17
18minimatch('bar.foo', '*.foo') // true!
19minimatch('bar.foo', '*.bar') // false!
20minimatch('bar.foo', '*.+(bar|foo)', { debug: true }) // true, and noisy!
21```
22
23## Features
24
25Supports these glob features:
26
27- Brace Expansion
28- Extended glob matching
29- "Globstar" `**` matching
30- [Posix character
31 classes](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html),
32 like `[[:alpha:]]`, supporting the full range of Unicode
33 characters. For example, `[[:alpha:]]` will match against
34 `'é'`, though `[a-zA-Z]` will not. Collating symbol and set
35 matching is not supported, so `[[=e=]]` will _not_ match `'é'`
36 and `[[.ch.]]` will not match `'ch'` in locales where `ch` is
37 considered a single character.
38
39See:
40
41- `man sh`
42- `man bash` [Pattern
43 Matching](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html)
44- `man 3 fnmatch`
45- `man 5 gitignore`
46
47## Windows
48
49**Please only use forward-slashes in glob expressions.**
50
51Though windows uses either `/` or `\` as its path separator, only `/`
52characters are used by this glob implementation. You must use
53forward-slashes **only** in glob expressions. Back-slashes in patterns
54will always be interpreted as escape characters, not path separators.
55
56Note that `\` or `/` _will_ be interpreted as path separators in paths on
57Windows, and will match against `/` in glob expressions.
58
59So just always use `/` in patterns.
60
61### UNC Paths
62
63On Windows, UNC paths like `//?/c:/...` or
64`//ComputerName/Share/...` are handled specially.
65
66- Patterns starting with a double-slash followed by some
67 non-slash characters will preserve their double-slash. As a
68 result, a pattern like `//*` will match `//x`, but not `/x`.
69- Patterns staring with `//?/<drive letter>:` will _not_ treat
70 the `?` as a wildcard character. Instead, it will be treated
71 as a normal string.
72- Patterns starting with `//?/<drive letter>:/...` will match
73 file paths starting with `<drive letter>:/...`, and vice versa,
74 as if the `//?/` was not present. This behavior only is
75 present when the drive letters are a case-insensitive match to
76 one another. The remaining portions of the path/pattern are
77 compared case sensitively, unless `nocase:true` is set.
78
79Note that specifying a UNC path using `\` characters as path
80separators is always allowed in the file path argument, but only
81allowed in the pattern argument when `windowsPathsNoEscape: true`
82is set in the options.
83
84## Minimatch Class
85
86Create a minimatch object by instantiating the `minimatch.Minimatch` class.
87
88```javascript
89var Minimatch = require('minimatch').Minimatch
90var mm = new Minimatch(pattern, options)
91```
92
93### Properties
94
95- `pattern` The original pattern the minimatch object represents.
96- `options` The options supplied to the constructor.
97- `set` A 2-dimensional array of regexp or string expressions.
98 Each row in the
99 array corresponds to a brace-expanded pattern. Each item in the row
100 corresponds to a single path-part. For example, the pattern
101 `{a,b/c}/d` would expand to a set of patterns like:
102
103 [ [ a, d ]
104 , [ b, c, d ] ]
105
106 If a portion of the pattern doesn't have any "magic" in it
107 (that is, it's something like `"foo"` rather than `fo*o?`), then it
108 will be left as a string rather than converted to a regular
109 expression.
110
111- `regexp` Created by the `makeRe` method. A single regular expression
112 expressing the entire pattern. This is useful in cases where you wish
113 to use the pattern somewhat like `fnmatch(3)` with `FNM_PATH` enabled.
114- `negate` True if the pattern is negated.
115- `comment` True if the pattern is a comment.
116- `empty` True if the pattern is `""`.
117
118### Methods
119
120- `makeRe()` Generate the `regexp` member if necessary, and return it.
121 Will return `false` if the pattern is invalid.
122- `match(fname)` Return true if the filename matches the pattern, or
123 false otherwise.
124- `matchOne(fileArray, patternArray, partial)` Take a `/`-split
125 filename, and match it against a single row in the `regExpSet`. This
126 method is mainly for internal use, but is exposed so that it can be
127 used by a glob-walker that needs to avoid excessive filesystem calls.
128- `hasMagic()` Returns true if the parsed pattern contains any
129 magic characters. Returns false if all comparator parts are
130 string literals. If the `magicalBraces` option is set on the
131 constructor, then it will consider brace expansions which are
132 not otherwise magical to be magic. If not set, then a pattern
133 like `a{b,c}d` will return `false`, because neither `abd` nor
134 `acd` contain any special glob characters.
135
136 This does **not** mean that the pattern string can be used as a
137 literal filename, as it may contain magic glob characters that
138 are escaped. For example, the pattern `\\*` or `[*]` would not
139 be considered to have magic, as the matching portion parses to
140 the literal string `'*'` and would match a path named `'*'`,
141 not `'\\*'` or `'[*]'`. The `minimatch.unescape()` method may
142 be used to remove escape characters.
143
144All other methods are internal, and will be called as necessary.
145
146### minimatch(path, pattern, options)
147
148Main export. Tests a path against the pattern using the options.
149
150```javascript
151var isJS = minimatch(file, '*.js', { matchBase: true })
152```
153
154### minimatch.filter(pattern, options)
155
156Returns a function that tests its
157supplied argument, suitable for use with `Array.filter`. Example:
158
159```javascript
160var javascripts = fileList.filter(
161 minimatch.filter('*.js', { matchBase: true }),
162)
163```
164
165### minimatch.escape(pattern, options = {})
166
167Escape all magic characters in a glob pattern, so that it will
168only ever match literal strings.
169
170If the `windowsPathsNoEscape` option is used, then characters are
171escaped by wrapping in `[]`, because a magic character wrapped in
172a character class can only be satisfied by that exact character.
173
174Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot
175be escaped or unescaped.
176
177### minimatch.unescape(pattern, options = {})
178
179Un-escape a glob string that may contain some escaped characters.
180
181If the `windowsPathsNoEscape` option is used, then square-brace
182escapes are removed, but not backslash escapes. For example, it
183will turn the string `'[*]'` into `*`, but it will not turn
184`'\\*'` into `'*'`, because `\` is a path separator in
185`windowsPathsNoEscape` mode.
186
187When `windowsPathsNoEscape` is not set, then both brace escapes
188and backslash escapes are removed.
189
190Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot
191be escaped or unescaped.
192
193### minimatch.match(list, pattern, options)
194
195Match against the list of
196files, in the style of fnmatch or glob. If nothing is matched, and
197options.nonull is set, then return a list containing the pattern itself.
198
199```javascript
200var javascripts = minimatch.match(fileList, '*.js', { matchBase: true })
201```
202
203### minimatch.makeRe(pattern, options)
204
205Make a regular expression object from the pattern.
206
207## Options
208
209All options are `false` by default.
210
211### debug
212
213Dump a ton of stuff to stderr.
214
215### nobrace
216
217Do not expand `{a,b}` and `{1..3}` brace sets.
218
219### noglobstar
220
221Disable `**` matching against multiple folder names.
222
223### dot
224
225Allow patterns to match filenames starting with a period, even if
226the pattern does not explicitly have a period in that spot.
227
228Note that by default, `a/**/b` will **not** match `a/.d/b`, unless `dot`
229is set.
230
231### noext
232
233Disable "extglob" style patterns like `+(a|b)`.
234
235### nocase
236
237Perform a case-insensitive match.
238
239### nocaseMagicOnly
240
241When used with `{nocase: true}`, create regular expressions that
242are case-insensitive, but leave string match portions untouched.
243Has no effect when used without `{nocase: true}`.
244
245Useful when some other form of case-insensitive matching is used,
246or if the original string representation is useful in some other
247way.
248
249### nonull
250
251When a match is not found by `minimatch.match`, return a list containing
252the pattern itself if this option is set. When not set, an empty list
253is returned if there are no matches.
254
255### magicalBraces
256
257This only affects the results of the `Minimatch.hasMagic` method.
258
259If the pattern contains brace expansions, such as `a{b,c}d`, but
260no other magic characters, then the `Minimatch.hasMagic()` method
261will return `false` by default. When this option set, it will
262return `true` for brace expansion as well as other magic glob
263characters.
264
265### matchBase
266
267If set, then patterns without slashes will be matched
268against the basename of the path if it contains slashes. For example,
269`a?b` would match the path `/xyz/123/acb`, but not `/xyz/acb/123`.
270
271### nocomment
272
273Suppress the behavior of treating `#` at the start of a pattern as a
274comment.
275
276### nonegate
277
278Suppress the behavior of treating a leading `!` character as negation.
279
280### flipNegate
281
282Returns from negate expressions the same as if they were not negated.
283(Ie, true on a hit, false on a miss.)
284
285### partial
286
287Compare a partial path to a pattern. As long as the parts of the path that
288are present are not contradicted by the pattern, it will be treated as a
289match. This is useful in applications where you're walking through a
290folder structure, and don't yet have the full path, but want to ensure that
291you do not walk down paths that can never be a match.
292
293For example,
294
295```js
296minimatch('/a/b', '/a/*/c/d', { partial: true }) // true, might be /a/b/c/d
297minimatch('/a/b', '/**/d', { partial: true }) // true, might be /a/b/.../d
298minimatch('/x/y/z', '/a/**/z', { partial: true }) // false, because x !== a
299```
300
301### windowsPathsNoEscape
302
303Use `\\` as a path separator _only_, and _never_ as an escape
304character. If set, all `\\` characters are replaced with `/` in
305the pattern. Note that this makes it **impossible** to match
306against paths containing literal glob pattern characters, but
307allows matching with patterns constructed using `path.join()` and
308`path.resolve()` on Windows platforms, mimicking the (buggy!)
309behavior of earlier versions on Windows. Please use with
310caution, and be mindful of [the caveat about Windows
311paths](#windows).
312
313For legacy reasons, this is also set if
314`options.allowWindowsEscape` is set to the exact value `false`.
315
316### windowsNoMagicRoot
317
318When a pattern starts with a UNC path or drive letter, and in
319`nocase:true` mode, do not convert the root portions of the
320pattern into a case-insensitive regular expression, and instead
321leave them as strings.
322
323This is the default when the platform is `win32` and
324`nocase:true` is set.
325
326### preserveMultipleSlashes
327
328By default, multiple `/` characters (other than the leading `//`
329in a UNC path, see "UNC Paths" above) are treated as a single
330`/`.
331
332That is, a pattern like `a///b` will match the file path `a/b`.
333
334Set `preserveMultipleSlashes: true` to suppress this behavior.
335
336### optimizationLevel
337
338A number indicating the level of optimization that should be done
339to the pattern prior to parsing and using it for matches.
340
341Globstar parts `**` are always converted to `*` when `noglobstar`
342is set, and multiple adjacent `**` parts are converted into a
343single `**` (ie, `a/**/**/b` will be treated as `a/**/b`, as this
344is equivalent in all cases).
345
346- `0` - Make no further changes. In this mode, `.` and `..` are
347 maintained in the pattern, meaning that they must also appear
348 in the same position in the test path string. Eg, a pattern
349 like `a/*/../c` will match the string `a/b/../c` but not the
350 string `a/c`.
351- `1` - (default) Remove cases where a double-dot `..` follows a
352 pattern portion that is not `**`, `.`, `..`, or empty `''`. For
353 example, the pattern `./a/b/../*` is converted to `./a/*`, and
354 so it will match the path string `./a/c`, but not the path
355 string `./a/b/../c`. Dots and empty path portions in the
356 pattern are preserved.
357- `2` (or higher) - Much more aggressive optimizations, suitable
358 for use with file-walking cases:
359 - Remove cases where a double-dot `..` follows a pattern
360 portion that is not `**`, `.`, or empty `''`. Remove empty
361 and `.` portions of the pattern, where safe to do so (ie,
362 anywhere other than the last position, the first position, or
363 the second position in a pattern starting with `/`, as this
364 may indicate a UNC path on Windows).
365 - Convert patterns containing `<pre>/**/../<p>/<rest>` into the
366 equivalent `<pre>/{..,**}/<p>/<rest>`, where `<p>` is a
367 a pattern portion other than `.`, `..`, `**`, or empty
368 `''`.
369 - Dedupe patterns where a `**` portion is present in one and
370 omitted in another, and it is not the final path portion, and
371 they are otherwise equivalent. So `{a/**/b,a/b}` becomes
372 `a/**/b`, because `**` matches against an empty path portion.
373 - Dedupe patterns where a `*` portion is present in one, and a
374 non-dot pattern other than `**`, `.`, `..`, or `''` is in the
375 same position in the other. So `a/{*,x}/b` becomes `a/*/b`,
376 because `*` can match against `x`.
377
378 While these optimizations improve the performance of
379 file-walking use cases such as [glob](http://npm.im/glob) (ie,
380 the reason this module exists), there are cases where it will
381 fail to match a literal string that would have been matched in
382 optimization level 1 or 0.
383
384 Specifically, while the `Minimatch.match()` method will
385 optimize the file path string in the same ways, resulting in
386 the same matches, it will fail when tested with the regular
387 expression provided by `Minimatch.makeRe()`, unless the path
388 string is first processed with
389 `minimatch.levelTwoFileOptimize()` or similar.
390
391### platform
392
393When set to `win32`, this will trigger all windows-specific
394behaviors (special handling for UNC paths, and treating `\` as
395separators in file paths for comparison.)
396
397Defaults to the value of `process.platform`.
398
399## Comparisons to other fnmatch/glob implementations
400
401While strict compliance with the existing standards is a
402worthwhile goal, some discrepancies exist between minimatch and
403other implementations. Some are intentional, and some are
404unavoidable.
405
406If the pattern starts with a `!` character, then it is negated. Set the
407`nonegate` flag to suppress this behavior, and treat leading `!`
408characters normally. This is perhaps relevant if you wish to start the
409pattern with a negative extglob pattern like `!(a|B)`. Multiple `!`
410characters at the start of a pattern will negate the pattern multiple
411times.
412
413If a pattern starts with `#`, then it is treated as a comment, and
414will not match anything. Use `\#` to match a literal `#` at the
415start of a line, or set the `nocomment` flag to suppress this behavior.
416
417The double-star character `**` is supported by default, unless the
418`noglobstar` flag is set. This is supported in the manner of bsdglob
419and bash 4.1, where `**` only has special significance if it is the only
420thing in a path part. That is, `a/**/b` will match `a/x/y/b`, but
421`a/**b` will not.
422
423If an escaped pattern has no matches, and the `nonull` flag is set,
424then minimatch.match returns the pattern as-provided, rather than
425interpreting the character escapes. For example,
426`minimatch.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
427`"*a?"`. This is akin to setting the `nullglob` option in bash, except
428that it does not resolve escaped pattern characters.
429
430If brace expansion is not disabled, then it is performed before any
431other interpretation of the glob pattern. Thus, a pattern like
432`+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
433**first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
434checked for validity. Since those two are valid, matching proceeds.
435
436Negated extglob patterns are handled as closely as possible to
437Bash semantics, but there are some cases with negative extglobs
438which are exceedingly difficult to express in a JavaScript
439regular expression. In particular the negated pattern
440`<start>!(<pattern>*|)*` will in bash match anything that does
441not start with `<start><pattern>`. However,
442`<start>!(<pattern>*)*` _will_ match paths starting with
443`<start><pattern>`, because the empty string can match against
444the negated portion. In this library, `<start>!(<pattern>*|)*`
445will _not_ match any pattern starting with `<start>`, due to a
446difference in precisely which patterns are considered "greedy" in
447Regular Expressions vs bash path expansion. This may be fixable,
448but not without incurring some complexity and performance costs,
449and the trade-off seems to not be worth pursuing.
450
451Note that `fnmatch(3)` in libc is an extremely naive string comparison
452matcher, which does not do anything special for slashes. This library is
453designed to be used in glob searching and file walkers, and so it does do
454special things with `/`. Thus, `foo*` will not match `foo/bar` in this
455library, even though it would in `fnmatch(3)`.
Note: See TracBrowser for help on using the repository browser.