balanced-match 3.0.0 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md CHANGED
@@ -1,6 +1,8 @@
1
1
  (MIT)
2
2
 
3
- Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
3
+ Original code Copyright Julian Gruber <julian@juliangruber.com>
4
+
5
+ Port to TypeScript Copyright Isaac Z. Schlueter <i@izs.me>
4
6
 
5
7
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
8
  this software and associated documentation files (the "Software"), to deal in
package/README.md CHANGED
@@ -1,20 +1,20 @@
1
1
  # balanced-match
2
2
 
3
- Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regular expressions as well!
4
-
5
- [![CI](https://github.com/juliangruber/balanced-match/actions/workflows/ci.yml/badge.svg)](https://github.com/juliangruber/balanced-match/actions/workflows/ci.yml)
6
- [![downloads](https://img.shields.io/npm/dm/balanced-match.svg)](https://www.npmjs.org/package/balanced-match)
3
+ Match balanced string pairs, like `{` and `}` or `<b>` and
4
+ `</b>`. Supports regular expressions as well!
7
5
 
8
6
  ## Example
9
7
 
10
8
  Get the first matching pair of braces:
11
9
 
12
10
  ```js
13
- import balanced from 'balanced-match'
11
+ import { balanced } from 'balanced-match'
14
12
 
15
13
  console.log(balanced('{', '}', 'pre{in{nested}}post'))
16
14
  console.log(balanced('{', '}', 'pre{first}between{second}post'))
17
- console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'))
15
+ console.log(
16
+ balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'),
17
+ )
18
18
  ```
19
19
 
20
20
  The matches are:
@@ -55,41 +55,3 @@ array with indexes: `[ <a index>, <b index> ]`.
55
55
  If there's no match, `undefined` will be returned.
56
56
 
57
57
  If the `str` contains more `a` than `b` / there are unmatched pairs, the first match that was closed will be used. For example, `{{a}` will match `[ 1, 3 ]` and `{a}}` will match `[0, 2]`.
58
-
59
- ## Installation
60
-
61
- With [npm](https://npmjs.org) do:
62
-
63
- ```bash
64
- npm install balanced-match
65
- ```
66
-
67
- ## Security contact information
68
-
69
- To report a security vulnerability, please use the
70
- [Tidelift security contact](https://tidelift.com/security).
71
- Tidelift will coordinate the fix and disclosure.
72
-
73
- ## License
74
-
75
- (MIT)
76
-
77
- Copyright (c) 2013 Julian Gruber &lt;julian@juliangruber.com&gt;
78
-
79
- Permission is hereby granted, free of charge, to any person obtaining a copy of
80
- this software and associated documentation files (the "Software"), to deal in
81
- the Software without restriction, including without limitation the rights to
82
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
83
- of the Software, and to permit persons to whom the Software is furnished to do
84
- so, subject to the following conditions:
85
-
86
- The above copyright notice and this permission notice shall be included in all
87
- copies or substantial portions of the Software.
88
-
89
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
90
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
91
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
92
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
93
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
94
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
95
- SOFTWARE.
@@ -0,0 +1,9 @@
1
+ export declare const balanced: (a: string | RegExp, b: string | RegExp, str: string) => false | {
2
+ start: number;
3
+ end: number;
4
+ pre: string;
5
+ body: string;
6
+ post: string;
7
+ } | undefined;
8
+ export declare const range: (a: string, b: string, str: string) => undefined | [number, number];
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,GACnB,GAAG,MAAM,GAAG,MAAM,EAClB,GAAG,MAAM,GAAG,MAAM,EAClB,KAAK,MAAM;;;;;;aAgBZ,CAAA;AAOD,eAAO,MAAM,KAAK,GAChB,GAAG,MAAM,EACT,GAAG,MAAM,EACT,KAAK,MAAM,KACV,SAAS,GAAG,CAAC,MAAM,EAAE,MAAM,CA2C7B,CAAA"}
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.range = exports.balanced = void 0;
4
+ const 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
+ };
16
+ exports.balanced = balanced;
17
+ const maybeMatch = (reg, str) => {
18
+ const m = str.match(reg);
19
+ return m ? m[0] : null;
20
+ };
21
+ const 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
+ };
58
+ exports.range = range;
59
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAO,MAAM,QAAQ,GAAG,CACtB,CAAkB,EAClB,CAAkB,EAClB,GAAW,EACX,EAAE;IACF,MAAM,EAAE,GAAG,CAAC,YAAY,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACvD,MAAM,EAAE,GAAG,CAAC,YAAY,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAEvD,MAAM,CAAC,GAAG,EAAE,KAAK,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,IAAA,aAAK,EAAC,EAAE,EAAE,EAAE,EAAE,GAAG,CAAC,CAAA;IAEzD,OAAO,CACL,CAAC,IAAI;QACH,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;QACX,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;QACT,GAAG,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACvB,IAAI,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACvC,IAAI,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC;KAClC,CACF,CAAA;AACH,CAAC,CAAA;AAnBY,QAAA,QAAQ,YAmBpB;AAED,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,GAAW,EAAE,EAAE;IAC9C,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,GA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AA;gBAChB,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,GAAG,IAAI,EAAE,CAAC;oBACpC,IAAI,GAAG,GAAG,CAAA;oBACV,KAAK,GAAG,EAAE,CAAA;gBACZ,CAAC;gBAED,EAAE,GAAG,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;YAC5B,CAAC;YAED,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;QAClC,CAAC;QAED,IAAI,IAAI,CAAC,MAAM,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACvC,MAAM,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;QACxB,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AA/CY,QAAA,KAAK,SA+CjB","sourcesContent":["export const balanced = (\n a: string | RegExp,\n b: string | RegExp,\n str: string,\n) => {\n const ma = a instanceof RegExp ? maybeMatch(a, str) : a\n const mb = b instanceof RegExp ? maybeMatch(b, str) : b\n\n const r = ma !== null && mb != null && range(ma, mb, str)\n\n return (\n r && {\n start: r[0],\n end: r[1],\n pre: str.slice(0, r[0]),\n body: str.slice(r[0] + ma.length, r[1]),\n post: str.slice(r[1] + mb.length),\n }\n )\n}\n\nconst maybeMatch = (reg: RegExp, str: string) => {\n const m = str.match(reg)\n return m ? m[0] : null\n}\n\nexport const range = (\n a: string,\n b: string,\n str: string,\n): undefined | [number, number] => {\n let begs: number[],\n beg: number | undefined,\n left: number,\n right: number | undefined = undefined,\n result: undefined | [number, number]\n let ai = str.indexOf(a)\n let bi = str.indexOf(b, ai + 1)\n let i = ai\n\n if (ai >= 0 && bi > 0) {\n if (a === b) {\n return [ai, bi]\n }\n begs = []\n left = str.length\n\n while (i >= 0 && !result) {\n if (i === ai) {\n begs.push(i)\n ai = str.indexOf(a, i + 1)\n } else if (begs.length === 1) {\n const r = begs.pop()\n if (r !== undefined) result = [r, bi]\n } else {\n beg = begs.pop()\n if (beg !== undefined && beg < left) {\n left = beg\n right = bi\n }\n\n bi = str.indexOf(b, i + 1)\n }\n\n i = ai < bi && ai >= 0 ? ai : bi\n }\n\n if (begs.length && right !== undefined) {\n result = [left, right]\n }\n }\n\n return result\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,9 @@
1
+ export declare const balanced: (a: string | RegExp, b: string | RegExp, str: string) => false | {
2
+ start: number;
3
+ end: number;
4
+ pre: string;
5
+ body: string;
6
+ post: string;
7
+ } | undefined;
8
+ export declare const range: (a: string, b: string, str: string) => undefined | [number, number];
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,GACnB,GAAG,MAAM,GAAG,MAAM,EAClB,GAAG,MAAM,GAAG,MAAM,EAClB,KAAK,MAAM;;;;;;aAgBZ,CAAA;AAOD,eAAO,MAAM,KAAK,GAChB,GAAG,MAAM,EACT,GAAG,MAAM,EACT,KAAK,MAAM,KACV,SAAS,GAAG,CAAC,MAAM,EAAE,MAAM,CA2C7B,CAAA"}
@@ -0,0 +1,54 @@
1
+ export 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
+ };
13
+ const maybeMatch = (reg, str) => {
14
+ const m = str.match(reg);
15
+ return m ? m[0] : null;
16
+ };
17
+ export 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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,QAAQ,GAAG,CACtB,CAAkB,EAClB,CAAkB,EAClB,GAAW,EACX,EAAE;IACF,MAAM,EAAE,GAAG,CAAC,YAAY,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACvD,MAAM,EAAE,GAAG,CAAC,YAAY,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAEvD,MAAM,CAAC,GAAG,EAAE,KAAK,IAAI,IAAI,EAAE,IAAI,IAAI,IAAI,KAAK,CAAC,EAAE,EAAE,EAAE,EAAE,GAAG,CAAC,CAAA;IAEzD,OAAO,CACL,CAAC,IAAI;QACH,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;QACX,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;QACT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YACzB,IAAI,CAAC,KAAK,EAAE,EAAE,CAAC;gBACb,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;gBACZ,EAAE,GAAG,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;YAC5B,CAAC;iBAAM,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBAC7B,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;gBACpB,IAAI,CAAC,KAAK,SAAS;oBAAE,MAAM,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;YACvC,CAAC;iBAAM,CAAC;gBACN,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;gBAChB,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,GAAG,IAAI,EAAE,CAAC;oBACpC,IAAI,GAAG,GAAG,CAAA;oBACV,KAAK,GAAG,EAAE,CAAA;gBACZ,CAAC;gBAED,EAAE,GAAG,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;YAC5B,CAAC;YAED,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;QAClC,CAAC;QAED,IAAI,IAAI,CAAC,MAAM,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACvC,MAAM,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;QACxB,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA","sourcesContent":["export const balanced = (\n a: string | RegExp,\n b: string | RegExp,\n str: string,\n) => {\n const ma = a instanceof RegExp ? maybeMatch(a, str) : a\n const mb = b instanceof RegExp ? maybeMatch(b, str) : b\n\n const r = ma !== null && mb != null && range(ma, mb, str)\n\n return (\n r && {\n start: r[0],\n end: r[1],\n pre: str.slice(0, r[0]),\n body: str.slice(r[0] + ma.length, r[1]),\n post: str.slice(r[1] + mb.length),\n }\n )\n}\n\nconst maybeMatch = (reg: RegExp, str: string) => {\n const m = str.match(reg)\n return m ? m[0] : null\n}\n\nexport const range = (\n a: string,\n b: string,\n str: string,\n): undefined | [number, number] => {\n let begs: number[],\n beg: number | undefined,\n left: number,\n right: number | undefined = undefined,\n result: undefined | [number, number]\n let ai = str.indexOf(a)\n let bi = str.indexOf(b, ai + 1)\n let i = ai\n\n if (ai >= 0 && bi > 0) {\n if (a === b) {\n return [ai, bi]\n }\n begs = []\n left = str.length\n\n while (i >= 0 && !result) {\n if (i === ai) {\n begs.push(i)\n ai = str.indexOf(a, i + 1)\n } else if (begs.length === 1) {\n const r = begs.pop()\n if (r !== undefined) result = [r, bi]\n } else {\n beg = begs.pop()\n if (beg !== undefined && beg < left) {\n left = beg\n right = bi\n }\n\n bi = str.indexOf(b, i + 1)\n }\n\n i = ai < bi && ai >= 0 ? ai : bi\n }\n\n if (begs.length && right !== undefined) {\n result = [left, right]\n }\n }\n\n return result\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json CHANGED
@@ -1,24 +1,49 @@
1
1
  {
2
2
  "name": "balanced-match",
3
3
  "description": "Match balanced character pairs, like \"{\" and \"}\"",
4
- "version": "3.0.0",
4
+ "version": "4.0.2",
5
+ "files": [
6
+ "dist"
7
+ ],
5
8
  "repository": {
6
9
  "type": "git",
7
10
  "url": "git://github.com/juliangruber/balanced-match.git"
8
11
  },
9
- "homepage": "https://github.com/juliangruber/balanced-match",
10
- "main": "index.js",
12
+ "exports": {
13
+ "./package.json": "./package.json",
14
+ ".": {
15
+ "import": {
16
+ "types": "./dist/esm/index.d.ts",
17
+ "default": "./dist/esm/index.js"
18
+ },
19
+ "require": {
20
+ "types": "./dist/commonjs/index.d.ts",
21
+ "default": "./dist/commonjs/index.js"
22
+ }
23
+ }
24
+ },
11
25
  "type": "module",
12
26
  "scripts": {
13
- "test": "standard --fix && node--test test/test.js",
14
- "bench": "matcha test/bench.js",
15
- "release": "np"
27
+ "preversion": "npm test",
28
+ "postversion": "npm publish",
29
+ "prepublishOnly": "git push origin --follow-tags",
30
+ "prepare": "tshy",
31
+ "pretest": "npm run prepare",
32
+ "presnap": "npm run prepare",
33
+ "test": "tap",
34
+ "snap": "tap",
35
+ "format": "prettier --write .",
36
+ "benchmark": "node benchmark/index.js",
37
+ "typedoc": "typedoc --tsconfig .tshy/esm.json ./src/*.ts"
16
38
  },
17
39
  "devDependencies": {
18
- "@c4312/matcha": "^1.3.1",
19
- "np": "^8.0.4",
20
- "standard": "^17.1.0",
21
- "test": "^3.3.0"
40
+ "@types/brace-expansion": "^1.1.2",
41
+ "@types/node": "^25.2.1",
42
+ "mkdirp": "^3.0.1",
43
+ "prettier": "^3.3.2",
44
+ "tap": "^21.1.0",
45
+ "tshy": "^3.0.2",
46
+ "typedoc": "^0.28.5"
22
47
  },
23
48
  "keywords": [
24
49
  "match",
@@ -27,29 +52,20 @@
27
52
  "balanced",
28
53
  "parse"
29
54
  ],
30
- "author": {
31
- "name": "Julian Gruber",
32
- "email": "mail@juliangruber.com",
33
- "url": "http://juliangruber.com"
34
- },
35
55
  "license": "MIT",
36
- "testling": {
37
- "files": "test/*.js",
38
- "browsers": [
39
- "ie/8..latest",
40
- "firefox/20..latest",
41
- "firefox/nightly",
42
- "chrome/25..latest",
43
- "chrome/canary",
44
- "opera/12..latest",
45
- "opera/next",
46
- "safari/5.1..latest",
47
- "ipad/6.0..latest",
48
- "iphone/6.0..latest",
49
- "android-browser/4.2..latest"
50
- ]
51
- },
52
56
  "engines": {
53
- "node": ">= 16"
57
+ "node": "20 || >=22"
58
+ },
59
+ "tshy": {
60
+ "exports": {
61
+ "./package.json": "./package.json",
62
+ ".": "./src/index.ts"
63
+ }
64
+ },
65
+ "main": "./dist/commonjs/index.js",
66
+ "types": "./dist/commonjs/index.d.ts",
67
+ "module": "./dist/esm/index.js",
68
+ "dependencies": {
69
+ "jackspeak": "^4.2.3"
54
70
  }
55
71
  }
package/index.js DELETED
@@ -1,75 +0,0 @@
1
- /**
2
- * @param {string | RegExp} a
3
- * @param {string | RegExp} b
4
- * @param {string} str
5
- */
6
- export default function balanced (a, b, str) {
7
- if (a instanceof RegExp) a = maybeMatch(a, str)
8
- if (b instanceof RegExp) b = maybeMatch(b, str)
9
-
10
- const r = range(a, b, str)
11
-
12
- return (
13
- r && {
14
- start: r[0],
15
- end: r[1],
16
- pre: str.slice(0, r[0]),
17
- body: str.slice(r[0] + a.length, r[1]),
18
- post: str.slice(r[1] + b.length)
19
- }
20
- )
21
- }
22
-
23
- /**
24
- * @param {RegExp} reg
25
- * @param {string} str
26
- */
27
- function maybeMatch (reg, str) {
28
- const m = str.match(reg)
29
- return m ? m[0] : null
30
- }
31
-
32
- /**
33
- * @param {string} a
34
- * @param {string} b
35
- * @param {string} str
36
- */
37
- export function range (a, b, str) {
38
- let begs, beg, left, right, result
39
- let ai = str.indexOf(a)
40
- let bi = str.indexOf(b, ai + 1)
41
- let i = ai
42
-
43
- if (ai >= 0 && bi > 0) {
44
- if (a === b) {
45
- return [ai, bi]
46
- }
47
- begs = []
48
- left = str.length
49
-
50
- while (i >= 0 && !result) {
51
- if (i === ai) {
52
- begs.push(i)
53
- ai = str.indexOf(a, i + 1)
54
- } else if (begs.length === 1) {
55
- result = [begs.pop(), bi]
56
- } else {
57
- beg = begs.pop()
58
- if (beg < left) {
59
- left = beg
60
- right = bi
61
- }
62
-
63
- bi = str.indexOf(b, i + 1)
64
- }
65
-
66
- i = ai < bi && ai >= 0 ? ai : bi
67
- }
68
-
69
- if (begs.length) {
70
- result = [left, right]
71
- }
72
- }
73
-
74
- return result
75
- }