balanced-match 1.0.2 → 3.0.0

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/README.md CHANGED
@@ -2,21 +2,19 @@
2
2
 
3
3
  Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regular expressions as well!
4
4
 
5
- [![build status](https://secure.travis-ci.org/juliangruber/balanced-match.svg)](http://travis-ci.org/juliangruber/balanced-match)
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
6
  [![downloads](https://img.shields.io/npm/dm/balanced-match.svg)](https://www.npmjs.org/package/balanced-match)
7
7
 
8
- [![testling badge](https://ci.testling.com/juliangruber/balanced-match.png)](https://ci.testling.com/juliangruber/balanced-match)
9
-
10
8
  ## Example
11
9
 
12
10
  Get the first matching pair of braces:
13
11
 
14
12
  ```js
15
- var balanced = require('balanced-match');
13
+ import balanced from 'balanced-match'
16
14
 
17
- console.log(balanced('{', '}', 'pre{in{nested}}post'));
18
- console.log(balanced('{', '}', 'pre{first}between{second}post'));
19
- console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'));
15
+ console.log(balanced('{', '}', 'pre{in{nested}}post'))
16
+ console.log(balanced('{', '}', 'pre{first}between{second}post'))
17
+ console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'))
20
18
  ```
21
19
 
22
20
  The matches are:
@@ -34,22 +32,22 @@ $ node example.js
34
32
 
35
33
  ## API
36
34
 
37
- ### var m = balanced(a, b, str)
35
+ ### const m = balanced(a, b, str)
38
36
 
39
37
  For the first non-nested matching pair of `a` and `b` in `str`, return an
40
38
  object with those keys:
41
39
 
42
- * **start** the index of the first match of `a`
43
- * **end** the index of the matching `b`
44
- * **pre** the preamble, `a` and `b` not included
45
- * **body** the match, `a` and `b` not included
46
- * **post** the postscript, `a` and `b` not included
40
+ - **start** the index of the first match of `a`
41
+ - **end** the index of the matching `b`
42
+ - **pre** the preamble, `a` and `b` not included
43
+ - **body** the match, `a` and `b` not included
44
+ - **post** the postscript, `a` and `b` not included
47
45
 
48
46
  If there's no match, `undefined` will be returned.
49
47
 
50
48
  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 `['{', 'a', '']` and `{a}}` will match `['', 'a', '}']`.
51
49
 
52
- ### var r = balanced.range(a, b, str)
50
+ ### const r = balanced.range(a, b, str)
53
51
 
54
52
  For the first non-nested matching pair of `a` and `b` in `str`, return an
55
53
  array with indexes: `[ <a index>, <b index> ]`.
package/index.js CHANGED
@@ -1,62 +1,75 @@
1
- 'use strict';
2
- module.exports = balanced;
3
- function balanced(a, b, str) {
4
- if (a instanceof RegExp) a = maybeMatch(a, str);
5
- if (b instanceof RegExp) b = maybeMatch(b, str);
6
-
7
- var r = range(a, b, str);
8
-
9
- return r && {
10
- start: r[0],
11
- end: r[1],
12
- pre: str.slice(0, r[0]),
13
- body: str.slice(r[0] + a.length, r[1]),
14
- post: str.slice(r[1] + b.length)
15
- };
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
+ )
16
21
  }
17
22
 
18
- function maybeMatch(reg, str) {
19
- var m = str.match(reg);
20
- return m ? m[0] : null;
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
21
30
  }
22
31
 
23
- balanced.range = range;
24
- function range(a, b, str) {
25
- var begs, beg, left, right, result;
26
- var ai = str.indexOf(a);
27
- var bi = str.indexOf(b, ai + 1);
28
- var i = ai;
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
29
42
 
30
43
  if (ai >= 0 && bi > 0) {
31
- if(a===b) {
32
- return [ai, bi];
44
+ if (a === b) {
45
+ return [ai, bi]
33
46
  }
34
- begs = [];
35
- left = str.length;
47
+ begs = []
48
+ left = str.length
36
49
 
37
50
  while (i >= 0 && !result) {
38
- if (i == ai) {
39
- begs.push(i);
40
- ai = str.indexOf(a, i + 1);
41
- } else if (begs.length == 1) {
42
- result = [ begs.pop(), bi ];
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]
43
56
  } else {
44
- beg = begs.pop();
57
+ beg = begs.pop()
45
58
  if (beg < left) {
46
- left = beg;
47
- right = bi;
59
+ left = beg
60
+ right = bi
48
61
  }
49
62
 
50
- bi = str.indexOf(b, i + 1);
63
+ bi = str.indexOf(b, i + 1)
51
64
  }
52
65
 
53
- i = ai < bi && ai >= 0 ? ai : bi;
66
+ i = ai < bi && ai >= 0 ? ai : bi
54
67
  }
55
68
 
56
69
  if (begs.length) {
57
- result = [ left, right ];
70
+ result = [left, right]
58
71
  }
59
72
  }
60
73
 
61
- return result;
74
+ return result
62
75
  }
package/package.json CHANGED
@@ -1,20 +1,24 @@
1
1
  {
2
2
  "name": "balanced-match",
3
3
  "description": "Match balanced character pairs, like \"{\" and \"}\"",
4
- "version": "1.0.2",
4
+ "version": "3.0.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git://github.com/juliangruber/balanced-match.git"
8
8
  },
9
9
  "homepage": "https://github.com/juliangruber/balanced-match",
10
10
  "main": "index.js",
11
+ "type": "module",
11
12
  "scripts": {
12
- "test": "tape test/test.js",
13
- "bench": "matcha test/bench.js"
13
+ "test": "standard --fix && node--test test/test.js",
14
+ "bench": "matcha test/bench.js",
15
+ "release": "np"
14
16
  },
15
17
  "devDependencies": {
16
- "matcha": "^0.7.0",
17
- "tape": "^4.6.0"
18
+ "@c4312/matcha": "^1.3.1",
19
+ "np": "^8.0.4",
20
+ "standard": "^17.1.0",
21
+ "test": "^3.3.0"
18
22
  },
19
23
  "keywords": [
20
24
  "match",
@@ -44,5 +48,8 @@
44
48
  "iphone/6.0..latest",
45
49
  "android-browser/4.2..latest"
46
50
  ]
51
+ },
52
+ "engines": {
53
+ "node": ">= 16"
47
54
  }
48
55
  }
@@ -1,2 +0,0 @@
1
- tidelift: "npm/balanced-match"
2
- patreon: juliangruber