Skip to content

Commit 8872e68

Browse files
committed
apply suggestions from #76
1 parent 857d78b commit 8872e68

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { type Options as FdirOptions, fdir } from 'fdir';
33
import picomatch from 'picomatch';
44
import { escapePath, getPartialMatcher, isDynamicPattern, splitPattern } from './utils.ts';
55

6+
const PARENT_DIRECTORY = /^(\/?\.\.)+/;
7+
const ESCAPING_BACKSLASHES = /\\(?=[()[\]{}!*+?@|])/g;
8+
const BACKSLASHES = /\\/g;
9+
610
export interface GlobOptions {
711
absolute?: boolean;
812
cwd?: string;
@@ -40,13 +44,13 @@ function normalizePattern(
4044
result += '/**';
4145
}
4246

43-
if (path.isAbsolute(result.replace(/\\(?=[()[\]{}!*+?@|])/g, ''))) {
47+
if (path.isAbsolute(result.replace(ESCAPING_BACKSLASHES, ''))) {
4448
result = posix.relative(escapePath(cwd), result);
4549
} else {
4650
result = posix.normalize(result);
4751
}
4852

49-
const parentDirectoryMatch = /^(\/?\.\.)+/.exec(result);
53+
const parentDirectoryMatch = PARENT_DIRECTORY.exec(result);
5054
if (parentDirectoryMatch?.[0]) {
5155
const potentialRoot = posix.join(cwd, parentDirectoryMatch[0]);
5256
if (properties.root.length > potentialRoot.length) {
@@ -238,7 +242,7 @@ function crawl(options: GlobOptions, cwd: string, sync: boolean) {
238242
}
239243

240244
// backslashes are removed so that inferred roots like `C:/New folder \\(1\\)` work
241-
properties.root = properties.root.replace(/\\/g, '');
245+
properties.root = properties.root.replace(BACKSLASHES, '');
242246
const api = new fdir(fdirOptions).crawl(properties.root);
243247

244248
if (cwd === properties.root || options.absolute) {
@@ -266,7 +270,7 @@ export async function glob(
266270
Array.isArray(patternsOrOptions) || typeof patternsOrOptions === 'string'
267271
? { ...options, patterns: patternsOrOptions }
268272
: patternsOrOptions;
269-
const cwd = opts.cwd ? path.resolve(opts.cwd).replace(/\\/g, '/') : process.cwd().replace(/\\/g, '/');
273+
const cwd = opts.cwd ? path.resolve(opts.cwd).replace(BACKSLASHES, '/') : process.cwd().replace(BACKSLASHES, '/');
270274

271275
return crawl(opts, cwd, false);
272276
}
@@ -282,7 +286,7 @@ export function globSync(patternsOrOptions: string | string[] | GlobOptions, opt
282286
Array.isArray(patternsOrOptions) || typeof patternsOrOptions === 'string'
283287
? { ...options, patterns: patternsOrOptions }
284288
: patternsOrOptions;
285-
const cwd = opts.cwd ? path.resolve(opts.cwd).replace(/\\/g, '/') : process.cwd().replace(/\\/g, '/');
289+
const cwd = opts.cwd ? path.resolve(opts.cwd).replace(BACKSLASHES, '/') : process.cwd().replace(BACKSLASHES, '/');
286290

287291
return crawl(opts, cwd, true);
288292
}

src/utils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ export interface PartialMatcherOptions {
88

99
// the result of over 4 months of figuring stuff out and a LOT of help
1010
export function getPartialMatcher(patterns: string[], options?: PartialMatcherOptions): Matcher {
11-
const regexes = patterns.map(pattern => splitPattern(pattern).map(part => picomatch.makeRe(part, options)));
11+
const regexes: RegExp[][] = [];
12+
const patternsParts: string[][] = [];
13+
for (const pattern of patterns) {
14+
const parts = splitPattern(pattern);
15+
patternsParts.push(parts);
16+
regexes.push(parts.map(part => picomatch.makeRe(part, options)));
17+
}
1218
return (input: string) => {
1319
// no need to `splitPattern` as this is indeed not a pattern
1420
const inputParts = input.split('/');
1521
for (let i = 0; i < patterns.length; i++) {
16-
const patternParts = splitPattern(patterns[i]);
22+
const patternParts = patternsParts[i];
1723
const regex = regexes[i];
1824
const minParts = Math.min(inputParts.length, patternParts.length);
1925
let j = 0;
@@ -36,7 +42,7 @@ export function getPartialMatcher(patterns: string[], options?: PartialMatcherOp
3642
// unlike popular belief, `**` doesn't return true in *all* cases
3743
// some examples are when matching it to `.a` with dot: false or `..`
3844
// so it needs to match to return early
39-
if (part === '**' && match) {
45+
if (part === '**') {
4046
return true;
4147
}
4248

0 commit comments

Comments
 (0)