Skip to content

Commit 7e72775

Browse files
committed
cleaned up Glob and fixed a bug in sub org pattern detection
1 parent ff7a656 commit 7e72775

File tree

1 file changed

+50
-14
lines changed

1 file changed

+50
-14
lines changed

lib/glob.js

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,64 @@ class Glob {
22
constructor (glob) {
33
this.glob = glob
44

5-
// If not a glob pattern then just match the string.
6-
if (!this.glob.includes('*')) {
7-
this.regexp = new RegExp(`.*${this.glob}.*`, 'u')
5+
// For patterns without any wildcards, match them anywhere in the string
6+
const hasWildcards = glob.includes('*') || glob.includes('?')
7+
8+
const hasNothingToEscape = escapeRegExp(glob) === glob
9+
10+
if (hasNothingToEscape) {
11+
this.regexp = new RegExp(`\\b${glob}\\b`, 'u')
812
return
913
}
10-
this.regexptText = this.globize(this.glob)
11-
this.regexp = new RegExp(`^${this.regexptText}$`, 'u')
12-
}
1314

14-
globize (glob) {
15-
return glob
16-
.replace(/\\/g, '\\\\') // escape backslashes
17-
.replace(/\//g, '\\/') // escape forward slashes
18-
.replace(/\./g, '\\.') // escape periods
19-
.replace(/\?/g, '([^\\/])') // match any single character except /
20-
.replace(/\*\*/g, '.+') // match any character except /, including /
21-
.replace(/\*/g, '([^\\/]*)') // match any character except /
15+
if (!hasWildcards) {
16+
// Simple case: no wildcards, just do a simple substring match
17+
this.regexp = new RegExp(escapeRegExp(glob), 'u')
18+
return
19+
}
20+
21+
// Handle wildcard patterns
22+
let pattern
23+
24+
if (glob.includes('**')) {
25+
// Handle ** which can match across directory boundaries
26+
pattern = glob
27+
.replace(/\*\*/g, '__GLOBSTAR__')
28+
.replace(/\./g, '\\.')
29+
.replace(/\//g, '\\/')
30+
.replace(/\?/g, '.')
31+
.replace(/\*/g, '[^\\/]*')
32+
.replace(/__GLOBSTAR__/g, '.*')
33+
} else {
34+
// Handle patterns with * but not **
35+
pattern = glob
36+
.replace(/\./g, '\\.')
37+
.replace(/\//g, '\\/')
38+
.replace(/\?/g, '.')
39+
.replace(/\*/g, '[^\\/]*')
40+
}
41+
42+
// Handle character classes
43+
pattern = pattern.replace(/\\\[([^\]]+)\\\]/g, '[$1]')
44+
45+
this.regexp = new RegExp(`^${pattern}$`, 'u')
2246
}
2347

2448
toString () {
2549
return this.glob
2650
}
2751

2852
[Symbol.search] (s) {
53+
console.log('regex patttern is ', this.regexp)
54+
console.log('string to search is ', s)
55+
console.log('string search result is ', s.search(this.regexp))
2956
return s.search(this.regexp)
3057
}
3158

3259
[Symbol.match] (s) {
60+
console.log('regex patttern is ', this.regexp)
61+
console.log('string to match is ', s)
62+
console.log('string match result is ', s.match(this.regexp))
3363
return s.match(this.regexp)
3464
}
3565

@@ -41,4 +71,10 @@ class Glob {
4171
return s.replaceAll(this.regexp, replacement)
4272
}
4373
}
74+
75+
// Helper function to escape regular expression special chars
76+
function escapeRegExp (string) {
77+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
78+
}
79+
4480
module.exports = Glob

0 commit comments

Comments
 (0)