-
Notifications
You must be signed in to change notification settings - Fork 148
/
glob.js
44 lines (37 loc) · 1.09 KB
/
glob.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Glob {
constructor (glob) {
this.glob = glob
// If not a glob pattern then just match the string.
if (!this.glob.includes('*')) {
this.regexp = new RegExp(`.*${this.glob}.*`, 'u')
return
}
this.regexptText = this.globize(this.glob)
this.regexp = new RegExp(`^${this.regexptText}$`, 'u')
}
globize (glob) {
return glob
.replace(/\\/g, '\\\\') // escape backslashes
.replace(/\//g, '\\/') // escape forward slashes
.replace(/\./g, '\\.') // escape periods
.replace(/\?/g, '([^\\/])') // match any single character except /
.replace(/\*\*/g, '.+') // match any character except /, including /
.replace(/\*/g, '([^\\/]*)') // match any character except /
}
toString () {
return this.glob
}
[Symbol.search] (s) {
return s.search(this.regexp)
}
[Symbol.match] (s) {
return s.match(this.regexp)
}
[Symbol.replace] (s, replacement) {
return s.replace(this.regexp, replacement)
}
[Symbol.replaceAll] (s, replacement) {
return s.replaceAll(this.regexp, replacement)
}
}
module.exports = Glob