You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.2 KiB

4 years ago
  1. # minimatch
  2. A minimal matching utility.
  3. [![Build Status](https://secure.travis-ci.org/isaacs/minimatch.svg)](http://travis-ci.org/isaacs/minimatch)
  4. This is the matching library used internally by npm.
  5. It works by converting glob expressions into JavaScript `RegExp`
  6. objects.
  7. ## Usage
  8. ```javascript
  9. var minimatch = require("minimatch")
  10. minimatch("bar.foo", "*.foo") // true!
  11. minimatch("bar.foo", "*.bar") // false!
  12. minimatch("bar.foo", "*.+(bar|foo)", { debug: true }) // true, and noisy!
  13. ```
  14. ## Features
  15. Supports these glob features:
  16. * Brace Expansion
  17. * Extended glob matching
  18. * "Globstar" `**` matching
  19. See:
  20. * `man sh`
  21. * `man bash`
  22. * `man 3 fnmatch`
  23. * `man 5 gitignore`
  24. ## Minimatch Class
  25. Create a minimatch object by instantiating the `minimatch.Minimatch` class.
  26. ```javascript
  27. var Minimatch = require("minimatch").Minimatch
  28. var mm = new Minimatch(pattern, options)
  29. ```
  30. ### Properties
  31. * `pattern` The original pattern the minimatch object represents.
  32. * `options` The options supplied to the constructor.
  33. * `set` A 2-dimensional array of regexp or string expressions.
  34. Each row in the
  35. array corresponds to a brace-expanded pattern. Each item in the row
  36. corresponds to a single path-part. For example, the pattern
  37. `{a,b/c}/d` would expand to a set of patterns like:
  38. [ [ a, d ]
  39. , [ b, c, d ] ]
  40. If a portion of the pattern doesn't have any "magic" in it
  41. (that is, it's something like `"foo"` rather than `fo*o?`), then it
  42. will be left as a string rather than converted to a regular
  43. expression.
  44. * `regexp` Created by the `makeRe` method. A single regular expression
  45. expressing the entire pattern. This is useful in cases where you wish
  46. to use the pattern somewhat like `fnmatch(3)` with `FNM_PATH` enabled.
  47. * `negate` True if the pattern is negated.
  48. * `comment` True if the pattern is a comment.
  49. * `empty` True if the pattern is `""`.
  50. ### Methods
  51. * `makeRe` Generate the `regexp` member if necessary, and return it.
  52. Will return `false` if the pattern is invalid.
  53. * `match(fname)` Return true if the filename matches the pattern, or
  54. false otherwise.
  55. * `matchOne(fileArray, patternArray, partial)` Take a `/`-split
  56. filename, and match it against a single row in the `regExpSet`. This
  57. method is mainly for internal use, but is exposed so that it can be
  58. used by a glob-walker that needs to avoid excessive filesystem calls.
  59. All other methods are internal, and will be called as necessary.
  60. ### minimatch(path, pattern, options)
  61. Main export. Tests a path against the pattern using the options.
  62. ```javascript
  63. var isJS = minimatch(file, "*.js", { matchBase: true })
  64. ```
  65. ### minimatch.filter(pattern, options)
  66. Returns a function that tests its
  67. supplied argument, suitable for use with `Array.filter`. Example:
  68. ```javascript
  69. var javascripts = fileList.filter(minimatch.filter("*.js", {matchBase: true}))
  70. ```
  71. ### minimatch.match(list, pattern, options)
  72. Match against the list of
  73. files, in the style of fnmatch or glob. If nothing is matched, and
  74. options.nonull is set, then return a list containing the pattern itself.
  75. ```javascript
  76. var javascripts = minimatch.match(fileList, "*.js", {matchBase: true}))
  77. ```
  78. ### minimatch.makeRe(pattern, options)
  79. Make a regular expression object from the pattern.
  80. ## Options
  81. All options are `false` by default.
  82. ### debug
  83. Dump a ton of stuff to stderr.
  84. ### nobrace
  85. Do not expand `{a,b}` and `{1..3}` brace sets.
  86. ### noglobstar
  87. Disable `**` matching against multiple folder names.
  88. ### dot
  89. Allow patterns to match filenames starting with a period, even if
  90. the pattern does not explicitly have a period in that spot.
  91. Note that by default, `a/**/b` will **not** match `a/.d/b`, unless `dot`
  92. is set.
  93. ### noext
  94. Disable "extglob" style patterns like `+(a|b)`.
  95. ### nocase
  96. Perform a case-insensitive match.
  97. ### nonull
  98. When a match is not found by `minimatch.match`, return a list containing
  99. the pattern itself if this option is set. When not set, an empty list
  100. is returned if there are no matches.
  101. ### matchBase
  102. If set, then patterns without slashes will be matched
  103. against the basename of the path if it contains slashes. For example,
  104. `a?b` would match the path `/xyz/123/acb`, but not `/xyz/acb/123`.
  105. ### nocomment
  106. Suppress the behavior of treating `#` at the start of a pattern as a
  107. comment.
  108. ### nonegate
  109. Suppress the behavior of treating a leading `!` character as negation.
  110. ### flipNegate
  111. Returns from negate expressions the same as if they were not negated.
  112. (Ie, true on a hit, false on a miss.)
  113. ## Comparisons to other fnmatch/glob implementations
  114. While strict compliance with the existing standards is a worthwhile
  115. goal, some discrepancies exist between minimatch and other
  116. implementations, and are intentional.
  117. If the pattern starts with a `!` character, then it is negated. Set the
  118. `nonegate` flag to suppress this behavior, and treat leading `!`
  119. characters normally. This is perhaps relevant if you wish to start the
  120. pattern with a negative extglob pattern like `!(a|B)`. Multiple `!`
  121. characters at the start of a pattern will negate the pattern multiple
  122. times.
  123. If a pattern starts with `#`, then it is treated as a comment, and
  124. will not match anything. Use `\#` to match a literal `#` at the
  125. start of a line, or set the `nocomment` flag to suppress this behavior.
  126. The double-star character `**` is supported by default, unless the
  127. `noglobstar` flag is set. This is supported in the manner of bsdglob
  128. and bash 4.1, where `**` only has special significance if it is the only
  129. thing in a path part. That is, `a/**/b` will match `a/x/y/b`, but
  130. `a/**b` will not.
  131. If an escaped pattern has no matches, and the `nonull` flag is set,
  132. then minimatch.match returns the pattern as-provided, rather than
  133. interpreting the character escapes. For example,
  134. `minimatch.match([], "\\*a\\?")` will return `"\\*a\\?"` rather than
  135. `"*a?"`. This is akin to setting the `nullglob` option in bash, except
  136. that it does not resolve escaped pattern characters.
  137. If brace expansion is not disabled, then it is performed before any
  138. other interpretation of the glob pattern. Thus, a pattern like
  139. `+(a|{b),c)}`, which would not be valid in bash or zsh, is expanded
  140. **first** into the set of `+(a|b)` and `+(a|c)`, and those patterns are
  141. checked for validity. Since those two are valid, matching proceeds.