Sha256: c5888614a84b2aea3f029356228a557de31a1d5061485c806e0770361b32a65a

Contents?: true

Size: 989 Bytes

Versions: 22

Compression:

Stored size: 989 Bytes

Contents

module.exports = balanced;
function balanced(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)
  };
}

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 < str.length && 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 (begs.length) {
      result = [ left, right ];
    }
  }

  return result;
}

Version data entries

22 entries across 10 versions & 3 rubygems

Version Path
brwy_rails-0.0.6 test/dummy/node_modules/fsevents/node_modules/rimraf/node_modules/glob/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.6 test/dummy/node_modules/fsevents/node_modules/fstream-ignore/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.6 test/dummy/node_modules/balanced-match/index.js
brwy_rails-0.0.5 test/dummy/node_modules/balanced-match/index.js
brwy_rails-0.0.5 test/dummy/node_modules/fsevents/node_modules/rimraf/node_modules/glob/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.5 test/dummy/node_modules/fsevents/node_modules/fstream-ignore/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.4 test/dummy/node_modules/fsevents/node_modules/fstream-ignore/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.4 test/dummy/node_modules/fsevents/node_modules/rimraf/node_modules/glob/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.4 test/dummy/node_modules/balanced-match/index.js
brwy_rails-0.0.3 test/dummy/node_modules/fsevents/node_modules/fstream-ignore/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.3 test/dummy/node_modules/balanced-match/index.js
brwy_rails-0.0.3 test/dummy/node_modules/fsevents/node_modules/rimraf/node_modules/glob/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.2 test/dummy/node_modules/fsevents/node_modules/rimraf/node_modules/glob/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.2 test/dummy/node_modules/fsevents/node_modules/fstream-ignore/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.2 test/dummy/node_modules/balanced-match/index.js
brwy_rails-0.0.1 test/dummy/node_modules/balanced-match/index.js
brwy_rails-0.0.1 test/dummy/node_modules/fsevents/node_modules/rimraf/node_modules/glob/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
brwy_rails-0.0.1 test/dummy/node_modules/fsevents/node_modules/fstream-ignore/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
babel-schmooze-sprockets-0.1.0.alpha.3 node_modules/balanced-match/index.js
babel-schmooze-sprockets-0.1.0.alpha.2 node_modules/balanced-match/index.js