balanced-match 0.4.2 → 2.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.
@@ -0,0 +1,2 @@
1
+ tidelift: 'npm/balanced-match'
2
+ patreon: juliangruber
package/README.md CHANGED
@@ -12,11 +12,11 @@ Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regu
12
12
  Get the first matching pair of braces:
13
13
 
14
14
  ```js
15
- var balanced = require('balanced-match');
15
+ var balanced = require('balanced-match')
16
16
 
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'));
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'))
20
20
  ```
21
21
 
22
22
  The matches are:
@@ -39,11 +39,11 @@ $ node example.js
39
39
  For the first non-nested matching pair of `a` and `b` in `str`, return an
40
40
  object with those keys:
41
41
 
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
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
47
47
 
48
48
  If there's no match, `undefined` will be returned.
49
49
 
@@ -66,6 +66,12 @@ With [npm](https://npmjs.org) do:
66
66
  npm install balanced-match
67
67
  ```
68
68
 
69
+ ## Security contact information
70
+
71
+ To report a security vulnerability, please use the
72
+ [Tidelift security contact](https://tidelift.com/security).
73
+ Tidelift will coordinate the fix and disclosure.
74
+
69
75
  ## License
70
76
 
71
77
  (MIT)
package/index.js CHANGED
@@ -1,58 +1,64 @@
1
- module.exports = balanced;
2
- function balanced(a, b, str) {
3
- if (a instanceof RegExp) a = maybeMatch(a, str);
4
- if (b instanceof RegExp) b = maybeMatch(b, str);
5
-
6
- var r = range(a, b, str);
7
-
8
- return r && {
9
- start: r[0],
10
- end: r[1],
11
- pre: str.slice(0, r[0]),
12
- body: str.slice(r[0] + a.length, r[1]),
13
- post: str.slice(r[1] + b.length)
14
- };
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
+ const r = range(a, b, str)
8
+
9
+ return (
10
+ r && {
11
+ start: r[0],
12
+ end: r[1],
13
+ pre: str.slice(0, r[0]),
14
+ body: str.slice(r[0] + a.length, r[1]),
15
+ post: str.slice(r[1] + b.length)
16
+ }
17
+ )
15
18
  }
16
19
 
17
- function maybeMatch(reg, str) {
18
- var m = str.match(reg);
19
- return m ? m[0] : null;
20
+ function maybeMatch (reg, str) {
21
+ const m = str.match(reg)
22
+ return m ? m[0] : null
20
23
  }
21
24
 
22
- balanced.range = range;
23
- function range(a, b, str) {
24
- var begs, beg, left, right, result;
25
- var ai = str.indexOf(a);
26
- var bi = str.indexOf(b, ai + 1);
27
- var i = ai;
25
+ balanced.range = range
26
+ function range (a, b, str) {
27
+ let begs, beg, left, right, result
28
+ let ai = str.indexOf(a)
29
+ let bi = str.indexOf(b, ai + 1)
30
+ let i = ai
28
31
 
29
32
  if (ai >= 0 && bi > 0) {
30
- begs = [];
31
- left = str.length;
33
+ if (a === b) {
34
+ return [ai, bi]
35
+ }
36
+ begs = []
37
+ left = str.length
32
38
 
33
39
  while (i >= 0 && !result) {
34
- if (i == ai) {
35
- begs.push(i);
36
- ai = str.indexOf(a, i + 1);
37
- } else if (begs.length == 1) {
38
- result = [ begs.pop(), bi ];
40
+ if (i === ai) {
41
+ begs.push(i)
42
+ ai = str.indexOf(a, i + 1)
43
+ } else if (begs.length === 1) {
44
+ result = [begs.pop(), bi]
39
45
  } else {
40
- beg = begs.pop();
46
+ beg = begs.pop()
41
47
  if (beg < left) {
42
- left = beg;
43
- right = bi;
48
+ left = beg
49
+ right = bi
44
50
  }
45
51
 
46
- bi = str.indexOf(b, i + 1);
52
+ bi = str.indexOf(b, i + 1)
47
53
  }
48
54
 
49
- i = ai < bi && ai >= 0 ? ai : bi;
55
+ i = ai < bi && ai >= 0 ? ai : bi
50
56
  }
51
57
 
52
58
  if (begs.length) {
53
- result = [ left, right ];
59
+ result = [left, right]
54
60
  }
55
61
  }
56
62
 
57
- return result;
63
+ return result
58
64
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "balanced-match",
3
3
  "description": "Match balanced character pairs, like \"{\" and \"}\"",
4
- "version": "0.4.2",
4
+ "version": "2.0.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git://github.com/juliangruber/balanced-match.git"
@@ -9,10 +9,15 @@
9
9
  "homepage": "https://github.com/juliangruber/balanced-match",
10
10
  "main": "index.js",
11
11
  "scripts": {
12
- "test": "make test"
12
+ "test": "prettier-standard && standard && tape test/test.js",
13
+ "bench": "matcha test/bench.js",
14
+ "release": "np"
13
15
  },
14
- "dependencies": {},
15
16
  "devDependencies": {
17
+ "@c4312/matcha": "^1.3.1",
18
+ "np": "^7.4.0",
19
+ "prettier-standard": "^16.4.1",
20
+ "standard": "^16.0.3",
16
21
  "tape": "^4.6.0"
17
22
  },
18
23
  "keywords": [
package/.npmignore DELETED
@@ -1,5 +0,0 @@
1
- test
2
- .gitignore
3
- .travis.yml
4
- Makefile
5
- example.js