Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
index.js54 linesDownload Raw Back to esm
1export const balanced = (a, b, str) => {
2    const ma = a instanceof RegExp ? maybeMatch(a, str) : a;
3    const mb = b instanceof RegExp ? maybeMatch(b, str) : b;
4    const r = ma !== null && mb != null && range(ma, mb, str);
5    return (r && {
6        start: r[0],
7        end: r[1],
8        pre: str.slice(0, r[0]),
9        body: str.slice(r[0] + ma.length, r[1]),
10        post: str.slice(r[1] + mb.length),
11    });
12};
13const maybeMatch = (reg, str) => {
14    const m = str.match(reg);
15    return m ? m[0] : null;
16};
17export const range = (a, b, str) => {
18    let begs, beg, left, right = undefined, result;
19    let ai = str.indexOf(a);
20    let bi = str.indexOf(b, ai + 1);
21    let i = ai;
22    if (ai >= 0 && bi > 0) {
23        if (a === b) {
24            return [ai, bi];
25        }
26        begs = [];
27        left = str.length;
28        while (i >= 0 && !result) {
29            if (i === ai) {
30                begs.push(i);
31                ai = str.indexOf(a, i + 1);
32            }
33            else if (begs.length === 1) {
34                const r = begs.pop();
35                if (r !== undefined)
36                    result = [r, bi];
37            }
38            else {
39                beg = begs.pop();
40                if (beg !== undefined && beg < left) {
41                    left = beg;
42                    right = bi;
43                }
44                bi = str.indexOf(b, i + 1);
45            }
46            i = ai < bi && ai >= 0 ? ai : bi;
47        }
48        if (begs.length && right !== undefined) {
49            result = [left, right];
50        }
51    }
52    return result;
53};
54//# sourceMappingURL=index.js.map
codekingpro/portable-devtools · Team Ai