micromatch / nanomatch

Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch, but without support for extended globs (extglobs), posix brackets or braces, and with complete Bash 4.3 wildcard support: ("*", "**", and "?").
https://github.com/micromatch
MIT License
95 stars 20 forks source link

Add nanomatch.capture to return captured matches #4

Closed devongovett closed 7 years ago

devongovett commented 7 years ago

This adds a new method nanomatch.capture, which returns captured matches from dynamic parts of a pattern (e.g. star, globstar). Should partially address https://github.com/micromatch/micromatch/issues/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 commented 7 years ago

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.

devongovett commented 7 years ago

@jonschlinkert updated!

jonschlinkert commented 7 years ago

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

jonschlinkert commented 7 years ago

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 commented 7 years ago

@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: https://github.com/micromatch/micromatch/pull/105.