micromatch 2.3.11 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of micromatch might be problematic. Click here for more details.

package/lib/cache.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = new (require('fragment-cache'))();
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ var nanomatch = require('nanomatch');
4
+ var extglob = require('extglob');
5
+
6
+ module.exports = function(snapdragon) {
7
+ var compilers = snapdragon.compiler.compilers;
8
+ var opts = snapdragon.options;
9
+
10
+ // register nanomatch compilers
11
+ snapdragon.use(nanomatch.compilers);
12
+
13
+ // get references to some specific nanomatch compilers before they
14
+ // are overridden by the extglob and/or custom compilers
15
+ var escape = compilers.escape;
16
+ var qmark = compilers.qmark;
17
+ var slash = compilers.slash;
18
+ var star = compilers.star;
19
+ var text = compilers.text;
20
+ var plus = compilers.plus;
21
+ var dot = compilers.dot;
22
+ var eos = compilers.eos;
23
+
24
+ // register extglob compilers or escape exglobs if disabled
25
+ if (opts.extglob === false || opts.noext === true) {
26
+ snapdragon.compiler.use(escapeExtglobs);
27
+ } else {
28
+ snapdragon.use(extglob.compilers);
29
+ }
30
+
31
+ snapdragon.use(function() {
32
+ this.options.star = this.options.star || function(/*node*/) {
33
+ return '[^/]*?';
34
+ };
35
+ });
36
+
37
+ // custom micromatch compilers
38
+ snapdragon.compiler
39
+
40
+ // reset referenced compiler
41
+ .set('dot', dot)
42
+ .set('escape', escape)
43
+ .set('plus', plus)
44
+ .set('slash', slash)
45
+ .set('qmark', qmark)
46
+ .set('star', star)
47
+ .set('text', text);
48
+ };
49
+
50
+ function escapeExtglobs(compiler) {
51
+ compiler.set('paren', function(node) {
52
+ var val = '';
53
+ visit(node, function(tok) {
54
+ if (tok.val) val += '\\' + tok.val;
55
+ });
56
+ return this.emit(val, node);
57
+ });
58
+
59
+ /**
60
+ * Visit `node` with the given `fn`
61
+ */
62
+
63
+ function visit(node, fn) {
64
+ return node.nodes ? mapVisit(node.nodes, fn) : fn(node);
65
+ }
66
+
67
+ /**
68
+ * Map visit over array of `nodes`.
69
+ */
70
+
71
+ function mapVisit(nodes, fn) {
72
+ var len = nodes.length;
73
+ var idx = -1;
74
+ while (++idx < len) {
75
+ visit(nodes[idx], fn);
76
+ }
77
+ }
78
+ }
package/lib/parsers.js ADDED
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+
3
+ var extglob = require('extglob');
4
+ var nanomatch = require('nanomatch');
5
+ var regexNot = require('regex-not');
6
+ var toRegex = require('to-regex');
7
+ var not;
8
+
9
+ /**
10
+ * Characters to use in negation regex (we want to "not" match
11
+ * characters that are matched by other parsers)
12
+ */
13
+
14
+ var TEXT = '([!@*?+]?\\(|\\)|\\[:?(?=.*?:?\\])|:?\\]|[*+?!^$.\\\\/])+';
15
+ var createNotRegex = function(opts) {
16
+ return not || (not = textRegex(TEXT));
17
+ };
18
+
19
+ /**
20
+ * Parsers
21
+ */
22
+
23
+ module.exports = function(snapdragon) {
24
+ var parsers = snapdragon.parser.parsers;
25
+
26
+ // register nanomatch parsers
27
+ snapdragon.use(nanomatch.parsers);
28
+
29
+ // get references to some specific nanomatch parsers before they
30
+ // are overridden by the extglob and/or parsers
31
+ var escape = parsers.escape;
32
+ var slash = parsers.slash;
33
+ var qmark = parsers.qmark;
34
+ var plus = parsers.plus;
35
+ var star = parsers.star;
36
+ var dot = parsers.dot;
37
+
38
+ // register extglob parsers
39
+ snapdragon.use(extglob.parsers);
40
+
41
+ // custom micromatch parsers
42
+ snapdragon.parser
43
+ .use(function() {
44
+ // override "notRegex" created in nanomatch parser
45
+ this.notRegex = /^\!+(?!\()/;
46
+ })
47
+ // reset the referenced parsers
48
+ .capture('escape', escape)
49
+ .capture('slash', slash)
50
+ .capture('qmark', qmark)
51
+ .capture('star', star)
52
+ .capture('plus', plus)
53
+ .capture('dot', dot)
54
+
55
+ /**
56
+ * Override `text` parser
57
+ */
58
+
59
+ .capture('text', function() {
60
+ if (this.isInside('bracket')) return;
61
+ var pos = this.position();
62
+ var m = this.match(createNotRegex(this.options));
63
+ if (!m || !m[0]) return;
64
+
65
+ // escape regex boundary characters and simple brackets
66
+ var val = m[0].replace(/([[\]^$])/g, '\\$1');
67
+
68
+ return pos({
69
+ type: 'text',
70
+ val: val
71
+ });
72
+ });
73
+ };
74
+
75
+ /**
76
+ * Create text regex
77
+ */
78
+
79
+ function textRegex(pattern) {
80
+ var notStr = regexNot.create(pattern, {contains: true, strictClose: false});
81
+ var prefix = '(?:[\\^]|\\\\|';
82
+ return toRegex(prefix + notStr + ')', {strictClose: false});
83
+ }
package/lib/utils.js CHANGED
@@ -1,70 +1,189 @@
1
1
  'use strict';
2
2
 
3
- var win32 = process && process.platform === 'win32';
4
- var path = require('path');
5
- var fileRe = require('filename-regex');
6
3
  var utils = module.exports;
4
+ var path = require('path');
7
5
 
8
6
  /**
9
7
  * Module dependencies
10
8
  */
11
9
 
10
+ var Snapdragon = require('snapdragon');
11
+ utils.define = require('define-property');
12
12
  utils.diff = require('arr-diff');
13
- utils.unique = require('array-unique');
14
- utils.braces = require('braces');
15
- utils.brackets = require('expand-brackets');
16
- utils.extglob = require('extglob');
17
- utils.isExtglob = require('is-extglob');
18
- utils.isGlob = require('is-glob');
13
+ utils.extend = require('extend-shallow');
14
+ utils.pick = require('object.pick');
19
15
  utils.typeOf = require('kind-of');
20
- utils.normalize = require('normalize-path');
21
- utils.omit = require('object.omit');
22
- utils.parseGlob = require('parse-glob');
23
- utils.cache = require('regex-cache');
16
+ utils.unique = require('array-unique');
17
+
18
+ /**
19
+ * Returns true if the platform is windows, or `path.sep` is `\\`.
20
+ * This is defined as a function to allow `path.sep` to be set in unit tests,
21
+ * or by the user, if there is a reason to do so.
22
+ * @return {Boolean}
23
+ */
24
+
25
+ utils.isWindows = function() {
26
+ return path.sep === '\\' || process.platform === 'win32';
27
+ };
28
+
29
+ /**
30
+ * Get the `Snapdragon` instance to use
31
+ */
32
+
33
+ utils.instantiate = function(ast, options) {
34
+ var snapdragon;
35
+ // if an instance was created by `.parse`, use that instance
36
+ if (utils.typeOf(ast) === 'object' && ast.snapdragon) {
37
+ snapdragon = ast.snapdragon;
38
+ // if the user supplies an instance on options, use that instance
39
+ } else if (utils.typeOf(options) === 'object' && options.snapdragon) {
40
+ snapdragon = options.snapdragon;
41
+ // create a new instance
42
+ } else {
43
+ snapdragon = new Snapdragon(options);
44
+ }
45
+
46
+ utils.define(snapdragon, 'parse', function(str, options) {
47
+ var parsed = Snapdragon.prototype.parse.apply(this, arguments);
48
+ parsed.input = str;
49
+
50
+ // escape unmatched brace/bracket/parens
51
+ var last = this.parser.stack.pop();
52
+ if (last && this.options.strictErrors !== true) {
53
+ var open = last.nodes[0];
54
+ var inner = last.nodes[1];
55
+ if (last.type === 'bracket') {
56
+ if (inner.val.charAt(0) === '[') {
57
+ inner.val = '\\' + inner.val;
58
+ }
59
+
60
+ } else {
61
+ open.val = '\\' + open.val;
62
+ var sibling = open.parent.nodes[1];
63
+ if (sibling.type === 'star') {
64
+ sibling.loose = true;
65
+ }
66
+ }
67
+ }
68
+
69
+ // add non-enumerable parser reference
70
+ utils.define(parsed, 'parser', this.parser);
71
+ return parsed;
72
+ });
73
+
74
+ return snapdragon;
75
+ };
76
+
77
+ /**
78
+ * Create the key to use for memoization. The key is generated
79
+ * by iterating over the options and concatenating key-value pairs
80
+ * to the pattern string.
81
+ */
82
+
83
+ utils.createKey = function(pattern, options) {
84
+ if (utils.typeOf(options) !== 'object') {
85
+ return pattern;
86
+ }
87
+ var val = pattern;
88
+ var keys = Object.keys(options);
89
+ for (var i = 0; i < keys.length; i++) {
90
+ var key = keys[i];
91
+ val += ';' + key + '=' + String(options[key]);
92
+ }
93
+ return val;
94
+ };
95
+
96
+ /**
97
+ * Cast `val` to an array
98
+ * @return {Array}
99
+ */
100
+
101
+ utils.arrayify = function(val) {
102
+ if (typeof val === 'string') return [val];
103
+ return val ? (Array.isArray(val) ? val : [val]) : [];
104
+ };
105
+
106
+ /**
107
+ * Return true if `val` is a non-empty string
108
+ */
109
+
110
+ utils.isString = function(val) {
111
+ return typeof val === 'string';
112
+ };
113
+
114
+ /**
115
+ * Return true if `val` is a non-empty string
116
+ */
117
+
118
+ utils.isObject = function(val) {
119
+ return utils.typeOf(val) === 'object';
120
+ };
24
121
 
25
122
  /**
26
- * Get the filename of a filepath
123
+ * Combines duplicate characters in the provided string.
124
+ * @param {String} `str`
125
+ * @returns {String}
126
+ */
127
+
128
+ utils.combineDuplicates = function(str, val) {
129
+ var re = new RegExp('(' + val + ')(?=(?:' + val + ')*\\1)', 'g');
130
+ return str.replace(re, '');
131
+ };
132
+
133
+ /**
134
+ * Returns true if the given `str` has special characters
135
+ */
136
+
137
+ utils.hasSpecialChars = function(str) {
138
+ return /(?:(?:(^|\/)[!.])|[*?+()|\[\]{}]|[+@]\()/.test(str);
139
+ };
140
+
141
+ /**
142
+ * Normalize slashes in the given filepath.
27
143
  *
28
- * @param {String} `string`
144
+ * @param {String} `filepath`
29
145
  * @return {String}
30
146
  */
31
147
 
32
- utils.filename = function filename(fp) {
33
- var seg = fp.match(fileRe());
34
- return seg && seg[0];
148
+ utils.toPosixPath = function(str) {
149
+ return str.replace(/\\+/g, '/');
35
150
  };
36
151
 
37
152
  /**
38
- * Returns a function that returns true if the given
39
- * pattern is the same as a given `filepath`
153
+ * Strip backslashes before special characters in a string.
40
154
  *
41
- * @param {String} `pattern`
42
- * @return {Function}
155
+ * @param {String} `str`
156
+ * @return {String}
43
157
  */
44
158
 
45
- utils.isPath = function isPath(pattern, opts) {
46
- opts = opts || {};
47
- return function(fp) {
48
- var unixified = utils.unixify(fp, opts);
49
- if(opts.nocase){
50
- return pattern.toLowerCase() === unixified.toLowerCase();
51
- }
52
- return pattern === unixified;
53
- };
159
+ utils.unescape = function(str) {
160
+ return utils.toPosixPath(str.replace(/\\(?=[*+?!.])/g, ''));
54
161
  };
55
162
 
56
163
  /**
57
- * Returns a function that returns true if the given
58
- * pattern contains a `filepath`
59
- *
60
- * @param {String} `pattern`
61
- * @return {Function}
164
+ * Strip the prefix from a filepath
165
+ * @param {String} `fp`
166
+ * @return {String}
62
167
  */
63
168
 
64
- utils.hasPath = function hasPath(pattern, opts) {
65
- return function(fp) {
66
- return utils.unixify(pattern, opts).indexOf(fp) !== -1;
67
- };
169
+ utils.stripPrefix = function(str) {
170
+ if (str.charAt(0) !== '.') {
171
+ return str;
172
+ }
173
+ var ch = str.charAt(1);
174
+ if (utils.isSlash(ch)) {
175
+ return str.slice(2);
176
+ }
177
+ return str;
178
+ };
179
+
180
+ /**
181
+ * Returns true if the given str is an escaped or
182
+ * unescaped path character
183
+ */
184
+
185
+ utils.isSlash = function(str) {
186
+ return str === '/' || str === '\\/' || str === '\\' || str === '\\\\';
68
187
  };
69
188
 
70
189
  /**
@@ -75,75 +194,119 @@ utils.hasPath = function hasPath(pattern, opts) {
75
194
  * @return {Function}
76
195
  */
77
196
 
78
- utils.matchPath = function matchPath(pattern, opts) {
79
- var fn = (opts && opts.contains)
80
- ? utils.hasPath(pattern, opts)
81
- : utils.isPath(pattern, opts);
82
- return fn;
197
+ utils.matchPath = function(pattern, options) {
198
+ return (options && options.contains)
199
+ ? utils.containsPattern(pattern, options)
200
+ : utils.equalsPattern(pattern, options);
83
201
  };
84
202
 
85
203
  /**
86
- * Returns a function that returns true if the given
87
- * regex matches the `filename` of a file path.
88
- *
89
- * @param {RegExp} `re`
90
- * @return {Boolean}
204
+ * Returns true if the given (original) filepath or unixified path are equal
205
+ * to the given pattern.
91
206
  */
92
207
 
93
- utils.hasFilename = function hasFilename(re) {
94
- return function(fp) {
95
- var name = utils.filename(fp);
96
- return name && re.test(name);
97
- };
208
+ utils._equals = function(filepath, unixPath, pattern) {
209
+ return pattern === filepath || pattern === unixPath;
98
210
  };
99
211
 
100
212
  /**
101
- * Coerce `val` to an array
102
- *
103
- * @param {*} val
104
- * @return {Array}
213
+ * Returns true if the given (original) filepath or unixified path contain
214
+ * the given pattern.
105
215
  */
106
216
 
107
- utils.arrayify = function arrayify(val) {
108
- return !Array.isArray(val)
109
- ? [val]
110
- : val;
217
+ utils._contains = function(filepath, unixPath, pattern) {
218
+ return filepath.indexOf(pattern) !== -1 || unixPath.indexOf(pattern) !== -1;
111
219
  };
112
220
 
113
221
  /**
114
- * Normalize all slashes in a file path or glob pattern to
115
- * forward slashes.
222
+ * Returns a function that returns true if the given
223
+ * pattern is the same as a given `filepath`
224
+ *
225
+ * @param {String} `pattern`
226
+ * @return {Function}
116
227
  */
117
228
 
118
- utils.unixify = function unixify(fp, opts) {
119
- if (opts && opts.unixify === false) return fp;
120
- if (opts && opts.unixify === true || win32 || path.sep === '\\') {
121
- return utils.normalize(fp, false);
122
- }
123
- if (opts && opts.unescape === true) {
124
- return fp ? fp.toString().replace(/\\(\w)/g, '$1') : '';
125
- }
126
- return fp;
229
+ utils.equalsPattern = function(pattern, options) {
230
+ var unixify = utils.unixify(options);
231
+ options = options || {};
232
+
233
+ return function fn(filepath) {
234
+ var equal = utils._equals(filepath, unixify(filepath), pattern);
235
+ if (equal === true || options.nocase !== true) {
236
+ return equal;
237
+ }
238
+ var lower = filepath.toLowerCase();
239
+ return utils._equals(lower, unixify(lower), pattern);
240
+ };
127
241
  };
128
242
 
129
243
  /**
130
- * Escape/unescape utils
244
+ * Returns a function that returns true if the given
245
+ * pattern contains a `filepath`
246
+ *
247
+ * @param {String} `pattern`
248
+ * @return {Function}
131
249
  */
132
250
 
133
- utils.escapePath = function escapePath(fp) {
134
- return fp.replace(/[\\.]/g, '\\$&');
251
+ utils.containsPattern = function(pattern, options) {
252
+ var unixify = utils.unixify(options);
253
+ options = options || {};
254
+
255
+ return function(filepath) {
256
+ var contains = utils._contains(filepath, unixify(filepath), pattern);
257
+ if (contains === true || options.nocase !== true) {
258
+ return contains;
259
+ }
260
+ var lower = filepath.toLowerCase();
261
+ return utils._contains(lower, unixify(lower), pattern);
262
+ };
135
263
  };
136
264
 
137
- utils.unescapeGlob = function unescapeGlob(fp) {
138
- return fp.replace(/[\\"']/g, '');
265
+ /**
266
+ * Returns a function that returns true if the given
267
+ * regex matches the `filename` of a file path.
268
+ *
269
+ * @param {RegExp} `re` Matching regex
270
+ * @return {Function}
271
+ */
272
+
273
+ utils.matchBasename = function(re) {
274
+ return function(filepath) {
275
+ return re.test(filepath) || re.test(path.basename(filepath));
276
+ };
139
277
  };
140
278
 
141
- utils.escapeRe = function escapeRe(str) {
142
- return str.replace(/[-[\\$*+?.#^\s{}(|)\]]/g, '\\$&');
279
+ /**
280
+ * Determines the filepath to return based on the provided options.
281
+ * @return {any}
282
+ */
283
+
284
+ utils.value = function(str, unixify, options) {
285
+ if (options && options.unixify === false) {
286
+ return str;
287
+ }
288
+ return unixify(str);
143
289
  };
144
290
 
145
291
  /**
146
- * Expose `utils`
292
+ * Returns a function that normalizes slashes in a string to forward
293
+ * slashes, strips `./` from beginning of paths, and optionally unescapes
294
+ * special characters.
295
+ * @return {Function}
147
296
  */
148
297
 
149
- module.exports = utils;
298
+ utils.unixify = function(options) {
299
+ options = options || {};
300
+ return function(filepath) {
301
+ if (utils.isWindows() || options.unixify === true) {
302
+ filepath = utils.toPosixPath(filepath);
303
+ }
304
+ if (options.stripPrefix !== false) {
305
+ filepath = utils.stripPrefix(filepath);
306
+ }
307
+ if (options.unescape === true) {
308
+ filepath = utils.unescape(filepath);
309
+ }
310
+ return filepath;
311
+ };
312
+ };