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.

README.md 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. # extglob [![NPM version](https://img.shields.io/npm/v/extglob.svg?style=flat)](https://www.npmjs.com/package/extglob) [![NPM monthly downloads](https://img.shields.io/npm/dm/extglob.svg?style=flat)](https://npmjs.org/package/extglob) [![NPM total downloads](https://img.shields.io/npm/dt/extglob.svg?style=flat)](https://npmjs.org/package/extglob) [![Linux Build Status](https://img.shields.io/travis/micromatch/extglob.svg?style=flat&label=Travis)](https://travis-ci.org/micromatch/extglob) [![Windows Build Status](https://img.shields.io/appveyor/ci/micromatch/extglob.svg?style=flat&label=AppVeyor)](https://ci.appveyor.com/project/micromatch/extglob)
  2. > Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob patterns.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. ## Install
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save extglob
  8. ```
  9. * Convert an extglob string to a regex-compatible string.
  10. * More complete (and correct) support than [minimatch](https://github.com/isaacs/minimatch) (minimatch fails a large percentage of the extglob tests)
  11. * Handles [negation patterns](#extglob-patterns)
  12. * Handles [nested patterns](#extglob-patterns)
  13. * Organized code base, easy to maintain and make changes when edge cases arise
  14. * As you can see by the [benchmarks](#benchmarks), extglob doesn't pay with speed for it's completeness, accuracy and quality.
  15. **Heads up!**: This library only supports extglobs, to handle full glob patterns and other extended globbing features use [micromatch](https://github.com/jonschlinkert/micromatch) instead.
  16. ## Usage
  17. The main export is a function that takes a string and options, and returns an object with the parsed AST and the compiled `.output`, which is a regex-compatible string that can be used for matching.
  18. ```js
  19. var extglob = require('extglob');
  20. console.log(extglob('!(xyz)*.js'));
  21. ```
  22. ## Extglob cheatsheet
  23. Extended globbing patterns can be defined as follows (as described by the [bash man page](https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html)):
  24. | **pattern** | **regex equivalent** | **description** |
  25. | --- | --- | --- |
  26. | `?(pattern-list)` | `(...|...)?` | Matches zero or one occurrence of the given pattern(s) |
  27. | `*(pattern-list)` | `(...|...)*` | Matches zero or more occurrences of the given pattern(s) |
  28. | `+(pattern-list)` | `(...|...)+` | Matches one or more occurrences of the given pattern(s) |
  29. | `@(pattern-list)` | `(...|...)` <sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup> | Matches one of the given pattern(s) |
  30. | `!(pattern-list)` | N/A | Matches anything except one of the given pattern(s) |
  31. ## API
  32. ### [extglob](index.js#L36)
  33. Convert the given `extglob` pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
  34. **Params**
  35. * `pattern` **{String}**
  36. * `options` **{Object}**
  37. * `returns` **{String}**
  38. **Example**
  39. ```js
  40. var extglob = require('extglob');
  41. console.log(extglob('*.!(*a)'));
  42. //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
  43. ```
  44. ### [.match](index.js#L56)
  45. Takes an array of strings and an extglob pattern and returns a new array that contains only the strings that match the pattern.
  46. **Params**
  47. * `list` **{Array}**: Array of strings to match
  48. * `pattern` **{String}**: Extglob pattern
  49. * `options` **{Object}**
  50. * `returns` **{Array}**: Returns an array of matches
  51. **Example**
  52. ```js
  53. var extglob = require('extglob');
  54. console.log(extglob.match(['a.a', 'a.b', 'a.c'], '*.!(*a)'));
  55. //=> ['a.b', 'a.c']
  56. ```
  57. ### [.isMatch](index.js#L111)
  58. Returns true if the specified `string` matches the given extglob `pattern`.
  59. **Params**
  60. * `string` **{String}**: String to match
  61. * `pattern` **{String}**: Extglob pattern
  62. * `options` **{String}**
  63. * `returns` **{Boolean}**
  64. **Example**
  65. ```js
  66. var extglob = require('extglob');
  67. console.log(extglob.isMatch('a.a', '*.!(*a)'));
  68. //=> false
  69. console.log(extglob.isMatch('a.b', '*.!(*a)'));
  70. //=> true
  71. ```
  72. ### [.contains](index.js#L150)
  73. Returns true if the given `string` contains the given pattern. Similar to `.isMatch` but the pattern can match any part of the string.
  74. **Params**
  75. * `str` **{String}**: The string to match.
  76. * `pattern` **{String}**: Glob pattern to use for matching.
  77. * `options` **{Object}**
  78. * `returns` **{Boolean}**: Returns true if the patter matches any part of `str`.
  79. **Example**
  80. ```js
  81. var extglob = require('extglob');
  82. console.log(extglob.contains('aa/bb/cc', '*b'));
  83. //=> true
  84. console.log(extglob.contains('aa/bb/cc', '*d'));
  85. //=> false
  86. ```
  87. ### [.matcher](index.js#L184)
  88. Takes an extglob pattern and returns a matcher function. The returned function takes the string to match as its only argument.
  89. **Params**
  90. * `pattern` **{String}**: Extglob pattern
  91. * `options` **{String}**
  92. * `returns` **{Boolean}**
  93. **Example**
  94. ```js
  95. var extglob = require('extglob');
  96. var isMatch = extglob.matcher('*.!(*a)');
  97. console.log(isMatch('a.a'));
  98. //=> false
  99. console.log(isMatch('a.b'));
  100. //=> true
  101. ```
  102. ### [.create](index.js#L214)
  103. Convert the given `extglob` pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
  104. **Params**
  105. * `str` **{String}**
  106. * `options` **{Object}**
  107. * `returns` **{String}**
  108. **Example**
  109. ```js
  110. var extglob = require('extglob');
  111. console.log(extglob.create('*.!(*a)').output);
  112. //=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
  113. ```
  114. ### [.capture](index.js#L248)
  115. Returns an array of matches captured by `pattern` in `string`, or `null` if the pattern did not match.
  116. **Params**
  117. * `pattern` **{String}**: Glob pattern to use for matching.
  118. * `string` **{String}**: String to match
  119. * `options` **{Object}**: See available [options](#options) for changing how matches are performed
  120. * `returns` **{Boolean}**: Returns an array of captures if the string matches the glob pattern, otherwise `null`.
  121. **Example**
  122. ```js
  123. var extglob = require('extglob');
  124. extglob.capture(pattern, string[, options]);
  125. console.log(extglob.capture('test/*.js', 'test/foo.js'));
  126. //=> ['foo']
  127. console.log(extglob.capture('test/*.js', 'foo/bar.css'));
  128. //=> null
  129. ```
  130. ### [.makeRe](index.js#L281)
  131. Create a regular expression from the given `pattern` and `options`.
  132. **Params**
  133. * `pattern` **{String}**: The pattern to convert to regex.
  134. * `options` **{Object}**
  135. * `returns` **{RegExp}**
  136. **Example**
  137. ```js
  138. var extglob = require('extglob');
  139. var re = extglob.makeRe('*.!(*a)');
  140. console.log(re);
  141. //=> /^[^\/]*?\.(?![^\/]*?a)[^\/]*?$/
  142. ```
  143. ## Options
  144. Available options are based on the options from Bash (and the option names used in bash).
  145. ### options.nullglob
  146. **Type**: `boolean`
  147. **Default**: `undefined`
  148. When enabled, the pattern itself will be returned when no matches are found.
  149. ### options.nonull
  150. Alias for [options.nullglob](#optionsnullglob), included for parity with minimatch.
  151. ### options.cache
  152. **Type**: `boolean`
  153. **Default**: `undefined`
  154. Functions are memoized based on the given glob patterns and options. Disable memoization by setting `options.cache` to false.
  155. ### options.failglob
  156. **Type**: `boolean`
  157. **Default**: `undefined`
  158. Throw an error is no matches are found.
  159. ## Benchmarks
  160. Last run on December 21, 2017
  161. ```sh
  162. # negation-nested (49 bytes)
  163. extglob x 2,228,255 ops/sec ±0.98% (89 runs sampled)
  164. minimatch x 207,875 ops/sec ±0.61% (91 runs sampled)
  165. fastest is extglob (by 1072% avg)
  166. # negation-simple (43 bytes)
  167. extglob x 2,205,668 ops/sec ±1.00% (91 runs sampled)
  168. minimatch x 311,923 ops/sec ±1.25% (91 runs sampled)
  169. fastest is extglob (by 707% avg)
  170. # range-false (57 bytes)
  171. extglob x 2,263,877 ops/sec ±0.40% (94 runs sampled)
  172. minimatch x 271,372 ops/sec ±1.02% (91 runs sampled)
  173. fastest is extglob (by 834% avg)
  174. # range-true (56 bytes)
  175. extglob x 2,161,891 ops/sec ±0.41% (92 runs sampled)
  176. minimatch x 268,265 ops/sec ±1.17% (91 runs sampled)
  177. fastest is extglob (by 806% avg)
  178. # star-simple (46 bytes)
  179. extglob x 2,211,081 ops/sec ±0.49% (92 runs sampled)
  180. minimatch x 343,319 ops/sec ±0.59% (91 runs sampled)
  181. fastest is extglob (by 644% avg)
  182. ```
  183. ## Differences from Bash
  184. This library has complete parity with Bash 4.3 with only a couple of minor differences.
  185. * In some cases Bash returns true if the given string "contains" the pattern, whereas this library returns true if the string is an exact match for the pattern. You can relax this by setting `options.contains` to true.
  186. * This library is more accurate than Bash and thus does not fail some of the tests that Bash 4.3 still lists as failing in their unit tests
  187. ## About
  188. <details>
  189. <summary><strong>Contributing</strong></summary>
  190. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  191. </details>
  192. <details>
  193. <summary><strong>Running Tests</strong></summary>
  194. Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
  195. ```sh
  196. $ npm install && npm test
  197. ```
  198. </details>
  199. <details>
  200. <summary><strong>Building docs</strong></summary>
  201. _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
  202. To generate the readme, run the following command:
  203. ```sh
  204. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  205. ```
  206. </details>
  207. ### Related projects
  208. You might also be interested in these projects:
  209. * [braces](https://www.npmjs.com/package/braces): Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support… [more](https://github.com/micromatch/braces) | [homepage](https://github.com/micromatch/braces "Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support for the Bash 4.3 braces specification, without sacrificing speed.")
  210. * [expand-brackets](https://www.npmjs.com/package/expand-brackets): Expand POSIX bracket expressions (character classes) in glob patterns. | [homepage](https://github.com/jonschlinkert/expand-brackets "Expand POSIX bracket expressions (character classes) in glob patterns.")
  211. * [expand-range](https://www.npmjs.com/package/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used… [more](https://github.com/jonschlinkert/expand-range) | [homepage](https://github.com/jonschlinkert/expand-range "Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used by [micromatch].")
  212. * [fill-range](https://www.npmjs.com/package/fill-range): Fill in a range of numbers or letters, optionally passing an increment or `step` to… [more](https://github.com/jonschlinkert/fill-range) | [homepage](https://github.com/jonschlinkert/fill-range "Fill in a range of numbers or letters, optionally passing an increment or `step` to use, or create a regex-compatible range with `options.toRegex`")
  213. * [micromatch](https://www.npmjs.com/package/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | [homepage](https://github.com/micromatch/micromatch "Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch.")
  214. ### Contributors
  215. | **Commits** | **Contributor** |
  216. | --- | --- |
  217. | 49 | [jonschlinkert](https://github.com/jonschlinkert) |
  218. | 2 | [isiahmeadows](https://github.com/isiahmeadows) |
  219. | 1 | [doowb](https://github.com/doowb) |
  220. | 1 | [devongovett](https://github.com/devongovett) |
  221. | 1 | [mjbvz](https://github.com/mjbvz) |
  222. | 1 | [shinnn](https://github.com/shinnn) |
  223. ### Author
  224. **Jon Schlinkert**
  225. * [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
  226. * [github/jonschlinkert](https://github.com/jonschlinkert)
  227. * [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
  228. ### License
  229. Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
  230. Released under the [MIT License](LICENSE).
  231. ***
  232. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on December 21, 2017._
  233. <hr class="footnotes-sep">
  234. <section class="footnotes">
  235. <ol class="footnotes-list">
  236. <li id="fn1" class="footnote-item">`@` isn "'t a RegEx character." <a href="#fnref1" class="footnote-backref">↩</a>
  237. </li>
  238. </ol>
  239. </section>