balanced-match 0.2.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/README.md CHANGED
@@ -47,6 +47,15 @@ If there's no match, `undefined` will be returned.
47
47
 
48
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
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
+
50
59
  ## Installation
51
60
 
52
61
  With [npm](https://npmjs.org) do:
package/index.js CHANGED
@@ -1,38 +1,50 @@
1
1
  module.exports = balanced;
2
2
  function balanced(a, b, str) {
3
- var bal = 0;
4
- var m = {};
5
- var ended = false;
6
-
7
- for (var i = 0; i < str.length; i++) {
8
- if (a == str.substr(i, a.length)) {
9
- if (!('start' in m)) m.start = i;
10
- bal++;
11
- }
12
- else if (b == str.substr(i, b.length) && 'start' in m) {
13
- ended = true;
14
- bal--;
15
- if (!bal) {
16
- m.end = i;
17
- m.pre = str.substr(0, m.start);
18
- m.body = (m.end - m.start > 1)
19
- ? str.substring(m.start + a.length, m.end)
20
- : '';
21
- m.post = str.slice(m.end + b.length);
22
- return m;
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);
23
39
  }
40
+
41
+ i = ai < bi && ai >= 0 ? ai : bi;
24
42
  }
25
- }
26
43
 
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;
44
+ if (begs.length) {
45
+ result = [ left, right ];
35
46
  }
36
- return m;
37
47
  }
48
+
49
+ return result;
38
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.2.1",
4
+ "version": "0.3.0",
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": "~1.1.1"
16
+ "tape": "~4.2.2"
17
17
  },
18
18
  "keywords": [
19
19
  "match",
package/test/balanced.js CHANGED
@@ -52,5 +52,33 @@ test('balanced', function(t) {
52
52
  body: 'in<b>nest</b>',
53
53
  post: 'post'
54
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
+ });
55
83
  t.end();
56
84
  });