From 2ff038852ec03e85e60e0eb333005c680ac8a543 Mon Sep 17 00:00:00 2001 From: isaacs Date: Wed, 9 Feb 2022 15:59:31 -0800 Subject: [PATCH] document, expose, and test 'partial:true' option --- README.md | 16 ++++++++++++++++ minimatch.js | 7 +++++-- test/partial.js | 5 +++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/partial.js diff --git a/README.md b/README.md index 5b4f9e7a..2e9bd0f4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/minimatch.js b/minimatch.js index 850ad0ee..5aaae5c1 100644 --- a/minimatch.js +++ b/minimatch.js @@ -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() @@ -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 \ @@ -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 diff --git a/test/partial.js b/test/partial.js new file mode 100644 index 00000000..422f6dc6 --- /dev/null +++ b/test/partial.js @@ -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)