Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to split patterns into segments #165

Open
mrmlnc opened this issue May 30, 2019 · 8 comments
Open

Ability to split patterns into segments #165

mrmlnc opened this issue May 30, 2019 · 8 comments

Comments

@mrmlnc
Copy link
Contributor

mrmlnc commented May 30, 2019

Hi, @jonschlinkert,

I don't know how correct it is to talk about this, so I think we should take this issue as a question.

📖 The minimatch package has a .set method that allows to get pattern segments.

I need to split the pattern into segments by slashes (not a maybe_slash). This is necessary in order to check whether the first part of the path matches the pattern or not. To decide whether to read this directory or not.

In the following example we should match the root/dir1 directory and shouldn't the root/dir2:

root/+(dir1|dir2)/one/**/*
root/dir1/one/…
root/dir1/two/…
root/dir2/…

But I cannot apply the pattern to root/dir1 or root/dir2 entry, because the pattern expects one after root/dir1 and will always return false.

Maybe you have some ideas?

Related to mrmlnc/fast-glob#156.

@jonschlinkert
Copy link
Member

I don't know how correct it is to talk about this, so I think we should take this issue as a question.

Of course! you can here anytime. I'll look at the related issue and play around with it and let you know what I come up with.

@jonschlinkert
Copy link
Member

@mrmlnc it looks like fast-glob 2.2.7 is published to npm as the latest, which is from this branch: https://github.com/mrmlnc/fast-glob/blob/2.x.x/package.json, and it's using micromatch 3.x. Were you having issues with micromatch 4.0? Anything I can help with?

The parser in micromatch v4.0 can split the pattern the way you want to (assuming I understand you correctly).

@jonschlinkert
Copy link
Member

[paraphrased] In root/+(dir1|dir2)/one/**/* we should match the root/dir1 directory and shouldn't the root/dir2

I think you mean: root/dir1/one and root/dir2/one should match, but NOT root/dir1/two and root/dir2/two. Correct?

because the pattern expects one after root/dir1 and will always return false.

Isn't that correct? Meaning, I think the bug is that fast-glob actually is matching two, but it should not.

I might be confused, thanks for being patient with me.

@mrmlnc
Copy link
Contributor Author

mrmlnc commented May 30, 2019

Yeap, fast-glob@2.2.7 shipped with micromatch@3. The master branch built on micromatch@4.

You say that micromatch@4 can split the pattern into segments? How I can do it? I must use the .scan method or… (I don't see .split method or something like it).

To clarify. I want split the pattern one/two by slashes and then apply their one by one:

Pattern: one/two
Segments: [one, two]
Process:

one — I will trying to apply first segment
one/two — I will trying to apply second segment to the second part of path (two)

For more complex example you can check the node-glob package (around minimatch.set):

https://github.com/isaacs/node-glob/blob/master/sync.js

Example with minimatch

const { Minimatch } = require('minimatch');

const pattern = new Minimatch('root/+(dir1|dir2)/one/**/*');

console.dir(pattern, { colors: true });
Minimatch {
  options: {},
  set: [
    [
      'root',
      /^(?!\.)(?=.)(?:dir1|dir2)+$/,
      'one',
      {},
      /^(?!\.)(?=.)[^\/]*?$/
    ]
  ],
  pattern: 'root/+(dir1|dir2)/one/**/*',
  regexp: null,
  negate: false,
  comment: false,
  empty: false,
  globSet: [ 'root/+(dir1|dir2)/one/**/*' ],
  globParts: [ [ 'root', '+(dir1|dir2)', 'one', '**', '*' ] ]
}

@jonschlinkert
Copy link
Member

You say that micromatch@4 can split the pattern into segments? How I can do it?

Oh man, I'm sorry I forgot that I haven't published that code yet. I was thinking it was in 2.0. Let me push it up to a branch on picomatch, and if you have a chance let me know if it does what you need.

@jonschlinkert
Copy link
Member

jonschlinkert commented May 30, 2019

@mrmlnc I just pushed up https://github.com/micromatch/picomatch/tree/dev, but it doesn't have the feature you're looking for. I can't find it!

@doowb, do you remember where that logic was that I showed you for splitting globs?

edit: nvm, I remember where it is... I'll see how long it will take me to get it integrated into the dev branch.

@jonschlinkert
Copy link
Member

Okay, I updated the branch with the code I was working on for splitting globs. Sorry, it's not ready to release, but you can see that I was working on several different approaches to try to get it right. If you want to play around with them and give me feedback that would be great. If not, no worries, I just try to get something published soon either way.

@doowb
Copy link
Member

doowb commented May 30, 2019

@doowb, do you remember where that logic was that I showed you for splitting globs?

I'm glad you found it because this is an awesome feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants