Giter VIP home page Giter VIP logo

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
  • 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) `(... ...)?`
*(pattern-list) `(... ...)*`
+(pattern-list) `(... ...)+`
@(pattern-list) `(... ...)` [1]
!(pattern-list) N/A Matches anything except one of the given pattern(s)

API

extglob

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

Example

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

Params

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

.match

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

Example

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

Params

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

.isMatch

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

Example

var extglob = require('extglob');

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

Params

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

.contains

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

Example

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

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.

.matcher

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

Example

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

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

Params

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

.create

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

Example

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

Params

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

.makeRe

Create a regular expression from the given pattern and options.

Example

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

Params

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

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 October 20, 2016

Benchmarking: (5 of 5)
 · negation-nested
 · negation-simple
 · range-false
 · range-true
 · star-simple

# benchmark/fixtures/isMatch/negation-nested.js (49 bytes)
  extglob x 1,988,591 ops/sec ±1.18% (84 runs sampled)
  minimatch x 73,335 ops/sec ±1.38% (84 runs sampled)

  fastest is extglob

# benchmark/fixtures/isMatch/negation-simple.js (43 bytes)
  extglob x 2,320,380 ops/sec ±1.71% (86 runs sampled)
  minimatch x 122,947 ops/sec ±1.28% (86 runs sampled)

  fastest is extglob

# benchmark/fixtures/isMatch/range-false.js (56 bytes)
  extglob x 1,729,572 ops/sec ±1.22% (84 runs sampled)
  minimatch x 112,566 ops/sec ±1.26% (85 runs sampled)

  fastest is extglob

# benchmark/fixtures/isMatch/range-true.js (56 bytes)
  extglob x 1,819,085 ops/sec ±1.28% (83 runs sampled)
  minimatch x 115,153 ops/sec ±1.50% (85 runs sampled)

  fastest is extglob

# benchmark/fixtures/isMatch/star-simple.js (46 bytes)
  extglob x 1,970,063 ops/sec ±1.46% (83 runs sampled)
  minimatch x 138,805 ops/sec ±1.31% (87 runs sampled)

  fastest is extglob

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: Fastest brace expansion for node.js, with the most complete support for the Bash 4.3 braces… 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. See… 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
| | --- | --- | --- | --- | --- | | 32 | jonschlinkert | | 2 | isiahmeadows | | 1 | shinnn |

Building docs

(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)

To generate the readme and API documentation with verb:

$ npm install -g verb verb-generate-readme && verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb-generate-readme, v0.2.0, on October 20, 2016.


  1. `@` isn't a RegEx character.

extglob's People

Contributors

doowb avatar jonschlinkert avatar mjbvz avatar shinnn avatar

Watchers

 avatar  avatar  avatar

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.