balanced-match 0.0.0 → 0.2.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.
- package/.npmignore +1 -0
- package/.travis.yml +3 -0
- package/LICENSE.md +21 -0
- package/Makefile +6 -0
- package/README.md +14 -8
- package/index.js +20 -6
- package/package.json +19 -3
- package/test/balanced.js +36 -1
package/.npmignore
CHANGED
package/.travis.yml
ADDED
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
(MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
9
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
10
|
+
so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Makefile
ADDED
package/README.md
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
|
|
2
1
|
# balanced-match
|
|
3
2
|
|
|
4
|
-
Match balanced
|
|
3
|
+
Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`.
|
|
4
|
+
|
|
5
|
+
[](http://travis-ci.org/juliangruber/balanced-match)
|
|
6
|
+
[](https://www.npmjs.org/package/balanced-match)
|
|
7
|
+
|
|
8
|
+
[](https://ci.testling.com/juliangruber/balanced-match)
|
|
5
9
|
|
|
6
10
|
## Example
|
|
7
11
|
|
|
8
|
-
Get the first
|
|
12
|
+
Get the first matching pair of braces:
|
|
9
13
|
|
|
10
14
|
```js
|
|
11
15
|
var balanced = require('balanced-match');
|
|
@@ -33,14 +37,16 @@ $ node example.js
|
|
|
33
37
|
For the first non-nested matching pair of `a` and `b` in `str`, return an
|
|
34
38
|
object with those keys:
|
|
35
39
|
|
|
36
|
-
* **start
|
|
37
|
-
* **end
|
|
38
|
-
* **pre
|
|
39
|
-
* **body
|
|
40
|
-
* **post
|
|
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
|
|
41
45
|
|
|
42
46
|
If there's no match, `undefined` will be returned.
|
|
43
47
|
|
|
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', '']`.
|
|
49
|
+
|
|
44
50
|
## Installation
|
|
45
51
|
|
|
46
52
|
With [npm](https://npmjs.org) do:
|
package/index.js
CHANGED
|
@@ -1,24 +1,38 @@
|
|
|
1
|
-
module.exports =
|
|
1
|
+
module.exports = balanced;
|
|
2
|
+
function balanced(a, b, str) {
|
|
2
3
|
var bal = 0;
|
|
3
4
|
var m = {};
|
|
5
|
+
var ended = false;
|
|
4
6
|
|
|
5
7
|
for (var i = 0; i < str.length; i++) {
|
|
6
|
-
if (str
|
|
8
|
+
if (a == str.substr(i, a.length)) {
|
|
7
9
|
if (!('start' in m)) m.start = i;
|
|
8
10
|
bal++;
|
|
9
11
|
}
|
|
10
|
-
else if (str
|
|
12
|
+
else if (b == str.substr(i, b.length) && 'start' in m) {
|
|
13
|
+
ended = true;
|
|
11
14
|
bal--;
|
|
12
15
|
if (!bal) {
|
|
13
16
|
m.end = i;
|
|
14
17
|
m.pre = str.substr(0, m.start);
|
|
15
18
|
m.body = (m.end - m.start > 1)
|
|
16
|
-
? str.substring(m.start +
|
|
19
|
+
? str.substring(m.start + a.length, m.end)
|
|
17
20
|
: '';
|
|
18
|
-
m.post = str.slice(m.end +
|
|
21
|
+
m.post = str.slice(m.end + b.length);
|
|
19
22
|
return m;
|
|
20
23
|
}
|
|
21
24
|
}
|
|
22
25
|
}
|
|
23
|
-
};
|
|
24
26
|
|
|
27
|
+
// if we opened more than we closed, find the one we closed
|
|
28
|
+
if (bal && ended) {
|
|
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;
|
|
35
|
+
}
|
|
36
|
+
return m;
|
|
37
|
+
}
|
|
38
|
+
}
|
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
|
+
"version": "0.2.1",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git://github.com/juliangruber/balanced-match.git"
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"homepage": "https://github.com/juliangruber/balanced-match",
|
|
10
10
|
"main": "index.js",
|
|
11
11
|
"scripts": {
|
|
12
|
-
"test": "
|
|
12
|
+
"test": "make test"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {},
|
|
15
15
|
"devDependencies": {
|
|
@@ -27,5 +27,21 @@
|
|
|
27
27
|
"email": "mail@juliangruber.com",
|
|
28
28
|
"url": "http://juliangruber.com"
|
|
29
29
|
},
|
|
30
|
-
"license": "MIT"
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"testling": {
|
|
32
|
+
"files": "test/*.js",
|
|
33
|
+
"browsers": [
|
|
34
|
+
"ie/8..latest",
|
|
35
|
+
"firefox/20..latest",
|
|
36
|
+
"firefox/nightly",
|
|
37
|
+
"chrome/25..latest",
|
|
38
|
+
"chrome/canary",
|
|
39
|
+
"opera/12..latest",
|
|
40
|
+
"opera/next",
|
|
41
|
+
"safari/5.1..latest",
|
|
42
|
+
"ipad/6.0..latest",
|
|
43
|
+
"iphone/6.0..latest",
|
|
44
|
+
"android-browser/4.2..latest"
|
|
45
|
+
]
|
|
46
|
+
}
|
|
31
47
|
}
|
package/test/balanced.js
CHANGED
|
@@ -9,6 +9,27 @@ test('balanced', function(t) {
|
|
|
9
9
|
body: 'in{nest}',
|
|
10
10
|
post: 'post'
|
|
11
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
|
+
});
|
|
12
33
|
t.deepEqual(balanced('{', '}', 'pre{body}between{body2}post'), {
|
|
13
34
|
start: 3,
|
|
14
35
|
end: 8,
|
|
@@ -16,6 +37,20 @@ test('balanced', function(t) {
|
|
|
16
37
|
body: 'body',
|
|
17
38
|
post: 'between{body2}post'
|
|
18
39
|
});
|
|
19
|
-
t.notOk(balanced('{', '}', 'nope'));
|
|
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
|
+
});
|
|
20
55
|
t.end();
|
|
21
56
|
});
|