Giter Site home page Giter Site logo

micromatch / extglob Goto Github PK

View Code? Open in Web Editor NEW
31.0 7.0 11.0 212 KB

Extended globs. Add (almost) the expressive power of regular expressions to glob patterns.

License: MIT License

JavaScript 100.00%
regex regular-expression extglob pattern glob extended-globbing globbing javascript minimatch nodejs

extglob's Introduction

extglob NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

Extended glob support for JavaScript. Adds (almost) the expressive power of regular expressions to glob patterns.

Install

Install with npm:

$ npm install --save extglob

Install with yarn:

$ yarn add extglob
  • Convert an extglob string to a regex-compatible string.
  • More complete (and correct) support than minimatch (minimatch fails a large percentage of the extglob tests)
  • Handles negation patterns
  • Handles nested patterns
  • Organized code base, easy to maintain and make changes when edge cases arise
  • As you can see by the benchmarks, extglob doesn't pay with speed for it's completeness, accuracy and quality.

Heads up!: This library only supports extglobs, to handle full glob patterns and other extended globbing features use micromatch instead.

Usage

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.

var extglob = require('extglob');
console.log(extglob('!(xyz)*.js'));

Extglob cheatsheet

Extended globbing patterns can be defined as follows (as described by the bash man page):

pattern regex equivalent description
?(pattern-list) (...|...)? Matches zero or one occurrence of the given pattern(s)
*(pattern-list) (...|...)* Matches zero or more occurrences of the given pattern(s)
+(pattern-list) (...|...)+ Matches one or more occurrences of the given pattern(s)
@(pattern-list) (...|...) [^1] Matches one of the given pattern(s)
!(pattern-list) N/A Matches anything except one of the given pattern(s)

API

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

Params

  • pattern {String}
  • options {Object}
  • returns {String}

Example

const extglob = require('extglob');
console.log(extglob('*.!(*a)'));
//=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'

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

Params

  • list {Array}: Array of strings to match
  • pattern {String}: Extglob pattern
  • options {Object}
  • returns {Array}: Returns an array of matches

Example

const extglob = require('extglob');
console.log(extglob.match(['a.a', 'a.b', 'a.c'], '*.!(*a)'));
//=> ['a.b', 'a.c']

Returns true if the specified string matches the given extglob pattern.

Params

  • string {String}: String to match
  • pattern {String}: Extglob pattern
  • options {String}
  • returns {Boolean}

Example

const extglob = require('extglob');

console.log(extglob.isMatch('a.a', '*.!(*a)'));
//=> false
console.log(extglob.isMatch('a.b', '*.!(*a)'));
//=> true

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

Params

  • str {String}: The string to match.
  • pattern {String}: Glob pattern to use for matching.
  • options {Object}
  • returns {Boolean}: Returns true if the patter matches any part of str.

Example

const extglob = require('extglob');
console.log(extglob.contains('aa/bb/cc', '*b'));
//=> true
console.log(extglob.contains('aa/bb/cc', '*d'));
//=> false

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

Params

  • pattern {String}: Extglob pattern
  • options {String}
  • returns {Boolean}

Example

const extglob = require('extglob');
const isMatch = extglob.matcher('*.!(*a)');

console.log(isMatch('a.a'));
//=> false
console.log(isMatch('a.b'));
//=> true

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

Params

  • str {String}
  • options {Object}
  • returns {String}

Example

const extglob = require('extglob');
console.log(extglob.create('*.!(*a)').output);
//=> '(?!\\.)[^/]*?\\.(?!(?!\\.)[^/]*?a\\b).*?'

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

Params

  • pattern {String}: Glob pattern to use for matching.
  • string {String}: String to match
  • options {Object}: See available options for changing how matches are performed
  • returns {Boolean}: Returns an array of captures if the string matches the glob pattern, otherwise null.

Example

const extglob = require('extglob');
extglob.capture(pattern, string[, options]);

console.log(extglob.capture('test/*.js', 'test/foo.js'));
//=> ['foo']
console.log(extglob.capture('test/*.js', 'foo/bar.css'));
//=> null

Create a regular expression from the given pattern and options.

Params

  • pattern {String}: The pattern to convert to regex.
  • options {Object}
  • returns {RegExp}

Example

const extglob = require('extglob');
const re = extglob.makeRe('*.!(*a)');
console.log(re);
//=> /^[^\/]*?\.(?![^\/]*?a)[^\/]*?$/

