-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fcc4c6
commit 2cdedf9
Showing
7 changed files
with
5,061 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
var balanced = require('./'); | ||
|
||
console.log(balanced('{', '}', 'pre{in{nested}}post')); | ||
console.log(balanced('{', '}', 'pre{first}between{second}post')); | ||
const balanced = require('./') | ||
|
||
console.log(balanced('{', '}', 'pre{in{nested}}post')) | ||
console.log(balanced('{', '}', 'pre{first}between{second}post')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,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) | ||
|
||
var r = range(a, b, str); | ||
const 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; | ||
function maybeMatch (reg, str) { | ||
const 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; | ||
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 | ||
|
||
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 | ||
} |
Oops, something went wrong.