balanced-match
Advanced tools
Comparing version 1.0.1 to 1.0.2
78
index.js
@@ -1,64 +0,62 @@ | ||
'use strict' | ||
module.exports = balanced | ||
function balanced (a, b, str) { | ||
if (a instanceof RegExp) a = maybeMatch(a, str) | ||
if (b instanceof RegExp) b = maybeMatch(b, str) | ||
'use strict'; | ||
module.exports = balanced; | ||
function balanced(a, b, str) { | ||
if (a instanceof RegExp) a = maybeMatch(a, str); | ||
if (b instanceof RegExp) b = maybeMatch(b, str); | ||
const r = range(a, 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) | ||
} | ||
) | ||
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) { | ||
const m = str.match(reg) | ||
return m ? m[0] : null | ||
function maybeMatch(reg, str) { | ||
var m = str.match(reg); | ||
return m ? m[0] : null; | ||
} | ||
balanced.range = range | ||
function range (a, b, str) { | ||
let begs, beg, left, right, result | ||
let ai = str.indexOf(a) | ||
let bi = str.indexOf(b, ai + 1) | ||
let i = ai | ||
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) { | ||
if (a === b) { | ||
return [ai, bi] | ||
if(a===b) { | ||
return [ai, bi]; | ||
} | ||
begs = [] | ||
left = str.length | ||
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] | ||
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() | ||
beg = begs.pop(); | ||
if (beg < left) { | ||
left = beg | ||
right = bi | ||
left = beg; | ||
right = bi; | ||
} | ||
bi = str.indexOf(b, i + 1) | ||
bi = str.indexOf(b, i + 1); | ||
} | ||
i = ai < bi && ai >= 0 ? ai : bi | ||
i = ai < bi && ai >= 0 ? ai : bi; | ||
} | ||
if (begs.length) { | ||
result = [left, right] | ||
result = [ left, right ]; | ||
} | ||
} | ||
return result | ||
return result; | ||
} |
{ | ||
"name": "balanced-match", | ||
"description": "Match balanced character pairs, like \"{\" and \"}\"", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"repository": { | ||
@@ -12,11 +12,7 @@ "type": "git", | ||
"scripts": { | ||
"test": "prettier-standard && standard && tape test/test.js", | ||
"bench": "matcha test/bench.js", | ||
"release": "np" | ||
"test": "tape test/test.js", | ||
"bench": "matcha test/bench.js" | ||
}, | ||
"devDependencies": { | ||
"@c4312/matcha": "^1.3.1", | ||
"np": "^7.4.0", | ||
"prettier-standard": "^16.4.1", | ||
"standard": "^16.0.3", | ||
"matcha": "^0.7.0", | ||
"tape": "^4.6.0" | ||
@@ -23,0 +19,0 @@ }, |
@@ -15,7 +15,7 @@ # balanced-match | ||
```js | ||
var balanced = require('balanced-match') | ||
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')) | ||
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')); | ||
``` | ||
@@ -43,7 +43,7 @@ | ||
- **start** the index of the first match of `a` | ||
- **end** the index of the matching `b` | ||
- **pre** the preamble, `a` and `b` not included | ||
- **body** the match, `a` and `b` not included | ||
- **post** the postscript, `a` and `b` not included | ||
* **start** the index of the first match of `a` | ||
* **end** the index of the matching `b` | ||
* **pre** the preamble, `a` and `b` not included | ||
* **body** the match, `a` and `b` not included | ||
* **post** the postscript, `a` and `b` not included | ||
@@ -50,0 +50,0 @@ If there's no match, `undefined` will be returned. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
2
0
6939
52