source: node_modules/brace-expansion/dist/esm/index.js@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 6.5 KB
Line 
1import { balanced } from 'balanced-match';
2const escSlash = '\0SLASH' + Math.random() + '\0';
3const escOpen = '\0OPEN' + Math.random() + '\0';
4const escClose = '\0CLOSE' + Math.random() + '\0';
5const escComma = '\0COMMA' + Math.random() + '\0';
6const escPeriod = '\0PERIOD' + Math.random() + '\0';
7const escSlashPattern = new RegExp(escSlash, 'g');
8const escOpenPattern = new RegExp(escOpen, 'g');
9const escClosePattern = new RegExp(escClose, 'g');
10const escCommaPattern = new RegExp(escComma, 'g');
11const escPeriodPattern = new RegExp(escPeriod, 'g');
12const slashPattern = /\\\\/g;
13const openPattern = /\\{/g;
14const closePattern = /\\}/g;
15const commaPattern = /\\,/g;
16const periodPattern = /\\./g;
17export const EXPANSION_MAX = 100_000;
18function numeric(str) {
19 return !isNaN(str) ? parseInt(str, 10) : str.charCodeAt(0);
20}
21function escapeBraces(str) {
22 return str
23 .replace(slashPattern, escSlash)
24 .replace(openPattern, escOpen)
25 .replace(closePattern, escClose)
26 .replace(commaPattern, escComma)
27 .replace(periodPattern, escPeriod);
28}
29function unescapeBraces(str) {
30 return str
31 .replace(escSlashPattern, '\\')
32 .replace(escOpenPattern, '{')
33 .replace(escClosePattern, '}')
34 .replace(escCommaPattern, ',')
35 .replace(escPeriodPattern, '.');
36}
37/**
38 * Basically just str.split(","), but handling cases
39 * where we have nested braced sections, which should be
40 * treated as individual members, like {a,{b,c},d}
41 */
42function parseCommaParts(str) {
43 if (!str) {
44 return [''];
45 }
46 const parts = [];
47 const m = balanced('{', '}', str);
48 if (!m) {
49 return str.split(',');
50 }
51 const { pre, body, post } = m;
52 const p = pre.split(',');
53 p[p.length - 1] += '{' + body + '}';
54 const postParts = parseCommaParts(post);
55 if (post.length) {
56 ;
57 p[p.length - 1] += postParts.shift();
58 p.push.apply(p, postParts);
59 }
60 parts.push.apply(parts, p);
61 return parts;
62}
63export function expand(str, options = {}) {
64 if (!str) {
65 return [];
66 }
67 const { max = EXPANSION_MAX } = options;
68 // I don't know why Bash 4.3 does this, but it does.
69 // Anything starting with {} will have the first two bytes preserved
70 // but *only* at the top level, so {},a}b will not expand to anything,
71 // but a{},b}c will be expanded to [a}c,abc].
72 // One could argue that this is a bug in Bash, but since the goal of
73 // this module is to match Bash's rules, we escape a leading {}
74 if (str.slice(0, 2) === '{}') {
75 str = '\\{\\}' + str.slice(2);
76 }
77 return expand_(escapeBraces(str), max, true).map(unescapeBraces);
78}
79function embrace(str) {
80 return '{' + str + '}';
81}
82function isPadded(el) {
83 return /^-?0\d/.test(el);
84}
85function lte(i, y) {
86 return i <= y;
87}
88function gte(i, y) {
89 return i >= y;
90}
91function expand_(str, max, isTop) {
92 /** @type {string[]} */
93 const expansions = [];
94 const m = balanced('{', '}', str);
95 if (!m)
96 return [str];
97 // no need to expand pre, since it is guaranteed to be free of brace-sets
98 const pre = m.pre;
99 const post = m.post.length ? expand_(m.post, max, false) : [''];
100 if (/\$$/.test(m.pre)) {
101 for (let k = 0; k < post.length && k < max; k++) {
102 const expansion = pre + '{' + m.body + '}' + post[k];
103 expansions.push(expansion);
104 }
105 }
106 else {
107 const isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
108 const isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
109 const isSequence = isNumericSequence || isAlphaSequence;
110 const isOptions = m.body.indexOf(',') >= 0;
111 if (!isSequence && !isOptions) {
112 // {a},b}
113 if (m.post.match(/,(?!,).*\}/)) {
114 str = m.pre + '{' + m.body + escClose + m.post;
115 return expand_(str, max, true);
116 }
117 return [str];
118 }
119 let n;
120 if (isSequence) {
121 n = m.body.split(/\.\./);
122 }
123 else {
124 n = parseCommaParts(m.body);
125 if (n.length === 1 && n[0] !== undefined) {
126 // x{{a,b}}y ==> x{a}y x{b}y
127 n = expand_(n[0], max, false).map(embrace);
128 //XXX is this necessary? Can't seem to hit it in tests.
129 /* c8 ignore start */
130 if (n.length === 1) {
131 return post.map(p => m.pre + n[0] + p);
132 }
133 /* c8 ignore stop */
134 }
135 }
136 // at this point, n is the parts, and we know it's not a comma set
137 // with a single entry.
138 let N;
139 if (isSequence && n[0] !== undefined && n[1] !== undefined) {
140 const x = numeric(n[0]);
141 const y = numeric(n[1]);
142 const width = Math.max(n[0].length, n[1].length);
143 let incr = n.length === 3 && n[2] !== undefined ? Math.abs(numeric(n[2])) : 1;
144 let test = lte;
145 const reverse = y < x;
146 if (reverse) {
147 incr *= -1;
148 test = gte;
149 }
150 const pad = n.some(isPadded);
151 N = [];
152 for (let i = x; test(i, y); i += incr) {
153 let c;
154 if (isAlphaSequence) {
155 c = String.fromCharCode(i);
156 if (c === '\\') {
157 c = '';
158 }
159 }
160 else {
161 c = String(i);
162 if (pad) {
163 const need = width - c.length;
164 if (need > 0) {
165 const z = new Array(need + 1).join('0');
166 if (i < 0) {
167 c = '-' + z + c.slice(1);
168 }
169 else {
170 c = z + c;
171 }
172 }
173 }
174 }
175 N.push(c);
176 }
177 }
178 else {
179 N = [];
180 for (let j = 0; j < n.length; j++) {
181 N.push.apply(N, expand_(n[j], max, false));
182 }
183 }
184 for (let j = 0; j < N.length; j++) {
185 for (let k = 0; k < post.length && expansions.length < max; k++) {
186 const expansion = pre + N[j] + post[k];
187 if (!isTop || isSequence || expansion) {
188 expansions.push(expansion);
189 }
190 }
191 }
192 }
193 return expansions;
194}
195//# sourceMappingURL=index.js.map
Note: See TracBrowser for help on using the repository browser.