balanced-match 0.0.1 → 0.3.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/.npmignore CHANGED
@@ -1 +1,2 @@
1
1
  node_modules
2
+ .DS_Store
package/.travis.yml CHANGED
@@ -1,4 +1,3 @@
1
1
  language: node_js
2
2
  node_js:
3
- - "0.8"
4
3
  - "0.10"
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
@@ -0,0 +1,6 @@
1
+
2
+ test:
3
+ @node_modules/.bin/tape test/*.js
4
+
5
+ .PHONY: test
6
+
package/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  # balanced-match
2
2
 
3
- Match balanced character pairs, like `{` and `}`.
3
+ Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`.
4
4
 
5
- [![build status](https://secure.travis-ci.org/juliangruber/balanced-match.png)](http://travis-ci.org/juliangruber/balanced-match)
5
+ [![build status](https://secure.travis-ci.org/juliangruber/balanced-match.svg)](http://travis-ci.org/juliangruber/balanced-match)
6
+ [![downloads](https://img.shields.io/npm/dm/balanced-match.svg)](https://www.npmjs.org/package/balanced-match)
6
7
 
7
8
  [![testling badge](https://ci.testling.com/juliangruber/balanced-match.png)](https://ci.testling.com/juliangruber/balanced-match)
8
9
 
9
10
  ## Example
10
11
 
11
- Get the first non-nested matching pair of braces:
12
+ Get the first matching pair of braces:
12
13
 
13
14
  ```js
14
15
  var balanced = require('balanced-match');
@@ -44,6 +45,17 @@ object with those keys:
44
45
 
45
46
  If there's no match, `undefined` will be returned.
46
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
+
50
+ ### var r = balanced.range(a, b, str)
51
+
52
+ For the first non-nested matching pair of `a` and `b` in `str`, return an
53
+ array with indexes: `[ <a index>, <b index> ]`.
54
+
55
+ If there's no match, `undefined` will be returned.
56
+
57
+ 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 ]`.
58
+
47
59
  ## Installation
48
60
 
49
61
  With [npm](https://npmjs.org) do:
package/index.js CHANGED
@@ -1,24 +1,50 @@
1
- module.exports = function(a, b, str) {
2
- var bal = 0;
3
- var m = {};
4
-
5
- for (var i = 0; i < str.length; i++) {
6
- if (str[i] == a) {
7
- if (!('start' in m)) m.start = i;
8
- bal++;
9
- }
10
- else if (str[i] == b) {
11
- bal--;
12
- if (!bal) {
13
- m.end = i;
14
- m.pre = str.substr(0, m.start);
15
- m.body = (m.end - m.start > 1)
16
- ? str.substring(m.start + 1, m.end)
17
- : '';
18
- m.post = str.slice(m.end + 1);
19
- return m;
1
+ module.exports = balanced;
2
+ function balanced(a, b, str) {
3
+ var r = range(a, b, str);
4
+
5
+ return r && {
6
+ start: r[0],
7
+ end: r[1],
8
+ pre: str.slice(0, r[0]),
9
+ body: str.slice(r[0] + a.length, r[1]),
10
+ post: str.slice(r[1] + b.length)
11
+ };
12
+ }
13
+
14
+ balanced.range = range;
15
+ function range(a, b, str) {
16
+ var begs, beg, left, right, result;
17
+ var ai = str.indexOf(a);
18
+ var bi = str.indexOf(b, ai + 1);
19
+ var i = ai;
20
+
21
+ if (ai >= 0 && bi > 0) {
22
+ begs = [];
23
+ left = str.length;
24
+
25
+ while (i < str.length && i >= 0 && ! result) {
26
+ if (i == ai) {
27
+ begs.push(i);
28
+ ai = str.indexOf(a, i + 1);
29
+ } else if (begs.length == 1) {
30
+ result = [ begs.pop(), bi ];
31
+ } else {
32
+ beg = begs.pop();
33
+ if (beg < left) {
34
+ left = beg;
35
+ right = bi;
36
+ }
37
+
38
+ bi = str.indexOf(b, i + 1);
20
39
  }
40
+
41
+ i = ai < bi && ai >= 0 ? ai : bi;
42
+ }
43
+
44
+ if (begs.length) {
45
+ result = [ left, right ];
21
46
  }
22
47
  }
23
- };
24
48
 
49
+ return result;
50
+ }
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.0.1",
4
+ "version": "0.3.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git://github.com/juliangruber/balanced-match.git"
@@ -9,11 +9,11 @@
9
9
  "homepage": "https://github.com/juliangruber/balanced-match",
10
10
  "main": "index.js",
11
11
  "scripts": {
12
- "test": "tape test/*.js"
12
+ "test": "make test"
13
13
  },
14
14
  "dependencies": {},
15
15
  "devDependencies": {
16
- "tape": "~1.1.1"
16
+ "tape": "~4.2.2"
17
17
  },
18
18
  "keywords": [
19
19
  "match",
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,48 @@ 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
+ });
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
+ });
20
83
  t.end();
21
84
  });
package/component.json DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "name": "balanced-match",
3
- "description": "Match balanced character pairs, like \"{\" and \"}\"",
4
- "version": "0.0.1",
5
- "repository": "juliangruber/balanced-match",
6
- "keywords": [
7
- "match",
8
- "regexp",
9
- "test",
10
- "balanced",
11
- "parse"
12
- ],
13
- "scripts": ["index.js"],
14
- "license": "MIT"
15
- }