Skip to content

Commit a70438c

Browse files
committed
reset to old code that
1 parent c6ce8aa commit a70438c

File tree

1 file changed

+14
-52
lines changed

1 file changed

+14
-52
lines changed

lib/glob.js

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

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')
5+
// If not a glob pattern then just match the string.
6+
if (!this.glob.includes('*')) {
7+
this.regexp = new RegExp(`.*${this.glob}.*`, 'u')
128
return
139
}
10+
this.regexptText = this.globize(this.glob)
11+
this.regexp = new RegExp(`^${this.regexptText}$`, 'u')
12+
}
1413

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, '\\\\')
28-
.replace(/\*\*/g, '__GLOBSTAR__')
29-
.replace(/\./g, '\\.')
30-
.replace(/\//g, '\\/')
31-
.replace(/\?/g, '.')
32-
.replace(/\*/g, '[^\\/]*')
33-
.replace(/__GLOBSTAR__/g, '.*')
34-
} else {
35-
// Handle patterns with * but not **
36-
pattern = glob
37-
.replace(/\\/g, '\\\\')
38-
.replace(/\./g, '\\.')
39-
.replace(/\//g, '\\/')
40-
.replace(/\?/g, '.')
41-
.replace(/\*/g, '[^\\/]*')
42-
}
43-
44-
// Handle character classes
45-
pattern = pattern.replace(/\\\[([^\]]+)\\\]/g, '[$1]')
46-
47-
this.regexp = new RegExp(`^${pattern}$`, 'u')
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 /
4822
}
4923

5024
toString () {
5125
return this.glob
5226
}
5327

5428
[Symbol.search] (s) {
55-
console.log('regex patttern is ', this.regexp)
56-
console.log('string to search is ', s)
57-
console.log('string search result is ', s.search(this.regexp))
5829
return s.search(this.regexp)
5930
}
6031

6132
[Symbol.match] (s) {
62-
console.log('regex patttern is ', this.regexp)
63-
console.log('string to match is ', s)
64-
console.log('string match result is ', s.match(this.regexp))
6533
return s.match(this.regexp)
6634
}
6735

@@ -73,10 +41,4 @@ class Glob {
7341
return s.replaceAll(this.regexp, replacement)
7442
}
7543
}
76-
77-
// Helper function to escape regular expression special chars
78-
function escapeRegExp (string) {
79-
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
80-
}
81-
8244
module.exports = Glob

0 commit comments

Comments
 (0)