glob.js
4.14 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
var chars = require('./chars');
var utils = require('./utils');
/**
* Expose `Glob`
*/
var Glob = module.exports = function Glob(pattern, options) {
if (!(this instanceof Glob)) {
return new Glob(pattern, options);
}
this.options = options || {};
this.pattern = pattern;
this.history = [];
this.tokens = {};
this.init(pattern);
};
/**
* Initialize defaults
*/
Glob.prototype.init = function(pattern) {
this.orig = pattern;
this.negated = this.isNegated();
this.options.track = this.options.track || false;
this.options.makeRe = true;
};
/**
* Push a change into `glob.history`. Useful
* for debugging.
*/
Glob.prototype.track = function(msg) {
if (this.options.track) {
this.history.push({msg: msg, pattern: this.pattern});
}
};
/**
* Return true if `glob.pattern` was negated
* with `!`, also remove the `!` from the pattern.
*
* @return {Boolean}
*/
Glob.prototype.isNegated = function() {
if (this.pattern.charCodeAt(0) === 33 /* '!' */) {
this.pattern = this.pattern.slice(1);
return true;
}
return false;
};
/**
* Expand braces in the given glob pattern.
*
* We only need to use the [braces] lib when
* patterns are nested.
*/
Glob.prototype.braces = function() {
if (this.options.nobraces !== true && this.options.nobrace !== true) {
// naive/fast check for imbalanced characters
var a = this.pattern.match(/[\{\(\[]/g);
var b = this.pattern.match(/[\}\)\]]/g);
// if imbalanced, don't optimize the pattern
if (a && b && (a.length !== b.length)) {
this.options.makeRe = false;
}
// expand brace patterns and join the resulting array
var expanded = utils.braces(this.pattern, this.options);
this.pattern = expanded.join('|');
}
};
/**
* Expand bracket expressions in `glob.pattern`
*/
Glob.prototype.brackets = function() {
if (this.options.nobrackets !== true) {
this.pattern = utils.brackets(this.pattern);
}
};
/**
* Expand bracket expressions in `glob.pattern`
*/
Glob.prototype.extglob = function() {
if (this.options.noextglob === true) return;
if (utils.isExtglob(this.pattern)) {
this.pattern = utils.extglob(this.pattern, {escape: true});
}
};
/**
* Parse the given pattern
*/
Glob.prototype.parse = function(pattern) {
this.tokens = utils.parseGlob(pattern || this.pattern, true);
return this.tokens;
};
/**
* Replace `a` with `b`. Also tracks the change before and
* after each replacement. This is disabled by default, but
* can be enabled by setting `options.track` to true.
*
* Also, when the pattern is a string, `.split()` is used,
* because it's much faster than replace.
*
* @param {RegExp|String} `a`
* @param {String} `b`
* @param {Boolean} `escape` When `true`, escapes `*` and `?` in the replacement.
* @return {String}
*/
Glob.prototype._replace = function(a, b, escape) {
this.track('before (find): "' + a + '" (replace with): "' + b + '"');
if (escape) b = esc(b);
if (a && b && typeof a === 'string') {
this.pattern = this.pattern.split(a).join(b);
} else {
this.pattern = this.pattern.replace(a, b);
}
this.track('after');
};
/**
* Escape special characters in the given string.
*
* @param {String} `str` Glob pattern
* @return {String}
*/
Glob.prototype.escape = function(str) {
this.track('before escape: ');
var re = /["\\](['"]?[^"'\\]['"]?)/g;
this.pattern = str.replace(re, function($0, $1) {
var o = chars.ESC;
var ch = o && o[$1];
if (ch) {
return ch;
}
if (/[a-z]/i.test($0)) {
return $0.split('\\').join('');
}
return $0;
});
this.track('after escape: ');
};
/**
* Unescape special characters in the given string.
*
* @param {String} `str`
* @return {String}
*/
Glob.prototype.unescape = function(str) {
var re = /__([A-Z]+)_([A-Z]+)__/g;
this.pattern = str.replace(re, function($0, $1) {
return chars[$1][$0];
});
this.pattern = unesc(this.pattern);
};
/**
* Escape/unescape utils
*/
function esc(str) {
str = str.split('?').join('%~');
str = str.split('*').join('%%');
return str;
}
function unesc(str) {
str = str.split('%~').join('?');
str = str.split('%%').join('*');
return str;
}