balanced-match 0.3.0 → 1.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/.npmignore CHANGED
@@ -1,2 +1,5 @@
1
- node_modules
2
- .DS_Store
1
+ test
2
+ .gitignore
3
+ .travis.yml
4
+ Makefile
5
+ example.js
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
  [![build status](https://secure.travis-ci.org/juliangruber/balanced-match.svg)](http://travis-ci.org/juliangruber/balanced-match)
6
6
  [![downloads](https://img.shields.io/npm/dm/balanced-match.svg)](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,7 @@ 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', '}']`.
49
51
 
50
52
  ### var r = balanced.range(a, b, str)
51
53
 
@@ -54,7 +56,7 @@ array with indexes: `[ <a index>, <b index> ]`.
54
56
 
55
57
  If there's no match, `undefined` will be returned.
56
58
 
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 ]`.
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]`.
58
60
 
59
61
  ## Installation
60
62
 
package/index.js CHANGED
@@ -1,5 +1,9 @@
1
+ 'use strict';
1
2
  module.exports = balanced;
2
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
+
3
7
  var r = range(a, b, str);
4
8
 
5
9
  return r && {
@@ -11,6 +15,11 @@ function balanced(a, b, str) {
11
15
  };
12
16
  }
13
17
 
18
+ function maybeMatch(reg, str) {
19
+ var m = str.match(reg);
20
+ return m ? m[0] : null;
21
+ }
22
+
14
23
  balanced.range = range;
15
24
  function range(a, b, str) {
16
25
  var begs, beg, left, right, result;
@@ -22,7 +31,7 @@ function range(a, b, str) {
22
31
  begs = [];
23
32
  left = str.length;
24
33
 
25
- while (i < str.length && i >= 0 && ! result) {
34
+ while (i >= 0 && !result) {
26
35
  if (i == ai) {
27
36
  begs.push(i);
28
37
  ai = str.indexOf(a, i + 1);
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.3.0",
4
+ "version": "1.0.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git://github.com/juliangruber/balanced-match.git"
@@ -9,11 +9,13 @@
9
9
  "homepage": "https://github.com/juliangruber/balanced-match",
10
10
  "main": "index.js",
11
11
  "scripts": {
12
- "test": "make test"
12
+ "test": "make test",
13
+ "bench": "make bench"
13
14
  },
14
15
  "dependencies": {},
15
16
  "devDependencies": {
16
- "tape": "~4.2.2"
17
+ "matcha": "^0.7.0",
18
+ "tape": "^4.6.0"
17
19
  },
18
20
  "keywords": [
19
21
  "match",
package/.travis.yml DELETED
@@ -1,3 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - "0.10"
package/Makefile DELETED
@@ -1,6 +0,0 @@
1
-
2
- test:
3
- @node_modules/.bin/tape test/*.js
4
-
5
- .PHONY: test
6
-
package/example.js DELETED
@@ -1,5 +0,0 @@
1
- var balanced = require('./');
2
-
3
- console.log(balanced('{', '}', 'pre{in{nested}}post'));
4
- console.log(balanced('{', '}', 'pre{first}between{second}post'));
5
-
package/test/balanced.js DELETED
@@ -1,84 +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.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
- });
83
- t.end();
84
- });