balanced-match 0.4.0 → 1.0.1

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,15 +39,15 @@ $ 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
 
50
- 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', '']`.
50
+ 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
51
 
52
52
  ### var r = balanced.range(a, b, str)
53
53
 
@@ -56,7 +56,7 @@ array with indexes: `[ <a index>, <b index> ]`.
56
56
 
57
57
  If there's no match, `undefined` will be returned.
58
58
 
59
- 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 ]`.
59
+ 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]`.
60
60
 
61
61
  ## Installation
62
62
 
@@ -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;
32
-
33
- while (i < str.length && 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 ];
33
+ if (a === b) {
34
+ return [ai, bi]
35
+ }
36
+ begs = []
37
+ left = str.length
38
+
39
+ while (i >= 0 && !result) {
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.0",
4
+ "version": "1.0.1",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git://github.com/juliangruber/balanced-match.git"
@@ -9,11 +9,16 @@
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": {
16
- "tape": "~4.5.0"
17
+ "@c4312/matcha": "^1.3.1",
18
+ "np": "^7.4.0",
19
+ "prettier-standard": "^16.4.1",
20
+ "standard": "^16.0.3",
21
+ "tape": "^4.6.0"
17
22
  },
18
23
  "keywords": [
19
24
  "match",
package/.npmignore DELETED
@@ -1,2 +0,0 @@
1
- node_modules
2
- .DS_Store
package/.travis.yml DELETED
@@ -1,3 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - "0.10"
package/Makefile DELETED
@@ -1,6 +0,0 @@
1
-
2
- test:
3
- @node_modules/.bin/tape test/*.js
4
-
5
- .PHONY: test
6
-
package/example.js DELETED
@@ -1,5 +0,0 @@
1
- var balanced = require('./');
2
-
3
- console.log(balanced('{', '}', 'pre{in{nested}}post'));
4
- console.log(balanced('{', '}', 'pre{first}between{second}post'));
5
-
package/test/balanced.js DELETED
@@ -1,92 +0,0 @@
1
- var test = require('tape');
2
- var balanced = require('..');
3
-
4
- test('balanced', function(t) {
5
- t.deepEqual(balanced('{', '}', 'pre{in{nest}}post'), {
6
- start: 3,
7
- end: 12,
8
- pre: 'pre',
9
- body: 'in{nest}',
10
- post: 'post'
11
- });
12
- t.deepEqual(balanced('{', '}', '{{{{{{{{{in}post'), {
13
- start: 8,
14
- end: 11,
15
- pre: '{{{{{{{{',
16
- body: 'in',
17
- post: 'post'
18
- });
19
- t.deepEqual(balanced('{', '}', 'pre{body{in}post'), {
20
- start: 8,
21
- end: 11,
22
- pre: 'pre{body',
23
- body: 'in',
24
- post: 'post'
25
- });
26
- t.deepEqual(balanced('{', '}', 'pre}{in{nest}}post'), {
27
- start: 4,
28
- end: 13,
29
- pre: 'pre}',
30
- body: 'in{nest}',
31
- post: 'post'
32
- });
33
- t.deepEqual(balanced('{', '}', 'pre{body}between{body2}post'), {
34
- start: 3,
35
- end: 8,
36
- pre: 'pre',
37
- body: 'body',
38
- post: 'between{body2}post'
39
- });
40
- t.notOk(balanced('{', '}', 'nope'), 'should be notOk');
41
- t.deepEqual(balanced('<b>', '</b>', 'pre<b>in<b>nest</b></b>post'), {
42
- start: 3,
43
- end: 19,
44
- pre: 'pre',
45
- body: 'in<b>nest</b>',
46
- post: 'post'
47
- });
48
- t.deepEqual(balanced('<b>', '</b>', 'pre</b><b>in<b>nest</b></b>post'), {
49
- start: 7,
50
- end: 23,
51
- pre: 'pre</b>',
52
- body: 'in<b>nest</b>',
53
- post: 'post'
54
- });
55
- t.deepEqual(balanced('{{', '}}', 'pre{{{in}}}post'), {
56
- start: 3,
57
- end: 9,
58
- pre: 'pre',
59
- body: '{in}',
60
- post: 'post'
61
- });
62
- t.deepEqual(balanced('{{{', '}}', 'pre{{{in}}}post'), {
63
- start: 3,
64
- end: 8,
65
- pre: 'pre',
66
- body: 'in',
67
- post: '}post'
68
- });
69
- t.deepEqual(balanced('{', '}', 'pre{{first}in{second}post'), {
70
- start: 4,
71
- end: 10,
72
- pre: 'pre{',
73
- body: 'first',
74
- post: 'in{second}post'
75
- });
76
- t.deepEqual(balanced('<?', '?>', 'pre<?>post'), {
77
- start: 3,
78
- end: 4,
79
- pre: 'pre',
80
- body: '',
81
- post: 'post'
82
- });
83
- t.notOk(balanced(/\{/, /\}/, 'nope'), 'should be notOk');
84
- t.deepEqual(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'), {
85
- start: 3,
86
- end: 17,
87
- pre: 'pre',
88
- body: 'in{nest}',
89
- post: 'post'
90
- });
91
- t.end();
92
- });