Code Monkey home page Code Monkey logo

Comments (8)

jonschlinkert avatar jonschlinkert commented on June 12, 2024

Hi, thanks for creating an issue.

Should this not match or is it a bug?

Please add a comparison of how bash handles these extglob patterns versus this library

from extglob.

smbape avatar smbape commented on June 12, 2024

Hello, according to bash 4.4.12, it is a bug.

shopt -s extglob
touch a.js a.js.gz a.json a.json.gz a.gz
ls !(*.js|*.json) # a.gz  a.js.gz  a.json.gz

extglob will only match a.gz

from extglob.

phated avatar phated commented on June 12, 2024

That seems like such incorrect behavior...

from extglob.

jonschlinkert avatar jonschlinkert commented on June 12, 2024

That seems like such incorrect behavior...

which result?

from extglob.

jonschlinkert avatar jonschlinkert commented on June 12, 2024

do the following instead:

console.log(extglob.makeRe('*.!(js|json)').test('a.js.gz')); // true
console.log(extglob.makeRe('*.!(js|json)').test('a.json.gz')); // true
console.log(extglob.makeRe('*.!(js|json)').test('a.gz')); // true

Even bash yields incorrect results sometimes when globs are nested inside negation globs. In javascript, we can't do negative lookbehinds in regular expressions, so it becomes very difficult or impossible to retroactively un-negate or un-match a substring.

edit: also, I forgot to mention that technically bash in this case is performing a "contains" match, rather than an exact match. This library is stricter in that regard. You can also use your original patterns with options.contains:

console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.js.gz')); // true
console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.json.gz')); // true
console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.gz')); // true

see https://github.com/micromatch/extglob#differences-from-bash

from extglob.

jonschlinkert avatar jonschlinkert commented on June 12, 2024

btw, on a totally unrelated note, if you happen to notice this table being corrupted, that's a bug in github's markdown parser.

from extglob.

smbape avatar smbape commented on June 12, 2024

Hi, thanks for the comments.

Indeed it is hard without negative lookbehinds.
The contains option seems to be an alternative but matches things I really don't want to match.

console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.js')); // true, but I want it to be false
console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.js.gz')); // true
console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.json.gz')); // true
console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.gz')); // true

I also tried to use the *.!(js|json) form in the past, but *.!(js|json) did not match files without extensions.

console.log(extglob.makeRe('*.!(js|json)').test('a')); // false

I will look for a solution with 2 extglobs.

from extglob.

jonschlinkert avatar jonschlinkert commented on June 12, 2024

I'm thinking this looks like a bug then. I try to stick to bash behavior, but sometimes bash is unable to handle certain patterns. In this case, it seems like bash is doing the right thing.

edit: fwiw, here is what I think the results should be for all of the patterns you gave:

console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.js')); // false
console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.js.gz')); // true
console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.json.gz')); // true
console.log(extglob.makeRe('!(*.js|*.json)', {contains: true}).test('a.gz')); // true
console.log(extglob.makeRe('*.!(js|json)').test('a')); // false

I agree with your opinion on the first one, it seems like that should be false.

The last one is correct because the pattern has *. which means that a dot is expected in the path before the negation even happens.

from extglob.

Related Issues (6)

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.