Team Ai
Datasetpublic

codekingpro/portable-devtools

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