index.js
3.79 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
/*!
* extglob <https://github.com/jonschlinkert/extglob>
*
* Copyright (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
/**
* Module dependencies
*/
var isExtglob = require('is-extglob');
var re, cache = {};
/**
* Expose `extglob`
*/
module.exports = extglob;
/**
* Convert the given extglob `string` to a regex-compatible
* string.
*
* ```js
* var extglob = require('extglob');
* extglob('!(a?(b))');
* //=> '(?!a(?:b)?)[^/]*?'
* ```
*
* @param {String} `str` The string to convert.
* @param {Object} `options`
* @option {Boolean} [options] `esc` If `false` special characters will not be escaped. Defaults to `true`.
* @option {Boolean} [options] `regex` If `true` a regular expression is returned instead of a string.
* @return {String}
* @api public
*/
function extglob(str, opts) {
opts = opts || {};
var o = {}, i = 0;
// fix common character reversals
// '*!(.js)' => '*.!(js)'
str = str.replace(/!\(([^\w*()])/g, '$1!(');
// support file extension negation
str = str.replace(/([*\/])\.!\([*]\)/g, function (m, ch) {
if (ch === '/') {
return escape('\\/[^.]+');
}
return escape('[^.]+');
});
// create a unique key for caching by
// combining the string and options
var key = str
+ String(!!opts.regex)
+ String(!!opts.contains)
+ String(!!opts.escape);
if (cache.hasOwnProperty(key)) {
return cache[key];
}
if (!(re instanceof RegExp)) {
re = regex();
}
opts.negate = false;
var m;
while (m = re.exec(str)) {
var prefix = m[1];
var inner = m[3];
if (prefix === '!') {
opts.negate = true;
}
var id = '__EXTGLOB_' + (i++) + '__';
// use the prefix of the _last_ (outtermost) pattern
o[id] = wrap(inner, prefix, opts.escape);
str = str.split(m[0]).join(id);
}
var keys = Object.keys(o);
var len = keys.length;
// we have to loop again to allow us to convert
// patterns in reverse order (starting with the
// innermost/last pattern first)
while (len--) {
var prop = keys[len];
str = str.split(prop).join(o[prop]);
}
var result = opts.regex
? toRegex(str, opts.contains, opts.negate)
: str;
result = result.split('.').join('\\.');
// cache the result and return it
return (cache[key] = result);
}
/**
* Convert `string` to a regex string.
*
* @param {String} `str`
* @param {String} `prefix` Character that determines how to wrap the string.
* @param {Boolean} `esc` If `false` special characters will not be escaped. Defaults to `true`.
* @return {String}
*/
function wrap(inner, prefix, esc) {
if (esc) inner = escape(inner);
switch (prefix) {
case '!':
return '(?!' + inner + ')[^/]' + (esc ? '%%%~' : '*?');
case '@':
return '(?:' + inner + ')';
case '+':
return '(?:' + inner + ')+';
case '*':
return '(?:' + inner + ')' + (esc ? '%%' : '*')
case '?':
return '(?:' + inner + '|)';
default:
return inner;
}
}
function escape(str) {
str = str.split('*').join('[^/]%%%~');
str = str.split('.').join('\\.');
return str;
}
/**
* extglob regex.
*/
function regex() {
return /(\\?[@?!+*$]\\?)(\(([^()]*?)\))/;
}
/**
* Negation regex
*/
function negate(str) {
return '(?!^' + str + ').*$';
}
/**
* Create the regex to do the matching. If
* the leading character in the `pattern` is `!`
* a negation regex is returned.
*
* @param {String} `pattern`
* @param {Boolean} `contains` Allow loose matching.
* @param {Boolean} `isNegated` True if the pattern is a negation pattern.
*/
function toRegex(pattern, contains, isNegated) {
var prefix = contains ? '^' : '';
var after = contains ? '$' : '';
pattern = ('(?:' + pattern + ')' + after);
if (isNegated) {
pattern = prefix + negate(pattern);
}
return new RegExp(prefix + pattern);
}