Skip to content

Commit

Permalink
document, expose, and test 'partial:true' option
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 9, 2022
1 parent 5dbd6a7 commit 2ff0388
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ Suppress the behavior of treating a leading `!` character as negation.
Returns from negate expressions the same as if they were not negated.
(Ie, true on a hit, false on a miss.)

### partial

Compare a partial path to a pattern. As long as the parts of the path that
are present are not contradicted by the pattern, it will be treated as a
match. This is useful in applications where you're walking through a
folder structure, and don't yet have the full path, but want to ensure that
you do not walk down paths that can never be a match.

For example,

```js
minimatch('/a/b', '/a/*/c/d', { partial: true }) // true, might be /a/b/c/d
minimatch('/a/b', '/**/d', { partial: true }) // true, might be /a/b/.../d
minimatch('/x/y/z', '/a/**/z', { partial: true }) // false, because x !== a
```

## Comparisons to other fnmatch/glob implementations

While strict compliance with the existing standards is a worthwhile
Expand Down
7 changes: 5 additions & 2 deletions minimatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ function Minimatch (pattern, options) {
this.negate = false
this.comment = false
this.empty = false
this.partial = !!options.partial

// make the set of regexps etc.
this.make()
Expand Down Expand Up @@ -719,13 +720,15 @@ minimatch.match = function (list, pattern, options) {
return list
}

Minimatch.prototype.match = function match (f) {
Minimatch.prototype.match = function match (f, partial = this.partial) {
this.debug('match', f, this.pattern)
// short-circuit in the case of busted things.
// comments, etc.
if (this.comment) return false
if (this.empty) return f === ''

if (f === '/' && partial) return true

var options = this.options

// windows: need to use /, not \
Expand Down Expand Up @@ -759,7 +762,7 @@ Minimatch.prototype.match = function match (f) {
if (options.matchBase && pattern.length === 1) {
file = [filename]
}
var hit = this.matchOne(file, pattern, false)
var hit = this.matchOne(file, pattern, partial)
if (hit) {
if (options.flipNegate) return true
return !this.negate
Expand Down
5 changes: 5 additions & 0 deletions test/partial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const t = require('tap')
const mm = require('../')
t.equal(mm('/a/b', '/*/b/x/y/z', { partial: true }), true)
t.equal(mm('/a/b/c', '/*/b/x/y/z', { partial: true }), false)
t.equal(mm('/', 'x', { partial: true }), true)

0 comments on commit 2ff0388

Please sign in to comment.