Skip to content

Commit 2ff0388

Browse files
committed
document, expose, and test 'partial:true' option
1 parent 5dbd6a7 commit 2ff0388

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@ Suppress the behavior of treating a leading `!` character as negation.
171171
Returns from negate expressions the same as if they were not negated.
172172
(Ie, true on a hit, false on a miss.)
173173

174+
### partial
175+
176+
Compare a partial path to a pattern. As long as the parts of the path that
177+
are present are not contradicted by the pattern, it will be treated as a
178+
match. This is useful in applications where you're walking through a
179+
folder structure, and don't yet have the full path, but want to ensure that
180+
you do not walk down paths that can never be a match.
181+
182+
For example,
183+
184+
```js
185+
minimatch('/a/b', '/a/*/c/d', { partial: true }) // true, might be /a/b/c/d
186+
minimatch('/a/b', '/**/d', { partial: true }) // true, might be /a/b/.../d
187+
minimatch('/x/y/z', '/a/**/z', { partial: true }) // false, because x !== a
188+
```
189+
174190
## Comparisons to other fnmatch/glob implementations
175191

176192
While strict compliance with the existing standards is a worthwhile

minimatch.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ function Minimatch (pattern, options) {
146146
this.negate = false
147147
this.comment = false
148148
this.empty = false
149+
this.partial = !!options.partial
149150

150151
// make the set of regexps etc.
151152
this.make()
@@ -719,13 +720,15 @@ minimatch.match = function (list, pattern, options) {
719720
return list
720721
}
721722

722-
Minimatch.prototype.match = function match (f) {
723+
Minimatch.prototype.match = function match (f, partial = this.partial) {
723724
this.debug('match', f, this.pattern)
724725
// short-circuit in the case of busted things.
725726
// comments, etc.
726727
if (this.comment) return false
727728
if (this.empty) return f === ''
728729

730+
if (f === '/' && partial) return true
731+
729732
var options = this.options
730733

731734
// windows: need to use /, not \
@@ -759,7 +762,7 @@ Minimatch.prototype.match = function match (f) {
759762
if (options.matchBase && pattern.length === 1) {
760763
file = [filename]
761764
}
762-
var hit = this.matchOne(file, pattern, false)
765+
var hit = this.matchOne(file, pattern, partial)
763766
if (hit) {
764767
if (options.flipNegate) return true
765768
return !this.negate

test/partial.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const t = require('tap')
2+
const mm = require('../')
3+
t.equal(mm('/a/b', '/*/b/x/y/z', { partial: true }), true)
4+
t.equal(mm('/a/b/c', '/*/b/x/y/z', { partial: true }), false)
5+
t.equal(mm('/', 'x', { partial: true }), true)

0 commit comments

Comments
 (0)