micromatch icon indicating copy to clipboard operation
micromatch copied to clipboard

Ability to split patterns into segments

Open mrmlnc opened this issue 6 years ago • 8 comments

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.

:book: 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 https://github.com/mrmlnc/fast-glob/issues/156.

mrmlnc avatar May 30 '19 06:05 mrmlnc

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 avatar May 30 '19 06:05 jonschlinkert

@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 avatar May 30 '19 07:05 jonschlinkert

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

jonschlinkert avatar May 30 '19 07:05 jonschlinkert

Yeap, [email protected] 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', '**', '*' ] ]
}

mrmlnc avatar May 30 '19 07:05 mrmlnc

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 avatar May 30 '19 07:05 jonschlinkert

@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 avatar May 30 '19 08:05 jonschlinkert

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.

jonschlinkert avatar May 30 '19 08:05 jonschlinkert

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

doowb avatar May 30 '19 13:05 doowb