Skip to content

Add nanomatch.capture to return captured matches #4

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

Merged
merged 1 commit into from
Sep 7, 2017

Conversation

devongovett
Copy link
Member

This adds a new method nanomatch.capture, which returns captured matches from dynamic parts of a pattern (e.g. star, globstar). Should partially address micromatch/micromatch#85.

For example:

capture('test/**/*.js', 'test/dir/a.js')
// => ['dir', 'a']

This is very useful for a number of purposes. My usecase is building a nested object with results of a glob. For example, given the path and pattern above:

{dir: {a: 'test/dir/a.js'}}

It works by adding a capture option to the compiler, which causes capturing groups to be added to the generated regex for stars and globstars. This option is off by default, so everything continues working exactly the same for other methods.

I couldn't get the documentation generator to run on my system, so if you could run it that would be great. Thanks for a great library!

@devongovett
Copy link
Member Author

Tests don't appear to be passing on windows already... Not sure about that.

If you like this PR, I'll go ahead and add the capture method to micromatch as well, and update extglob with support for capture groups as well.

index.js Outdated
return null;
}

return match.slice(1).filter(Boolean);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.filter(Boolean) removes the empty captures/strings, right?
Doesn't that make this API hard to use?

For instance: given a/**/c/**/f.txt then both paths below result in the same capture ["x"]:

  • a/x/c/f.txt
  • a/c/x/f.txt

It's impossible to rebuild the matching path from the capture.
I you kept empty strings, then the first would capture ["x", ""] and the second ["", "x"]. The matching paths are very easy to rebuild.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. This made sense for my use case (described above), but I can see it being weird for other cases. I'll remove the filter here and move it to my code.

Copy link
Member

@jonschlinkert jonschlinkert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! I like the simplicity of the code, nice work. Great all around PR.

Tests don't appear to be passing on windows already... Not sure about that.

Looks like one specific test if failing. I'm not sure why, but I'll look into it.

I couldn't get the documentation generator to run on my system, so if you could run it that would be great. Thanks for a great library!

No problem. I'll take care of it.

If you like this PR, I'll go ahead and add the capture method to micromatch as well, and update extglob with support for capture groups as well.

Yes, I do and that would be great! thanks!

index.js Outdated
* @api public
*/

nanomatch.capture = function(pattern, string, options) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a little thing, but could you change string to str to be consistent with other methods?

index.js Outdated
var unixify = utils.unixify(options);

function match() {
return function(str) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and change this to string (this is probably why you did the other as string. noted, and thanks for indulging my OCD)

@devongovett
Copy link
Member Author

@jonschlinkert updated!

@jonschlinkert
Copy link
Member

@devongovett great work, perfect PR all around! I sent you an invite to join the org. Thank you!

jonschlinkert added a commit to base-repos/base that referenced this pull request Sep 8, 2017
@jonschlinkert
Copy link
Member

I accidentally pushed this up as a patch, since I was focused on a bugfix and I forgot I merged this in first. However, there shouldn't be anything breaking, it's really straight forward, and I tested extensively with micromatch and all tests pass.

@devongovett
Copy link
Member Author

@jonschlinkert awesome, thanks. I think it should be ok. I tested with micromatch as well, and added all of the nanomatch/extglob tests that I added to micromatch as well: micromatch/micromatch#105.

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

Successfully merging this pull request may close these issues.

3 participants