UNPKG

12 kBMarkdownView Raw
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
3> Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob patterns.
4
5## Install
6
7Install with [npm](https://www.npmjs.com/):
8
9```sh
10$ npm install --save extglob
11```
12
13Install with [yarn](https://yarnpkg.com):
14
15```sh
16$ yarn add extglob
17```
18
19* Convert an extglob string to a regex-compatible string.
20* More complete (and correct) support than [minimatch](https://github.com/isaacs/minimatch) (minimatch fails a large percentage of the extglob tests)
21* Handles [negation patterns](#extglob-patterns)
22* Handles [nested patterns](#extglob-patterns)
23* Organized code base, easy to maintain and make changes when edge cases arise
24* As you can see by the [benchmarks](#benchmarks), extglob doesn't pay with speed for it's completeness, accuracy and quality.
25
26**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.
27
28## Usage
29
30The 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.
31
32```js
33var extglob = require('extglob');
34console.log(extglob('!(xyz)*.js'));
35```
36
37## Extglob cheatsheet
38
39Extended 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)):
40
41| **pattern** | **regex equivalent** | **description** |
42| --- | --- | --- |
43| `?(pattern-list)` | `(...\|...)?` | Matches zero or one occurrence of the given pattern(s) |
44| `*(pattern-list)` | `(...\|...)*` | Matches zero or more occurrences of the given pattern(s) |
45| `+(pattern-list)` | `(...\|...)+` | Matches one or more occurrences of the given pattern(s) |
46| `@(pattern-list)` | `(...\|...)` [^1] | Matches one of the given pattern(s) |
47| `!(pattern-list)` | N/A | Matches anything except one of the given pattern(s) |
48
49## API
50
51### [extglob](index.js#L35)
52
53Convert the given `extglob` pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
54
55**Params**
56
57* `pattern` **{String}**
58* `options` **{Object}**
59* `returns` **{String}**
60
61**Example**
62
63```js
64const extglob = require('extglob');
65console.log(extglob('*.!(*a)'));
66//=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
67```
68
69### [.match](index.js#L55)
70
71Takes an array of strings and an extglob pattern and returns a new array that contains only the strings that match the pattern.
72
73**Params**
74
75* `list` **{Array}**: Array of strings to match
76* `pattern` **{String}**: Extglob pattern
77* `options` **{Object}**
78* `returns` **{Array}**: Returns an array of matches
79
80**Example**
81
82```js
83const extglob = require('extglob');
84console.log(extglob.match(['a.a', 'a.b', 'a.c'], '*.!(*a)'));
85//=> ['a.b', 'a.c']
86```
87
88### [.isMatch](index.js#L110)
89
90Returns true if the specified `string` matches the given extglob `pattern`.
91
92**Params**
93
94* `string` **{String}**: String to match
95* `pattern` **{String}**: Extglob pattern
96* `options` **{String}**
97* `returns` **{Boolean}**
98
99**Example**
100
101```js
102const extglob = require('extglob');
103
104console.log(extglob.isMatch('a.a', '*.!(*a)'));
105//=> false
106console.log(extglob.isMatch('a.b', '*.!(*a)'));
107//=> true
108```
109
110### [.contains](index.js#L149)
111
112Returns true if the given `string` contains the given pattern. Similar to `.isMatch` but the pattern can match any part of the string.
113
114**Params**
115
116* `str` **{String}**: The string to match.
117* `pattern` **{String}**: Glob pattern to use for matching.
118* `options` **{Object}**
119* `returns` **{Boolean}**: Returns true if the patter matches any part of `str`.
120
121**Example**
122
123```js
124const extglob = require('extglob');
125console.log(extglob.contains('aa/bb/cc', '*b'));
126//=> true
127console.log(extglob.contains('aa/bb/cc', '*d'));
128//=> false
129```
130
131### [.matcher](index.js#L183)
132
133Takes an extglob pattern and returns a matcher function. The returned function takes the string to match as its only argument.
134
135**Params**
136
137* `pattern` **{String}**: Extglob pattern
138* `options` **{String}**
139* `returns` **{Boolean}**
140
141**Example**
142
143```js
144const extglob = require('extglob');
145const isMatch = extglob.matcher('*.!(*a)');
146
147console.log(isMatch('a.a'));
148//=> false
149console.log(isMatch('a.b'));
150//=> true
151```
152
153### [.create](index.js#L213)
154
155Convert the given `extglob` pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
156
157**Params**
158
159* `str` **{String}**
160* `options` **{Object}**
161* `returns` **{String}**
162
163**Example**
164
165```js
166const extglob = require('extglob');
167console.log(extglob.create('*.!(*a)').output);
168//=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
169```
170
171### [.capture](index.js#L247)
172
173Returns an array of matches captured by `pattern` in `string`, or `null` if the pattern did not match.
174
175**Params**
176
177* `pattern` **{String}**: Glob pattern to use for matching.
178* `string` **{String}**: String to match
179* `options` **{Object}**: See available [options](#options) for changing how matches are performed
180* `returns` **{Boolean}**: Returns an array of captures if the string matches the glob pattern, otherwise `null`.
181
182**Example**
183
184```js
185const extglob = require('extglob');
186extglob.capture(pattern, string[, options]);
187
188console.log(extglob.capture('test/*.js', 'test/foo.js'));
189//=> ['foo']
190console.log(extglob.capture('test/*.js', 'foo/bar.css'));
191//=> null
192```
193
194### [.makeRe](index.js#L280)
195
196Create a regular expression from the given `pattern` and `options`.
197
198**Params**
199
200* `pattern` **{String}**: The pattern to convert to regex.
201* `options` **{Object}**
202* `returns` **{RegExp}**
203
204**Example**
205
206```js
207const extglob = require('extglob');
208const re = extglob.makeRe('*.!(*a)');
209console.log(re);
210//=> /^[^\/]*?\.(?![^\/]*?a)[^\/]*?$/
211```
212
213## Options
214
215Available options are based on the options from Bash (and the option names used in bash).
216
217### options.nullglob
218
219**Type**: `boolean`
220
221**Default**: `undefined`
222
223When enabled, the pattern itself will be returned when no matches are found.
224
225### options.nonull
226
227Alias for [options.nullglob](#optionsnullglob), included for parity with minimatch.
228
229### options.cache
230
231**Type**: `boolean`
232
233**Default**: `undefined`
234
235Functions are memoized based on the given glob patterns and options. Disable memoization by setting `options.cache` to false.
236
237### options.failglob
238
239**Type**: `boolean`
240
241**Default**: `undefined`
242
243Throw an error is no matches are found.
244
245## Benchmarks
246
247Last run on April 30, 2018
248
249```sh
250# negation-nested (49 bytes)
251 extglob x 1,380,148 ops/sec ±3.35% (62 runs sampled)
252 minimatch x 156,800 ops/sec ±4.13% (76 runs sampled)
253
254 fastest is extglob (by 880% avg)
255
256# negation-simple (43 bytes)
257 extglob x 1,821,746 ops/sec ±1.61% (76 runs sampled)
258 minimatch x 365,618 ops/sec ±1.87% (84 runs sampled)
259
260 fastest is extglob (by 498% avg)
261
262# range-false (57 bytes)
263 extglob x 2,038,592 ops/sec ±3.39% (85 runs sampled)
264 minimatch x 310,897 ops/sec ±12.62% (87 runs sampled)
265
266 fastest is extglob (by 656% avg)
267
268# range-true (56 bytes)
269 extglob x 2,105,081 ops/sec ±0.69% (91 runs sampled)
270 minimatch x 332,188 ops/sec ±0.45% (91 runs sampled)
271
272 fastest is extglob (by 634% avg)
273
274# star-simple (46 bytes)
275 extglob x 2,154,184 ops/sec ±0.99% (89 runs sampled)
276 minimatch x 452,812 ops/sec ±0.51% (88 runs sampled)
277
278 fastest is extglob (by 476% avg)
279
280```
281
282## Differences from Bash
283
284This library has complete parity with Bash 4.3 with only a couple of minor differences.
285
286* 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.
287* 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
288
289## About
290
291### Related projects
292
293* [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.")
294* [expand-brackets](https://www.npmjs.com/package/expand-brackets): Expand POSIX bracket expressions (character classes) in glob patterns. | [homepage](https://github.com/micromatch/expand-brackets "Expand POSIX bracket expressions (character classes) in glob patterns.")
295* [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].")
296* [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`")
297* [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.")
298
299### Contributing
300
301Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
302
303### Contributors
304
305| **Commits** | **Contributor** |
306| --- | --- |
307| 54 | [jonschlinkert](https://github.com/jonschlinkert) |
308| 6 | [danez](https://github.com/danez) |
309| 2 | [isiahmeadows](https://github.com/isiahmeadows) |
310| 1 | [doowb](https://github.com/doowb) |
311| 1 | [devongovett](https://github.com/devongovett) |
312| 1 | [mjbvz](https://github.com/mjbvz) |
313| 1 | [shinnn](https://github.com/shinnn) |
314
315### Building docs
316
317_(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.)_
318
319To generate the readme, run the following command:
320
321```sh
322$ npm install -g verbose/verb#dev verb-generate-readme && verb
323```
324
325### Running tests
326
327Running 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:
328
329```sh
330$ npm install && npm test
331```
332
333### Author
334
335**Jon Schlinkert**
336
337* [github/jonschlinkert](https://github.com/jonschlinkert)
338* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
339
340### License
341
342Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
343Released under the [MIT License](LICENSE).
344
345***
346
347_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on April 30, 2018._
\No newline at end of file