Team Ai
Datasetpublic

codekingpro/portable-devtools

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