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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
'use strict';
/**
* Local dependencies
*/
var compilers = require('./lib/compilers');
var parsers = require('./lib/parsers');
/**
* Module dependencies
*/
var debug = require('debug')('expand-brackets');
var extend = require('extend-shallow');
var Snapdragon = require('snapdragon');
var toRegex = require('to-regex');
/**
* Parses the given POSIX character class `pattern` and returns a
* string that can be used for creating regular expressions for matching.
*
* @param {String} `pattern`
* @param {Object} `options`
* @return {Object}
* @api public
*/
function brackets(pattern, options) {
debug('initializing from <%s>', __filename);
var res = brackets.create(pattern, options);
return res.output;
}
/**
* Takes an array of strings and a POSIX character class pattern, and returns a new
* array with only the strings that matched the pattern.
*
* ```js
* var brackets = require('expand-brackets');
* console.log(brackets.match(['1', 'a', 'ab'], '[[:alpha:]]'));
* //=> ['a']
*
* console.log(brackets.match(['1', 'a', 'ab'], '[[:alpha:]]+'));
* //=> ['a', 'ab']
* ```
* @param {Array} `arr` Array of strings to match
* @param {String} `pattern` POSIX character class pattern(s)
* @param {Object} `options`
* @return {Array}
* @api public
*/
brackets.match = function(arr, pattern, options) {
arr = [].concat(arr);
var opts = extend({}, options);
var isMatch = brackets.matcher(pattern, opts);
var len = arr.length;
var idx = -1;
var res = [];
while (++idx < len) {
var ele = arr[idx];
if (isMatch(ele)) {
res.push(ele);
}
}
if (res.length === 0) {
if (opts.failglob === true) {
throw new Error('no matches found for "' + pattern + '"');
}
if (opts.nonull === true || opts.nullglob === true) {
return [pattern.split('\\').join('')];
}
}
return res;
};
/**
* Returns true if the specified `string` matches the given
* brackets `pattern`.
*
* ```js
* var brackets = require('expand-brackets');
*
* console.log(brackets.isMatch('a.a', '[[:alpha:]].[[:alpha:]]'));
* //=> true
* console.log(brackets.isMatch('1.2', '[[:alpha:]].[[:alpha:]]'));
* //=> false
* ```
* @param {String} `string` String to match
* @param {String} `pattern` Poxis pattern
* @param {String} `options`
* @return {Boolean}
* @api public
*/
brackets.isMatch = function(str, pattern, options) {
return brackets.matcher(pattern, options)(str);
};
/**
* Takes a POSIX character class pattern and returns a matcher function. The returned
* function takes the string to match as its only argument.
*
* ```js
* var brackets = require('expand-brackets');
* var isMatch = brackets.matcher('[[:lower:]].[[:upper:]]');
*
* console.log(isMatch('a.a'));
* //=> false
* console.log(isMatch('a.A'));
* //=> true
* ```
* @param {String} `pattern` Poxis pattern
* @param {String} `options`
* @return {Boolean}
* @api public
*/
brackets.matcher = function(pattern, options) {
var re = brackets.makeRe(pattern, options);
return function(str) {
return re.test(str);
};
};
/**
* Create a regular expression from the given `pattern`.
*
* ```js
* var brackets = require('expand-brackets');
* var re = brackets.makeRe('[[:alpha:]]');
* console.log(re);
* //=> /^(?:[a-zA-Z])$/
* ```
* @param {String} `pattern` The pattern to convert to regex.
* @param {Object} `options`
* @return {RegExp}
* @api public
*/
brackets.makeRe = function(pattern, options) {
var res = brackets.create(pattern, options);
var opts = extend({strictErrors: false}, options);
return toRegex(res.output, opts);
};
/**
* Parses the given POSIX character class `pattern` and returns an object
* with the compiled `output` and optional source `map`.
*
* ```js
* var brackets = require('expand-brackets');
* console.log(brackets('[[:alpha:]]'));
* // { options: { source: 'string' },
* // input: '[[:alpha:]]',
* // state: {},
* // compilers:
* // { eos: [Function],
* // noop: [Function],
* // bos: [Function],
* // not: [Function],
* // escape: [Function],
* // text: [Function],
* // posix: [Function],
* // bracket: [Function],
* // 'bracket.open': [Function],
* // 'bracket.inner': [Function],
* // 'bracket.literal': [Function],
* // 'bracket.close': [Function] },
* // output: '[a-zA-Z]',
* // ast:
* // { type: 'root',
* // errors: [],
* // nodes: [ [Object], [Object], [Object] ] },
* // parsingErrors: [] }
* ```
* @param {String} `pattern`
* @param {Object} `options`
* @return {Object}
* @api public
*/
brackets.create = function(pattern, options) {
var snapdragon = (options && options.snapdragon) || new Snapdragon(options);
compilers(snapdragon);
parsers(snapdragon);
var ast = snapdragon.parse(pattern, options);
ast.input = pattern;
var res = snapdragon.compile(ast, options);
res.input = pattern;
return res;
};
/**
* Expose `brackets` constructor, parsers and compilers
*/
brackets.compilers = compilers;
brackets.parsers = parsers;
/**
* Expose `brackets`
* @type {Function}
*/
module.exports = brackets;
|