path: add `matchesGlob` method · nodejs/node@57b8b8e · GitHub
Skip to content

Commit 57b8b8e

Browse files
avivkellermarco-ippolito
authored andcommitted
path: add matchesGlob method
PR-URL: #52881 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 62f4f6f commit 57b8b8e

3 files changed

Lines changed: 99 additions & 0 deletions

File tree

doc/api/path.md

Lines changed: 23 additions & 0 deletions

lib/path.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ const {
4747
validateString,
4848
} = require('internal/validators');
4949

50+
const {
51+
getLazy,
52+
emitExperimentalWarning,
53+
} = require('internal/util');
54+
55+
const lazyMinimatch = getLazy(() => require('internal/deps/minimatch/index'));
56+
5057
const platformIsWin32 = (process.platform === 'win32');
58+
const platformIsOSX = (process.platform === 'darwin');
5159

5260
function isPathSeparator(code) {
5361
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
@@ -153,6 +161,22 @@ function _format(sep, pathObject) {
153161
return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`;
154162
}
155163

164+
function glob(path, pattern, windows) {
165+
emitExperimentalWarning('glob');
166+
validateString(path, 'path');
167+
validateString(pattern, 'pattern');
168+
return lazyMinimatch().minimatch(path, pattern, {
169+
__proto__: null,
170+
nocase: platformIsOSX || platformIsWin32,
171+
windowsPathsNoEscape: true,
172+
nonegate: true,
173+
nocomment: true,
174+
optimizationLevel: 2,
175+
platform: windows ? 'win32' : 'posix',
176+
nocaseMagicOnly: true,
177+
});
178+
}
179+
156180
const win32 = {
157181
/**
158182
* path.resolve([from ...], to)
@@ -1065,6 +1089,10 @@ const win32 = {
10651089
return ret;
10661090
},
10671091

1092+
matchesGlob(path, pattern) {
1093+
return glob(path, pattern, true);
1094+
},
1095+
10681096
sep: '\\',
10691097
delimiter: ';',
10701098
win32: null,
@@ -1530,6 +1558,10 @@ const posix = {
15301558
return ret;
15311559
},
15321560

1561+
matchesGlob(path, pattern) {
1562+
return glob(path, pattern, false);
1563+
},
1564+
15331565
sep: '/',
15341566
delimiter: ':',
15351567
win32: null,

test/parallel/test-path-glob.js

Lines changed: 44 additions & 0 deletions

0 commit comments

Comments
 (0)