Options

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

options.nullglob

Type: boolean

Default: undefined

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

options.nonull

Alias for options.nullglob, included for parity with minimatch.

options.cache

Type: boolean

Default: undefined

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

options.failglob

Type: boolean

Default: undefined

Throw an error is no matches are found.

Benchmarks

Last run on April 30, 2018

# negation-nested (49 bytes)
  extglob x 1,380,148 ops/sec ±3.35% (62 runs sampled)
  minimatch x 156,800 ops/sec ±4.13% (76 runs sampled)

  fastest is extglob (by 880% avg)

# negation-simple (43 bytes)
  extglob x 1,821,746 ops/sec ±1.61% (76 runs sampled)
  minimatch x 365,618 ops/sec ±1.87% (84 runs sampled)

  fastest is extglob (by 498% avg)

# range-false (57 bytes)
  extglob x 2,038,592 ops/sec ±3.39% (85 runs sampled)
  minimatch x 310,897 ops/sec ±12.62% (87 runs sampled)

  fastest is extglob (by 656% avg)

# range-true (56 bytes)
  extglob x 2,105,081 ops/sec ±0.69% (91 runs sampled)
  minimatch x 332,188 ops/sec ±0.45% (91 runs sampled)

  fastest is extglob (by 634% avg)

# star-simple (46 bytes)
  extglob x 2,154,184 ops/sec ±0.99% (89 runs sampled)
  minimatch x 452,812 ops/sec ±0.51% (88 runs sampled)

  fastest is extglob (by 476% avg)

Differences from Bash

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

  • 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.
  • 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

About

Related projects

  • braces: Bash-like brace expansion, implemented in JavaScript. Safer than other brace expansion libs, with complete support… more | homepage
  • expand-brackets: Expand POSIX bracket expressions (character classes) in glob patterns. | homepage
  • expand-range: Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. Used… more | homepage
  • fill-range: Fill in a range of numbers or letters, optionally passing an increment or step to… more | homepage
  • micromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Contributors

Commits Contributor
54 jonschlinkert
6 danez
2 isiahmeadows
1 doowb
1 devongovett
1 mjbvz
1 shinnn

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

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:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on April 30, 2018.

extglob's People

Contributors

danez avatar devongovett avatar doowb avatar jonschlinkert avatar mjbvz avatar shinnn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

extglob's Issues

!(*.js|*.json) does not match a.js.gz

Hello,

I am using extglob v2.0.2 and node v8.9.0.

As the title states, there is a case where a pattern does not match where I thought it would.

extglob.makeRe("!(*.js|*.json)").test("a.js.gz") === true // KO
extglob.makeRe("!(*.js|*.json)").test("a.json.gz") === true // KO
extglob.makeRe("!(*.js|*.json)").test("a.gz") === true // OK

Should this not match or is it a bug?

questionable patterns

While working on the refactor, these were the only patterns where Bash 4.3 disagreed with the results from this library. (fwiw minimatch fails 37 of the these tests).

I believe extglob is correct on most of these - this is only a few patterns with multiple examples for clarity (for example, IMHO !(foo)* should not match foo). I'd like some feedback on what the "verdict" should be.

edited 2016-10-17: most of these now agree. I realized that (in most of the listed cases) Bash is doing a "contains" match versus an "exact" match. Also, fwiw minimatch agrees with extglob on all cases except for the one minimatch fails on (which extglob and bash agree on). Unfortunately I've not been able to identify a specific reason that bash or minimatch disagree with this library on any cases - but it's clear that extglob is much more accurate than minimatch, and it appears that extglob is also more accurate than bash (it's known that bash still fails some edge cases).

String Pattern Bash Extglob Minimatch Agreed?
foo !(foo)* true true true
bar !(foo)* true true true
baz !(foo)* true true true
foobar !(foo)* true true true
foo *(!(foo)) true false true
moo.cow !(*.*).!(*.*) true true true
mad.moo.cow !(*.*).!(*.*) false true true X
foo.js.js *.!(js) true true true
foo.js.js *.!(js)* true false false X
foo.js.js *.!(js)+ false false false
foo.js.js *.!(js)*.!(js) false false false

Notes

One thing to consider is that whereas in bash, the extglob pattern might be the complete pattern, with this library the pattern is more likely to be a part of a larger glob pattern for testing file paths, etc.

cc @phated @doowb @tunnckoCore

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.