| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -279,6 +279,29 @@ path.format({ | |||
| 279 | 279 | // Returns: 'C:\\path\\dir\\file.txt' | |
| 280 | 280 | ``` | |
| 281 | 281 | ||
| 282 | + ## `path.matchesGlob(path, pattern)` | ||
| 283 | + | ||
| 284 | + <!-- YAML | ||
| 285 | + added: REPLACEME | ||
| 286 | + --> | ||
| 287 | + | ||
| 288 | + > Stability: 1 - Experimental | ||
| 289 | + | ||
| 290 | + * `path` {string} The path to glob-match against. | ||
| 291 | + * `pattern` {string} The glob to check the path against. | ||
| 292 | + * Returns: {boolean} Whether or not the `path` matched the `pattern`. | ||
| 293 | + | ||
| 294 | + The `path.matchesGlob()` method determines if `path` matches the `pattern`. | ||
| 295 | + | ||
| 296 | + For example: | ||
| 297 | + | ||
| 298 | + ```js | ||
| 299 | + path.matchesGlob('/foo/bar', '/foo/*'); // true | ||
| 300 | + path.matchesGlob('/foo/bar*', 'foo/bird'); // false | ||
| 301 | + ``` | ||
| 302 | + | ||
| 303 | + A [`TypeError`][] is thrown if `path` or `pattern` are not strings. | ||
| 304 | + | ||
| 282 | 305 | ## `path.isAbsolute(path)` | |
| 283 | 306 | ||
| 284 | 307 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,7 +47,15 @@ const { | |||
| 47 | 47 | validateString, | |
| 48 | 48 | } = require('internal/validators'); | |
| 49 | 49 | ||
| 50 | + const { | ||
| 51 | + getLazy, | ||
| 52 | + emitExperimentalWarning, | ||
| 53 | + } = require('internal/util'); | ||
| 54 | + | ||
| 55 | + const lazyMinimatch = getLazy(() => require('internal/deps/minimatch/index')); | ||
| 56 | + | ||
| 50 | 57 | const platformIsWin32 = (process.platform === 'win32'); | |
| 58 | + const platformIsOSX = (process.platform === 'darwin'); | ||
| 51 | 59 | ||
| 52 | 60 | function isPathSeparator(code) { | |
| 53 | 61 | return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH; | |
@@ -153,6 +161,22 @@ function _format(sep, pathObject) { | |||
| 153 | 161 | return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`; | |
| 154 | 162 | } | |
| 155 | 163 | ||
| 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 | + | ||
| 156 | 180 | const win32 = { | |
| 157 | 181 | /** | |
| 158 | 182 | * path.resolve([from ...], to) | |
@@ -1065,6 +1089,10 @@ const win32 = { | |||
| 1065 | 1089 | return ret; | |
| 1066 | 1090 | }, | |
| 1067 | 1091 | ||
| 1092 | + matchesGlob(path, pattern) { | ||
| 1093 | + return glob(path, pattern, true); | ||
| 1094 | + }, | ||
| 1095 | + | ||
| 1068 | 1096 | sep: '\\', | |
| 1069 | 1097 | delimiter: ';', | |
| 1070 | 1098 | win32: null, | |
@@ -1530,6 +1558,10 @@ const posix = { | |||
| 1530 | 1558 | return ret; | |
| 1531 | 1559 | }, | |
| 1532 | 1560 | ||
| 1561 | + matchesGlob(path, pattern) { | ||
| 1562 | + return glob(path, pattern, false); | ||
| 1563 | + }, | ||
| 1564 | + | ||
| 1533 | 1565 | sep: '/', | |
| 1534 | 1566 | delimiter: ':', | |
| 1535 | 1567 | win32: null, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,44 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const path = require('path'); | ||
| 6 | + | ||
| 7 | + const globs = { | ||
| 8 | + win32: [ | ||
| 9 | + ['foo\\bar\\baz', 'foo\\[bcr]ar\\baz', true], // Matches 'bar' or 'car' in 'foo\\bar' | ||
| 10 | + ['foo\\bar\\baz', 'foo\\[!bcr]ar\\baz', false], // Matches anything except 'bar' or 'car' in 'foo\\bar' | ||
| 11 | + ['foo\\bar\\baz', 'foo\\[bc-r]ar\\baz', true], // Matches 'bar' or 'car' using range in 'foo\\bar' | ||
| 12 | + ['foo\\bar\\baz', 'foo\\*\\!bar\\*\\baz', false], // Matches anything with 'foo' and 'baz' but not 'bar' in between | ||
| 13 | + ['foo\\bar1\\baz', 'foo\\bar[0-9]\\baz', true], // Matches 'bar' followed by any digit in 'foo\\bar1' | ||
| 14 | + ['foo\\bar5\\baz', 'foo\\bar[0-9]\\baz', true], // Matches 'bar' followed by any digit in 'foo\\bar5' | ||
| 15 | + ['foo\\barx\\baz', 'foo\\bar[a-z]\\baz', true], // Matches 'bar' followed by any lowercase letter in 'foo\\barx' | ||
| 16 | + ['foo\\bar\\baz\\boo', 'foo\\[bc-r]ar\\baz\\*', true], // Matches 'bar' or 'car' in 'foo\\bar' | ||
| 17 | + ['foo\\bar\\baz', 'foo/**', true], // Matches anything in 'foo' | ||
| 18 | + ['foo\\bar\\baz', '*', false], // No match | ||
| 19 | + ], | ||
| 20 | + posix: [ | ||
| 21 | + ['foo/bar/baz', 'foo/[bcr]ar/baz', true], // Matches 'bar' or 'car' in 'foo/bar' | ||
| 22 | + ['foo/bar/baz', 'foo/[!bcr]ar/baz', false], // Matches anything except 'bar' or 'car' in 'foo/bar' | ||
| 23 | + ['foo/bar/baz', 'foo/[bc-r]ar/baz', true], // Matches 'bar' or 'car' using range in 'foo/bar' | ||
| 24 | + ['foo/bar/baz', 'foo/*/!bar/*/baz', false], // Matches anything with 'foo' and 'baz' but not 'bar' in between | ||
| 25 | + ['foo/bar1/baz', 'foo/bar[0-9]/baz', true], // Matches 'bar' followed by any digit in 'foo/bar1' | ||
| 26 | + ['foo/bar5/baz', 'foo/bar[0-9]/baz', true], // Matches 'bar' followed by any digit in 'foo/bar5' | ||
| 27 | + ['foo/barx/baz', 'foo/bar[a-z]/baz', true], // Matches 'bar' followed by any lowercase letter in 'foo/barx' | ||
| 28 | + ['foo/bar/baz/boo', 'foo/[bc-r]ar/baz/*', true], // Matches 'bar' or 'car' in 'foo/bar' | ||
| 29 | + ['foo/bar/baz', 'foo/**', true], // Matches anything in 'foo' | ||
| 30 | + ['foo/bar/baz', '*', false], // No match | ||
| 31 | + ], | ||
| 32 | + }; | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + for (const [platform, platformGlobs] of Object.entries(globs)) { | ||
| 36 | + for (const [pathStr, glob, expected] of platformGlobs) { | ||
| 37 | + const actual = path[platform].matchesGlob(pathStr, glob); | ||
| 38 | + assert.strictEqual(actual, expected, `Expected ${pathStr} to ` + (expected ? '' : 'not ') + `match ${glob} on ${platform}`); | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + // Test for non-string input | ||
| 43 | + assert.throws(() => path.matchesGlob(123, 'foo/bar/baz'), /.*must be of type string.*/); | ||
| 44 | + assert.throws(() => path.matchesGlob('foo/bar/baz', 123), /.*must be of type string.*/); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments