balanced-match 1.0.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.
- package/.github/FUNDING.yml +1 -1
- package/README.md +9 -9
- package/index.js +42 -40
- package/package.json +8 -4
package/.github/FUNDING.yml
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
tidelift:
|
|
1
|
+
tidelift: 'npm/balanced-match'
|
|
2
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
|
package/index.js
CHANGED
|
@@ -1,62 +1,64 @@
|
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
return
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
)
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
function maybeMatch(reg, str) {
|
|
19
|
-
|
|
20
|
-
return m ? m[0] : null
|
|
20
|
+
function maybeMatch (reg, str) {
|
|
21
|
+
const m = str.match(reg)
|
|
22
|
+
return m ? m[0] : null
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
balanced.range = range
|
|
24
|
-
function range(a, b, str) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
|
29
31
|
|
|
30
32
|
if (ai >= 0 && bi > 0) {
|
|
31
|
-
if(a===b) {
|
|
32
|
-
return [ai, bi]
|
|
33
|
+
if (a === b) {
|
|
34
|
+
return [ai, bi]
|
|
33
35
|
}
|
|
34
|
-
begs = []
|
|
35
|
-
left = str.length
|
|
36
|
+
begs = []
|
|
37
|
+
left = str.length
|
|
36
38
|
|
|
37
39
|
while (i >= 0 && !result) {
|
|
38
|
-
if (i
|
|
39
|
-
begs.push(i)
|
|
40
|
-
ai = str.indexOf(a, i + 1)
|
|
41
|
-
} else if (begs.length
|
|
42
|
-
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]
|
|
43
45
|
} else {
|
|
44
|
-
beg = begs.pop()
|
|
46
|
+
beg = begs.pop()
|
|
45
47
|
if (beg < left) {
|
|
46
|
-
left = beg
|
|
47
|
-
right = bi
|
|
48
|
+
left = beg
|
|
49
|
+
right = bi
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
bi = str.indexOf(b, i + 1)
|
|
52
|
+
bi = str.indexOf(b, i + 1)
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
i = ai < bi && ai >= 0 ? ai : bi
|
|
55
|
+
i = ai < bi && ai >= 0 ? ai : bi
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
if (begs.length) {
|
|
57
|
-
result = [
|
|
59
|
+
result = [left, right]
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
62
|
|
|
61
|
-
return result
|
|
63
|
+
return result
|
|
62
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": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git://github.com/juliangruber/balanced-match.git"
|
|
@@ -9,11 +9,15 @@
|
|
|
9
9
|
"homepage": "https://github.com/juliangruber/balanced-match",
|
|
10
10
|
"main": "index.js",
|
|
11
11
|
"scripts": {
|
|
12
|
-
"test": "tape test/test.js",
|
|
13
|
-
"bench": "matcha test/bench.js"
|
|
12
|
+
"test": "prettier-standard && standard && tape test/test.js",
|
|
13
|
+
"bench": "matcha test/bench.js",
|
|
14
|
+
"release": "np"
|
|
14
15
|
},
|
|
15
16
|
"devDependencies": {
|
|
16
|
-
"matcha": "^
|
|
17
|
+
"@c4312/matcha": "^1.3.1",
|
|
18
|
+
"np": "^7.4.0",
|
|
19
|
+
"prettier-standard": "^16.4.1",
|
|
20
|
+
"standard": "^16.0.3",
|
|
17
21
|
"tape": "^4.6.0"
|
|
18
22
|
},
|
|
19
23
|
"keywords": [
|