balanced-match 0.2.1 → 0.4.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/.npmignore +5 -2
- package/README.md +13 -2
- package/index.js +50 -30
- package/package.json +2 -2
- package/.travis.yml +0 -3
- package/Makefile +0 -6
- package/example.js +0 -5
- package/test/balanced.js +0 -56
package/.npmignore
CHANGED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# balanced-match
|
|
2
2
|
|
|
3
|
-
Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`.
|
|
3
|
+
Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regular expressions as well!
|
|
4
4
|
|
|
5
5
|
[](http://travis-ci.org/juliangruber/balanced-match)
|
|
6
6
|
[](https://www.npmjs.org/package/balanced-match)
|
|
@@ -16,6 +16,7 @@ var balanced = require('balanced-match');
|
|
|
16
16
|
|
|
17
17
|
console.log(balanced('{', '}', 'pre{in{nested}}post'));
|
|
18
18
|
console.log(balanced('{', '}', 'pre{first}between{second}post'));
|
|
19
|
+
console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'));
|
|
19
20
|
```
|
|
20
21
|
|
|
21
22
|
The matches are:
|
|
@@ -28,6 +29,7 @@ $ node example.js
|
|
|
28
29
|
pre: 'pre',
|
|
29
30
|
body: 'first',
|
|
30
31
|
post: 'between{second}post' }
|
|
32
|
+
{ start: 3, end: 17, pre: 'pre', body: 'in{nest}', post: 'post' }
|
|
31
33
|
```
|
|
32
34
|
|
|
33
35
|
## API
|
|
@@ -45,7 +47,16 @@ object with those keys:
|
|
|
45
47
|
|
|
46
48
|
If there's no match, `undefined` will be returned.
|
|
47
49
|
|
|
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', '']`.
|
|
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
|
+
|
|
52
|
+
### var r = balanced.range(a, b, str)
|
|
53
|
+
|
|
54
|
+
For the first non-nested matching pair of `a` and `b` in `str`, return an
|
|
55
|
+
array with indexes: `[ <a index>, <b index> ]`.
|
|
56
|
+
|
|
57
|
+
If there's no match, `undefined` will be returned.
|
|
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 ]` and `{a}}` will match `[0, 2]`.
|
|
49
60
|
|
|
50
61
|
## Installation
|
|
51
62
|
|
package/index.js
CHANGED
|
@@ -1,38 +1,58 @@
|
|
|
1
1
|
module.exports = balanced;
|
|
2
2
|
function balanced(a, b, str) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function maybeMatch(reg, str) {
|
|
18
|
+
var m = str.match(reg);
|
|
19
|
+
return m ? m[0] : null;
|
|
20
|
+
}
|
|
21
|
+
|
|
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;
|
|
28
|
+
|
|
29
|
+
if (ai >= 0 && bi > 0) {
|
|
30
|
+
begs = [];
|
|
31
|
+
left = str.length;
|
|
32
|
+
|
|
33
|
+
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 ];
|
|
39
|
+
} else {
|
|
40
|
+
beg = begs.pop();
|
|
41
|
+
if (beg < left) {
|
|
42
|
+
left = beg;
|
|
43
|
+
right = bi;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
bi = str.indexOf(b, i + 1);
|
|
23
47
|
}
|
|
48
|
+
|
|
49
|
+
i = ai < bi && ai >= 0 ? ai : bi;
|
|
24
50
|
}
|
|
25
|
-
}
|
|
26
51
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
var start = m.start + a.length;
|
|
30
|
-
m = balanced(a, b, str.substr(start));
|
|
31
|
-
if (m) {
|
|
32
|
-
m.start += start;
|
|
33
|
-
m.end += start;
|
|
34
|
-
m.pre = str.slice(0, start) + m.pre;
|
|
52
|
+
if (begs.length) {
|
|
53
|
+
result = [ left, right ];
|
|
35
54
|
}
|
|
36
|
-
return m;
|
|
37
55
|
}
|
|
56
|
+
|
|
57
|
+
return result;
|
|
38
58
|
}
|
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.2
|
|
4
|
+
"version": "0.4.2",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git://github.com/juliangruber/balanced-match.git"
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {},
|
|
15
15
|
"devDependencies": {
|
|
16
|
-
"tape": "
|
|
16
|
+
"tape": "^4.6.0"
|
|
17
17
|
},
|
|
18
18
|
"keywords": [
|
|
19
19
|
"match",
|
package/.travis.yml
DELETED
package/Makefile
DELETED
package/example.js
DELETED
package/test/balanced.js
DELETED
|
@@ -1,56 +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.end();
|
|
56
|
-
});
|