Projekt

Obecné

Profil

Stáhnout (12.3 KB) Statistiky
| Větev: | Revize:
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
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
6

    
7
## Install
8

    
9
Install with [npm](https://www.npmjs.com/):
10

    
11
```sh
12
$ npm install --save extglob
13
```
14

    
15
* Convert an extglob string to a regex-compatible string.
16
* More complete (and correct) support than [minimatch](https://github.com/isaacs/minimatch) (minimatch fails a large percentage of the extglob tests)
17
* Handles [negation patterns](#extglob-patterns)
18
* Handles [nested patterns](#extglob-patterns)
19
* Organized code base, easy to maintain and make changes when edge cases arise
20
* As you can see by the [benchmarks](#benchmarks), extglob doesn't pay with speed for it's completeness, accuracy and quality.
21

    
22
**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.
23

    
24
## Usage
25

    
26
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.
27

    
28
```js
29
var extglob = require('extglob');
30
console.log(extglob('!(xyz)*.js'));
31
```
32

    
33
## Extglob cheatsheet
34

    
35
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)):
36

    
37
| **pattern** | **regex equivalent** | **description** | 
38
| --- | --- | --- |
39
| `?(pattern-list)` | `(...|...)?` | Matches zero or one occurrence of the given pattern(s) |
40
| `*(pattern-list)` | `(...|...)*` | Matches zero or more occurrences of the given pattern(s) |
41
| `+(pattern-list)` | `(...|...)+` | Matches one or more occurrences of the given pattern(s) |
42
| `@(pattern-list)` | `(...|...)` <sup class="footnote-ref"><a href="#fn1" id="fnref1">[1]</a></sup> | Matches one of the given pattern(s) |
43
| `!(pattern-list)` | N/A | Matches anything except one of the given pattern(s) |
44

    
45
## API
46

    
47
### [extglob](index.js#L36)
48

    
49
Convert the given `extglob` pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
50

    
51
**Params**
52

    
53
* `pattern` **{String}**
54
* `options` **{Object}**
55
* `returns` **{String}**
56

    
57
**Example**
58

    
59
```js
60
var extglob = require('extglob');
61
console.log(extglob('*.!(*a)'));
62
//=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
63
```
64

    
65
### [.match](index.js#L56)
66

    
67
Takes an array of strings and an extglob pattern and returns a new array that contains only the strings that match the pattern.
68

    
69
**Params**
70

    
71
* `list` **{Array}**: Array of strings to match
72
* `pattern` **{String}**: Extglob pattern
73
* `options` **{Object}**
74
* `returns` **{Array}**: Returns an array of matches
75

    
76
**Example**
77

    
78
```js
79
var extglob = require('extglob');
80
console.log(extglob.match(['a.a', 'a.b', 'a.c'], '*.!(*a)'));
81
//=> ['a.b', 'a.c']
82
```
83

    
84
### [.isMatch](index.js#L111)
85

    
86
Returns true if the specified `string` matches the given extglob `pattern`.
87

    
88
**Params**
89

    
90
* `string` **{String}**: String to match
91
* `pattern` **{String}**: Extglob pattern
92
* `options` **{String}**
93
* `returns` **{Boolean}**
94

    
95
**Example**
96

    
97
```js
98
var extglob = require('extglob');
99

    
100
console.log(extglob.isMatch('a.a', '*.!(*a)'));
101
//=> false
102
console.log(extglob.isMatch('a.b', '*.!(*a)'));
103
//=> true
104
```
105

    
106
### [.contains](index.js#L150)
107

    
108
Returns true if the given `string` contains the given pattern. Similar to `.isMatch` but the pattern can match any part of the string.
109

    
110
**Params**
111

    
112
* `str` **{String}**: The string to match.
113
* `pattern` **{String}**: Glob pattern to use for matching.
114
* `options` **{Object}**
115
* `returns` **{Boolean}**: Returns true if the patter matches any part of `str`.
116

    
117
**Example**
118

    
119
```js
120
var extglob = require('extglob');
121
console.log(extglob.contains('aa/bb/cc', '*b'));
122
//=> true
123
console.log(extglob.contains('aa/bb/cc', '*d'));
124
//=> false
125
```
126

    
127
### [.matcher](index.js#L184)
128

    
129
Takes an extglob pattern and returns a matcher function. The returned function takes the string to match as its only argument.
130

    
131
**Params**
132

    
133
* `pattern` **{String}**: Extglob pattern
134
* `options` **{String}**
135
* `returns` **{Boolean}**
136

    
137
**Example**
138

    
139
```js
140
var extglob = require('extglob');
141
var isMatch = extglob.matcher('*.!(*a)');
142

    
143
console.log(isMatch('a.a'));
144
//=> false
145
console.log(isMatch('a.b'));
146
//=> true
147
```
148

    
149
### [.create](index.js#L214)
150

    
151
Convert the given `extglob` pattern into a regex-compatible string. Returns an object with the compiled result and the parsed AST.
152

    
153
**Params**
154

    
155
* `str` **{String}**
156
* `options` **{Object}**
157
* `returns` **{String}**
158

    
159
**Example**
160

    
161
```js
162
var extglob = require('extglob');
163
console.log(extglob.create('*.!(*a)').output);
164
//=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'
165
```
166

    
167
### [.capture](index.js#L248)
168

    
169
Returns an array of matches captured by `pattern` in `string`, or `null` if the pattern did not match.
170

    
171
**Params**
172

    
173
* `pattern` **{String}**: Glob pattern to use for matching.
174
* `string` **{String}**: String to match
175
* `options` **{Object}**: See available [options](#options) for changing how matches are performed
176
* `returns` **{Boolean}**: Returns an array of captures if the string matches the glob pattern, otherwise `null`.
177

    
178
**Example**
179

    
180
```js
181
var extglob = require('extglob');
182
extglob.capture(pattern, string[, options]);
183

    
184
console.log(extglob.capture('test/*.js', 'test/foo.js'));
185
//=> ['foo']
186
console.log(extglob.capture('test/*.js', 'foo/bar.css'));
187
//=> null
188
```
189

    
190
### [.makeRe](index.js#L281)
191

    
192
Create a regular expression from the given `pattern` and `options`.
193

    
194
**Params**
195

    
196
* `pattern` **{String}**: The pattern to convert to regex.
197
* `options` **{Object}**
198
* `returns` **{RegExp}**
199

    
200
**Example**
201

    
202
```js
203
var extglob = require('extglob');
204
var re = extglob.makeRe('*.!(*a)');
205
console.log(re);
206
//=> /^[^\/]*?\.(?![^\/]*?a)[^\/]*?$/
207
```
208

    
209
## Options
210

    
211
Available options are based on the options from Bash (and the option names used in bash).
212

    
213
### options.nullglob
214

    
215
**Type**: `boolean`
216

    
217
**Default**: `undefined`
218

    
219
When enabled, the pattern itself will be returned when no matches are found.
220

    
221
### options.nonull
222

    
223
Alias for [options.nullglob](#optionsnullglob), included for parity with minimatch.
224

    
225
### options.cache
226

    
227
**Type**: `boolean`
228

    
229
**Default**: `undefined`
230

    
231
Functions are memoized based on the given glob patterns and options. Disable memoization by setting `options.cache` to false.
232

    
233
### options.failglob
234

    
235
**Type**: `boolean`
236

    
237
**Default**: `undefined`
238

    
239
Throw an error is no matches are found.
240

    
241
## Benchmarks
242

    
243
Last run on December 21, 2017
244

    
245
```sh
246
# negation-nested (49 bytes)
247
  extglob x 2,228,255 ops/sec ±0.98% (89 runs sampled)
248
  minimatch x 207,875 ops/sec ±0.61% (91 runs sampled)
249

    
250
  fastest is extglob (by 1072% avg)
251

    
252
# negation-simple (43 bytes)
253
  extglob x 2,205,668 ops/sec ±1.00% (91 runs sampled)
254
  minimatch x 311,923 ops/sec ±1.25% (91 runs sampled)
255

    
256
  fastest is extglob (by 707% avg)
257

    
258
# range-false (57 bytes)
259
  extglob x 2,263,877 ops/sec ±0.40% (94 runs sampled)
260
  minimatch x 271,372 ops/sec ±1.02% (91 runs sampled)
261

    
262
  fastest is extglob (by 834% avg)
263

    
264
# range-true (56 bytes)
265
  extglob x 2,161,891 ops/sec ±0.41% (92 runs sampled)
266
  minimatch x 268,265 ops/sec ±1.17% (91 runs sampled)
267

    
268
  fastest is extglob (by 806% avg)
269

    
270
# star-simple (46 bytes)
271
  extglob x 2,211,081 ops/sec ±0.49% (92 runs sampled)
272
  minimatch x 343,319 ops/sec ±0.59% (91 runs sampled)
273

    
274
  fastest is extglob (by 644% avg)
275

    
276
```
277

    
278
## Differences from Bash
279

    
280
This library has complete parity with Bash 4.3 with only a couple of minor differences.
281

    
282
* 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.
283
* 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
284

    
285
## About
286

    
287
<details>
288
<summary><strong>Contributing</strong></summary>
289

    
290
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
291

    
292
</details>
293

    
294
<details>
295
<summary><strong>Running Tests</strong></summary>
296

    
297
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:
298

    
299
```sh
300
$ npm install && npm test
301
```
302

    
303
</details>
304
<details>
305
<summary><strong>Building docs</strong></summary>
306

    
307
_(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.)_
308

    
309
To generate the readme, run the following command:
310

    
311
```sh
312
$ npm install -g verbose/verb#dev verb-generate-readme && verb
313
```
314

    
315
</details>
316

    
317
### Related projects
318

    
319
You might also be interested in these projects:
320

    
321
* [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.")
322
* [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.")
323
* [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].")
324
* [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`")
325
* [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.")
326

    
327
### Contributors
328

    
329
| **Commits** | **Contributor** | 
330
| --- | --- |
331
| 49 | [jonschlinkert](https://github.com/jonschlinkert) |
332
| 2 | [isiahmeadows](https://github.com/isiahmeadows) |
333
| 1 | [doowb](https://github.com/doowb) |
334
| 1 | [devongovett](https://github.com/devongovett) |
335
| 1 | [mjbvz](https://github.com/mjbvz) |
336
| 1 | [shinnn](https://github.com/shinnn) |
337

    
338
### Author
339

    
340
**Jon Schlinkert**
341

    
342
* [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert)
343
* [github/jonschlinkert](https://github.com/jonschlinkert)
344
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert)
345

    
346
### License
347

    
348
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert).
349
Released under the [MIT License](LICENSE).
350

    
351
***
352

    
353
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on December 21, 2017._
354

    
355
<hr class="footnotes-sep">
356
<section class="footnotes">
357
<ol class="footnotes-list">
358
<li id="fn1"  class="footnote-item">`@` isn "'t a RegEx character." <a href="#fnref1" class="footnote-backref">↩</a>
359

    
360
</li>
361
</ol>
362
</section>
(2-2/5)