1 | 'use strict';
|
---|
2 |
|
---|
3 | const stringify = require('./lib/stringify');
|
---|
4 | const compile = require('./lib/compile');
|
---|
5 | const expand = require('./lib/expand');
|
---|
6 | const parse = require('./lib/parse');
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Expand the given pattern or create a regex-compatible string.
|
---|
10 | *
|
---|
11 | * ```js
|
---|
12 | * const braces = require('braces');
|
---|
13 | * console.log(braces('{a,b,c}', { compile: true })); //=> ['(a|b|c)']
|
---|
14 | * console.log(braces('{a,b,c}')); //=> ['a', 'b', 'c']
|
---|
15 | * ```
|
---|
16 | * @param {String} `str`
|
---|
17 | * @param {Object} `options`
|
---|
18 | * @return {String}
|
---|
19 | * @api public
|
---|
20 | */
|
---|
21 |
|
---|
22 | const braces = (input, options = {}) => {
|
---|
23 | let output = [];
|
---|
24 |
|
---|
25 | if (Array.isArray(input)) {
|
---|
26 | for (let pattern of input) {
|
---|
27 | let result = braces.create(pattern, options);
|
---|
28 | if (Array.isArray(result)) {
|
---|
29 | output.push(...result);
|
---|
30 | } else {
|
---|
31 | output.push(result);
|
---|
32 | }
|
---|
33 | }
|
---|
34 | } else {
|
---|
35 | output = [].concat(braces.create(input, options));
|
---|
36 | }
|
---|
37 |
|
---|
38 | if (options && options.expand === true && options.nodupes === true) {
|
---|
39 | output = [...new Set(output)];
|
---|
40 | }
|
---|
41 | return output;
|
---|
42 | };
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Parse the given `str` with the given `options`.
|
---|
46 | *
|
---|
47 | * ```js
|
---|
48 | * // braces.parse(pattern, [, options]);
|
---|
49 | * const ast = braces.parse('a/{b,c}/d');
|
---|
50 | * console.log(ast);
|
---|
51 | * ```
|
---|
52 | * @param {String} pattern Brace pattern to parse
|
---|
53 | * @param {Object} options
|
---|
54 | * @return {Object} Returns an AST
|
---|
55 | * @api public
|
---|
56 | */
|
---|
57 |
|
---|
58 | braces.parse = (input, options = {}) => parse(input, options);
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Creates a braces string from an AST, or an AST node.
|
---|
62 | *
|
---|
63 | * ```js
|
---|
64 | * const braces = require('braces');
|
---|
65 | * let ast = braces.parse('foo/{a,b}/bar');
|
---|
66 | * console.log(stringify(ast.nodes[2])); //=> '{a,b}'
|
---|
67 | * ```
|
---|
68 | * @param {String} `input` Brace pattern or AST.
|
---|
69 | * @param {Object} `options`
|
---|
70 | * @return {Array} Returns an array of expanded values.
|
---|
71 | * @api public
|
---|
72 | */
|
---|
73 |
|
---|
74 | braces.stringify = (input, options = {}) => {
|
---|
75 | if (typeof input === 'string') {
|
---|
76 | return stringify(braces.parse(input, options), options);
|
---|
77 | }
|
---|
78 | return stringify(input, options);
|
---|
79 | };
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Compiles a brace pattern into a regex-compatible, optimized string.
|
---|
83 | * This method is called by the main [braces](#braces) function by default.
|
---|
84 | *
|
---|
85 | * ```js
|
---|
86 | * const braces = require('braces');
|
---|
87 | * console.log(braces.compile('a/{b,c}/d'));
|
---|
88 | * //=> ['a/(b|c)/d']
|
---|
89 | * ```
|
---|
90 | * @param {String} `input` Brace pattern or AST.
|
---|
91 | * @param {Object} `options`
|
---|
92 | * @return {Array} Returns an array of expanded values.
|
---|
93 | * @api public
|
---|
94 | */
|
---|
95 |
|
---|
96 | braces.compile = (input, options = {}) => {
|
---|
97 | if (typeof input === 'string') {
|
---|
98 | input = braces.parse(input, options);
|
---|
99 | }
|
---|
100 | return compile(input, options);
|
---|
101 | };
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Expands a brace pattern into an array. This method is called by the
|
---|
105 | * main [braces](#braces) function when `options.expand` is true. Before
|
---|
106 | * using this method it's recommended that you read the [performance notes](#performance))
|
---|
107 | * and advantages of using [.compile](#compile) instead.
|
---|
108 | *
|
---|
109 | * ```js
|
---|
110 | * const braces = require('braces');
|
---|
111 | * console.log(braces.expand('a/{b,c}/d'));
|
---|
112 | * //=> ['a/b/d', 'a/c/d'];
|
---|
113 | * ```
|
---|
114 | * @param {String} `pattern` Brace pattern
|
---|
115 | * @param {Object} `options`
|
---|
116 | * @return {Array} Returns an array of expanded values.
|
---|
117 | * @api public
|
---|
118 | */
|
---|
119 |
|
---|
120 | braces.expand = (input, options = {}) => {
|
---|
121 | if (typeof input === 'string') {
|
---|
122 | input = braces.parse(input, options);
|
---|
123 | }
|
---|
124 |
|
---|
125 | let result = expand(input, options);
|
---|
126 |
|
---|
127 | // filter out empty strings if specified
|
---|
128 | if (options.noempty === true) {
|
---|
129 | result = result.filter(Boolean);
|
---|
130 | }
|
---|
131 |
|
---|
132 | // filter out duplicates if specified
|
---|
133 | if (options.nodupes === true) {
|
---|
134 | result = [...new Set(result)];
|
---|
135 | }
|
---|
136 |
|
---|
137 | return result;
|
---|
138 | };
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Processes a brace pattern and returns either an expanded array
|
---|
142 | * (if `options.expand` is true), a highly optimized regex-compatible string.
|
---|
143 | * This method is called by the main [braces](#braces) function.
|
---|
144 | *
|
---|
145 | * ```js
|
---|
146 | * const braces = require('braces');
|
---|
147 | * console.log(braces.create('user-{200..300}/project-{a,b,c}-{1..10}'))
|
---|
148 | * //=> 'user-(20[0-9]|2[1-9][0-9]|300)/project-(a|b|c)-([1-9]|10)'
|
---|
149 | * ```
|
---|
150 | * @param {String} `pattern` Brace pattern
|
---|
151 | * @param {Object} `options`
|
---|
152 | * @return {Array} Returns an array of expanded values.
|
---|
153 | * @api public
|
---|
154 | */
|
---|
155 |
|
---|
156 | braces.create = (input, options = {}) => {
|
---|
157 | if (input === '' || input.length < 3) {
|
---|
158 | return [input];
|
---|
159 | }
|
---|
160 |
|
---|
161 | return options.expand !== true
|
---|
162 | ? braces.compile(input, options)
|
---|
163 | : braces.expand(input, options);
|
---|
164 | };
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Expose "braces"
|
---|
168 | */
|
---|
169 |
|
---|
170 | module.exports = braces;
|
---|