1 | 'use strict';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Local dependencies
|
---|
5 | */
|
---|
6 |
|
---|
7 | var compilers = require('./lib/compilers');
|
---|
8 | var parsers = require('./lib/parsers');
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Module dependencies
|
---|
12 | */
|
---|
13 |
|
---|
14 | var debug = require('debug')('expand-brackets');
|
---|
15 | var extend = require('extend-shallow');
|
---|
16 | var Snapdragon = require('snapdragon');
|
---|
17 | var toRegex = require('to-regex');
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Parses the given POSIX character class `pattern` and returns a
|
---|
21 | * string that can be used for creating regular expressions for matching.
|
---|
22 | *
|
---|
23 | * @param {String} `pattern`
|
---|
24 | * @param {Object} `options`
|
---|
25 | * @return {Object}
|
---|
26 | * @api public
|
---|
27 | */
|
---|
28 |
|
---|
29 | function brackets(pattern, options) {
|
---|
30 | debug('initializing from <%s>', __filename);
|
---|
31 | var res = brackets.create(pattern, options);
|
---|
32 | return res.output;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Takes an array of strings and a POSIX character class pattern, and returns a new
|
---|
37 | * array with only the strings that matched the pattern.
|
---|
38 | *
|
---|
39 | * ```js
|
---|
40 | * var brackets = require('expand-brackets');
|
---|
41 | * console.log(brackets.match(['1', 'a', 'ab'], '[[:alpha:]]'));
|
---|
42 | * //=> ['a']
|
---|
43 | *
|
---|
44 | * console.log(brackets.match(['1', 'a', 'ab'], '[[:alpha:]]+'));
|
---|
45 | * //=> ['a', 'ab']
|
---|
46 | * ```
|
---|
47 | * @param {Array} `arr` Array of strings to match
|
---|
48 | * @param {String} `pattern` POSIX character class pattern(s)
|
---|
49 | * @param {Object} `options`
|
---|
50 | * @return {Array}
|
---|
51 | * @api public
|
---|
52 | */
|
---|
53 |
|
---|
54 | brackets.match = function(arr, pattern, options) {
|
---|
55 | arr = [].concat(arr);
|
---|
56 | var opts = extend({}, options);
|
---|
57 | var isMatch = brackets.matcher(pattern, opts);
|
---|
58 | var len = arr.length;
|
---|
59 | var idx = -1;
|
---|
60 | var res = [];
|
---|
61 |
|
---|
62 | while (++idx < len) {
|
---|
63 | var ele = arr[idx];
|
---|
64 | if (isMatch(ele)) {
|
---|
65 | res.push(ele);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (res.length === 0) {
|
---|
70 | if (opts.failglob === true) {
|
---|
71 | throw new Error('no matches found for "' + pattern + '"');
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (opts.nonull === true || opts.nullglob === true) {
|
---|
75 | return [pattern.split('\\').join('')];
|
---|
76 | }
|
---|
77 | }
|
---|
78 | return res;
|
---|
79 | };
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Returns true if the specified `string` matches the given
|
---|
83 | * brackets `pattern`.
|
---|
84 | *
|
---|
85 | * ```js
|
---|
86 | * var brackets = require('expand-brackets');
|
---|
87 | *
|
---|
88 | * console.log(brackets.isMatch('a.a', '[[:alpha:]].[[:alpha:]]'));
|
---|
89 | * //=> true
|
---|
90 | * console.log(brackets.isMatch('1.2', '[[:alpha:]].[[:alpha:]]'));
|
---|
91 | * //=> false
|
---|
92 | * ```
|
---|
93 | * @param {String} `string` String to match
|
---|
94 | * @param {String} `pattern` Poxis pattern
|
---|
95 | * @param {String} `options`
|
---|
96 | * @return {Boolean}
|
---|
97 | * @api public
|
---|
98 | */
|
---|
99 |
|
---|
100 | brackets.isMatch = function(str, pattern, options) {
|
---|
101 | return brackets.matcher(pattern, options)(str);
|
---|
102 | };
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Takes a POSIX character class pattern and returns a matcher function. The returned
|
---|
106 | * function takes the string to match as its only argument.
|
---|
107 | *
|
---|
108 | * ```js
|
---|
109 | * var brackets = require('expand-brackets');
|
---|
110 | * var isMatch = brackets.matcher('[[:lower:]].[[:upper:]]');
|
---|
111 | *
|
---|
112 | * console.log(isMatch('a.a'));
|
---|
113 | * //=> false
|
---|
114 | * console.log(isMatch('a.A'));
|
---|
115 | * //=> true
|
---|
116 | * ```
|
---|
117 | * @param {String} `pattern` Poxis pattern
|
---|
118 | * @param {String} `options`
|
---|
119 | * @return {Boolean}
|
---|
120 | * @api public
|
---|
121 | */
|
---|
122 |
|
---|
123 | brackets.matcher = function(pattern, options) {
|
---|
124 | var re = brackets.makeRe(pattern, options);
|
---|
125 | return function(str) {
|
---|
126 | return re.test(str);
|
---|
127 | };
|
---|
128 | };
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Create a regular expression from the given `pattern`.
|
---|
132 | *
|
---|
133 | * ```js
|
---|
134 | * var brackets = require('expand-brackets');
|
---|
135 | * var re = brackets.makeRe('[[:alpha:]]');
|
---|
136 | * console.log(re);
|
---|
137 | * //=> /^(?:[a-zA-Z])$/
|
---|
138 | * ```
|
---|
139 | * @param {String} `pattern` The pattern to convert to regex.
|
---|
140 | * @param {Object} `options`
|
---|
141 | * @return {RegExp}
|
---|
142 | * @api public
|
---|
143 | */
|
---|
144 |
|
---|
145 | brackets.makeRe = function(pattern, options) {
|
---|
146 | var res = brackets.create(pattern, options);
|
---|
147 | var opts = extend({strictErrors: false}, options);
|
---|
148 | return toRegex(res.output, opts);
|
---|
149 | };
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Parses the given POSIX character class `pattern` and returns an object
|
---|
153 | * with the compiled `output` and optional source `map`.
|
---|
154 | *
|
---|
155 | * ```js
|
---|
156 | * var brackets = require('expand-brackets');
|
---|
157 | * console.log(brackets('[[:alpha:]]'));
|
---|
158 | * // { options: { source: 'string' },
|
---|
159 | * // input: '[[:alpha:]]',
|
---|
160 | * // state: {},
|
---|
161 | * // compilers:
|
---|
162 | * // { eos: [Function],
|
---|
163 | * // noop: [Function],
|
---|
164 | * // bos: [Function],
|
---|
165 | * // not: [Function],
|
---|
166 | * // escape: [Function],
|
---|
167 | * // text: [Function],
|
---|
168 | * // posix: [Function],
|
---|
169 | * // bracket: [Function],
|
---|
170 | * // 'bracket.open': [Function],
|
---|
171 | * // 'bracket.inner': [Function],
|
---|
172 | * // 'bracket.literal': [Function],
|
---|
173 | * // 'bracket.close': [Function] },
|
---|
174 | * // output: '[a-zA-Z]',
|
---|
175 | * // ast:
|
---|
176 | * // { type: 'root',
|
---|
177 | * // errors: [],
|
---|
178 | * // nodes: [ [Object], [Object], [Object] ] },
|
---|
179 | * // parsingErrors: [] }
|
---|
180 | * ```
|
---|
181 | * @param {String} `pattern`
|
---|
182 | * @param {Object} `options`
|
---|
183 | * @return {Object}
|
---|
184 | * @api public
|
---|
185 | */
|
---|
186 |
|
---|
187 | brackets.create = function(pattern, options) {
|
---|
188 | var snapdragon = (options && options.snapdragon) || new Snapdragon(options);
|
---|
189 | compilers(snapdragon);
|
---|
190 | parsers(snapdragon);
|
---|
191 |
|
---|
192 | var ast = snapdragon.parse(pattern, options);
|
---|
193 | ast.input = pattern;
|
---|
194 | var res = snapdragon.compile(ast, options);
|
---|
195 | res.input = pattern;
|
---|
196 | return res;
|
---|
197 | };
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Expose `brackets` constructor, parsers and compilers
|
---|
201 | */
|
---|
202 |
|
---|
203 | brackets.compilers = compilers;
|
---|
204 | brackets.parsers = parsers;
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Expose `brackets`
|
---|
208 | * @type {Function}
|
---|
209 | */
|
---|
210 |
|
---|
211 | module.exports = brackets;
|
---|