|
1 | 1 | import * as pathModule from 'path';
|
2 |
| -import { PathLike } from './types/misc'; |
| 2 | +import {toRegex} from 'glob-to-regex.js'; |
3 | 3 | import { IGlobOptions } from './types/options';
|
4 | 4 | import { pathToFilename } from './util';
|
5 | 5 | import Dirent from './Dirent';
|
6 | 6 |
|
7 |
| -const { sep, join, relative, resolve } = pathModule.posix; |
8 |
| - |
9 |
| -/** |
10 |
| - * Convert a glob pattern to a regular expression |
11 |
| - * Supports: *, ?, **, [abc], [!abc], [a-z] |
12 |
| - */ |
13 |
| -function globToRegex(pattern: string): RegExp { |
14 |
| - let regexStr = ''; |
15 |
| - let i = 0; |
16 |
| - |
17 |
| - while (i < pattern.length) { |
18 |
| - const char = pattern[i]; |
19 |
| - |
20 |
| - switch (char) { |
21 |
| - case '*': |
22 |
| - if (pattern[i + 1] === '*') { |
23 |
| - // Handle ** |
24 |
| - if (pattern[i + 2] === '/' || i + 2 === pattern.length) { |
25 |
| - regexStr += '(?:.*\\/)?'; // Match zero or more directories |
26 |
| - i += pattern[i + 2] === '/' ? 3 : 2; |
27 |
| - } else { |
28 |
| - regexStr += '[^/]*'; // Single * |
29 |
| - i++; |
30 |
| - } |
31 |
| - } else { |
32 |
| - regexStr += '[^/]*'; // Single * |
33 |
| - i++; |
34 |
| - } |
35 |
| - break; |
36 |
| - case '?': |
37 |
| - regexStr += '[^/]'; |
38 |
| - i++; |
39 |
| - break; |
40 |
| - case '[': |
41 |
| - regexStr += '['; |
42 |
| - i++; |
43 |
| - if (i < pattern.length && pattern[i] === '!') { |
44 |
| - regexStr += '^'; |
45 |
| - i++; |
46 |
| - } |
47 |
| - while (i < pattern.length && pattern[i] !== ']') { |
48 |
| - if (pattern[i] === '\\') { |
49 |
| - regexStr += '\\\\'; |
50 |
| - i++; |
51 |
| - } |
52 |
| - regexStr += pattern[i]; |
53 |
| - i++; |
54 |
| - } |
55 |
| - regexStr += ']'; |
56 |
| - i++; |
57 |
| - break; |
58 |
| - case '.': |
59 |
| - case '^': |
60 |
| - case '$': |
61 |
| - case '+': |
62 |
| - case '{': |
63 |
| - case '}': |
64 |
| - case '(': |
65 |
| - case ')': |
66 |
| - case '|': |
67 |
| - case '\\': |
68 |
| - regexStr += '\\' + char; |
69 |
| - i++; |
70 |
| - break; |
71 |
| - default: |
72 |
| - regexStr += char; |
73 |
| - i++; |
74 |
| - break; |
75 |
| - } |
76 |
| - } |
77 |
| - |
78 |
| - return new RegExp('^' + regexStr + '$'); |
79 |
| -} |
| 7 | +const { join, relative, resolve } = pathModule.posix; |
80 | 8 |
|
81 | 9 | /**
|
82 | 10 | * Check if a path matches a glob pattern
|
83 | 11 | */
|
84 | 12 | function matchesPattern(path: string, pattern: string): boolean {
|
85 |
| - const regex = globToRegex(pattern); |
| 13 | + const regex = toRegex(pattern); |
86 | 14 | return regex.test(path);
|
87 | 15 | }
|
88 | 16 |
|
|
0 commit comments