[Pkg-javascript-commits] [node-balanced-match] 01/03: New upstream version 0.4.2
Praveen Arimbrathodiyil
praveen at moszumanska.debian.org
Tue Oct 18 08:19:11 UTC 2016
This is an automated email from the git hooks/post-receive script.
praveen pushed a commit to branch master
in repository node-balanced-match.
commit 1df562d1e801c7758ab7d451e39bb0fb6a072a10
Author: Praveen Arimbrathodiyil <praveen at debian.org>
Date: Tue Oct 18 13:39:08 2016 +0530
New upstream version 0.4.2
---
.npmignore | 5 ++++
.travis.yml | 4 ++-
LICENSE.md | 21 +++++++++++++++
README.md | 15 +++++++++--
index.js | 80 +++++++++++++++++++++++++++++++++++---------------------
package.json | 4 +--
test/balanced.js | 43 ++++++++++++++++++++++++++++++
7 files changed, 137 insertions(+), 35 deletions(-)
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..ae5d8c3
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,5 @@
+test
+.gitignore
+.travis.yml
+Makefile
+example.js
diff --git a/.travis.yml b/.travis.yml
index cc4dba2..fd16c6b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,4 +1,6 @@
language: node_js
node_js:
- - "0.8"
- "0.10"
+ - "4"
+ - "5"
+ - "6"
diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644
index 0000000..2cdc8e4
--- /dev/null
+++ b/LICENSE.md
@@ -0,0 +1,21 @@
+(MIT)
+
+Copyright (c) 2013 Julian Gruber <julian at juliangruber.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
index 2aff0eb..08e918c 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# balanced-match
-Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`.
+Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regular expressions as well!
[![build status](https://secure.travis-ci.org/juliangruber/balanced-match.svg)](http://travis-ci.org/juliangruber/balanced-match)
[![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');
console.log(balanced('{', '}', 'pre{in{nested}}post'));
console.log(balanced('{', '}', 'pre{first}between{second}post'));
+console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'));
```
The matches are:
@@ -28,6 +29,7 @@ $ node example.js
pre: 'pre',
body: 'first',
post: 'between{second}post' }
+{ start: 3, end: 17, pre: 'pre', body: 'in{nest}', post: 'post' }
```
## API
@@ -45,7 +47,16 @@ object with those keys:
If there's no match, `undefined` will be returned.
-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', '']`.
+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', '}']`.
+
+### var r = balanced.range(a, b, str)
+
+For the first non-nested matching pair of `a` and `b` in `str`, return an
+array with indexes: `[ <a index>, <b index> ]`.
+
+If there's no match, `undefined` will be returned.
+
+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]`.
## Installation
diff --git a/index.js b/index.js
index d165ae8..e8d8587 100644
--- a/index.js
+++ b/index.js
@@ -1,38 +1,58 @@
module.exports = balanced;
function balanced(a, b, str) {
- var bal = 0;
- var m = {};
- var ended = false;
-
- for (var i = 0; i < str.length; i++) {
- if (a == str.substr(i, a.length)) {
- if (!('start' in m)) m.start = i;
- bal++;
- }
- else if (b == str.substr(i, b.length) && 'start' in m) {
- ended = true;
- bal--;
- if (!bal) {
- m.end = i;
- m.pre = str.substr(0, m.start);
- m.body = (m.end - m.start > 1)
- ? str.substring(m.start + a.length, m.end)
- : '';
- m.post = str.slice(m.end + b.length);
- return m;
+ if (a instanceof RegExp) a = maybeMatch(a, str);
+ if (b instanceof RegExp) b = maybeMatch(b, str);
+
+ var r = range(a, b, str);
+
+ return r && {
+ start: r[0],
+ end: r[1],
+ pre: str.slice(0, r[0]),
+ body: str.slice(r[0] + a.length, r[1]),
+ post: str.slice(r[1] + b.length)
+ };
+}
+
+function maybeMatch(reg, str) {
+ var m = str.match(reg);
+ return m ? m[0] : null;
+}
+
+balanced.range = range;
+function range(a, b, str) {
+ var begs, beg, left, right, result;
+ var ai = str.indexOf(a);
+ var bi = str.indexOf(b, ai + 1);
+ var i = ai;
+
+ if (ai >= 0 && bi > 0) {
+ begs = [];
+ left = str.length;
+
+ while (i >= 0 && !result) {
+ if (i == ai) {
+ begs.push(i);
+ ai = str.indexOf(a, i + 1);
+ } else if (begs.length == 1) {
+ result = [ begs.pop(), bi ];
+ } else {
+ beg = begs.pop();
+ if (beg < left) {
+ left = beg;
+ right = bi;
+ }
+
+ bi = str.indexOf(b, i + 1);
}
+
+ i = ai < bi && ai >= 0 ? ai : bi;
}
- }
- // if we opened more than we closed, find the one we closed
- if (bal && ended) {
- var start = m.start + a.length;
- m = balanced(a, b, str.substr(start));
- if (m) {
- m.start += start;
- m.end += start;
- m.pre = str.slice(0, start) + m.pre;
+ if (begs.length) {
+ result = [ left, right ];
}
- return m;
}
+
+ return result;
}
diff --git a/package.json b/package.json
index 0181423..720eb4d 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "balanced-match",
"description": "Match balanced character pairs, like \"{\" and \"}\"",
- "version": "0.2.0",
+ "version": "0.4.2",
"repository": {
"type": "git",
"url": "git://github.com/juliangruber/balanced-match.git"
@@ -13,7 +13,7 @@
},
"dependencies": {},
"devDependencies": {
- "tape": "~1.1.1"
+ "tape": "^4.6.0"
},
"keywords": [
"match",
diff --git a/test/balanced.js b/test/balanced.js
index 36bfd39..03924ed 100644
--- a/test/balanced.js
+++ b/test/balanced.js
@@ -23,6 +23,13 @@ test('balanced', function(t) {
body: 'in',
post: 'post'
});
+ t.deepEqual(balanced('{', '}', 'pre{in}po}st'), {
+ start: 3,
+ end: 6,
+ pre: 'pre',
+ body: 'in',
+ post: 'po}st'
+ });
t.deepEqual(balanced('{', '}', 'pre}{in{nest}}post'), {
start: 4,
end: 13,
@@ -52,5 +59,41 @@ test('balanced', function(t) {
body: 'in<b>nest</b>',
post: 'post'
});
+ t.deepEqual(balanced('{{', '}}', 'pre{{{in}}}post'), {
+ start: 3,
+ end: 9,
+ pre: 'pre',
+ body: '{in}',
+ post: 'post'
+ });
+ t.deepEqual(balanced('{{{', '}}', 'pre{{{in}}}post'), {
+ start: 3,
+ end: 8,
+ pre: 'pre',
+ body: 'in',
+ post: '}post'
+ });
+ t.deepEqual(balanced('{', '}', 'pre{{first}in{second}post'), {
+ start: 4,
+ end: 10,
+ pre: 'pre{',
+ body: 'first',
+ post: 'in{second}post'
+ });
+ t.deepEqual(balanced('<?', '?>', 'pre<?>post'), {
+ start: 3,
+ end: 4,
+ pre: 'pre',
+ body: '',
+ post: 'post'
+ });
+ t.notOk(balanced(/\{/, /\}/, 'nope'), 'should be notOk');
+ t.deepEqual(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre { in{nest} } post'), {
+ start: 3,
+ end: 17,
+ pre: 'pre',
+ body: 'in{nest}',
+ post: 'post'
+ });
t.end();
});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-balanced-match.git
More information about the Pkg-javascript-commits
mailing list