codekingpro/portable-devtools
114k
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 ?
144 Math.max(Math.abs(numeric(n[2])), 1)
145 : 1;
146 let test = lte;
147 const reverse = y < x;
148 if (reverse) {
149 incr *= -1;
150 test = gte;
151 }
152 const pad = n.some(isPadded);
153 N = [];
154 for (let i = x; test(i, y); i += incr) {
155 let c;
156 if (isAlphaSequence) {
157 c = String.fromCharCode(i);
158 if (c === '\\') {
159 c = '';
160 }
161 }
162 else {
163 c = String(i);
164 if (pad) {
165 const need = width - c.length;
166 if (need > 0) {
167 const z = new Array(need + 1).join('0');
168 if (i < 0) {
169 c = '-' + z + c.slice(1);
170 }
171 else {
172 c = z + c;
173 }
174 }
175 }
176 }
177 N.push(c);
178 }
179 }
180 else {
181 N = [];
182 for (let j = 0; j < n.length; j++) {
183 N.push.apply(N, expand_(n[j], max, false));
184 }
185 }
186 for (let j = 0; j < N.length; j++) {
187 for (let k = 0; k < post.length && expansions.length < max; k++) {
188 const expansion = pre + N[j] + post[k];
189 if (!isTop || isSequence || expansion) {
190 expansions.push(expansion);
191 }
192 }
193 }
194 }
195 return expansions;
196}
197//# sourceMappingURL=index.js.map