Skip to content

Commit

Permalink
add standard
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Apr 6, 2021
1 parent 6fcc4c6 commit 2cdedf9
Show file tree
Hide file tree
Showing 7 changed files with 5,061 additions and 117 deletions.
7 changes: 3 additions & 4 deletions example.js
@@ -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'))
64 changes: 32 additions & 32 deletions index.js
@@ -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
}

0 comments on commit 2cdedf9

Please sign in to comment.