Sha256: 49ed20ff8afa5f962b72caebbf7ecff79c36d3957796b065219a5a53730b2c9d

Contents?: true

Size: 1.13 KB

Versions: 41

Compression:

Stored size: 1.13 KB

Contents

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);

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

  return result;
}

Version data entries

41 entries across 16 versions & 9 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/postcss-selector-not/node_modules/balanced-match/index.js
optimacms-0.1.61 spec/dummy/node_modules/reduce-css-calc/node_modules/balanced-match/index.js
optimacms-0.1.61 spec/dummy/node_modules/postcss-selector-matches/node_modules/balanced-match/index.js
optimacms-0.1.61 spec/dummy/node_modules/postcss-apply/node_modules/balanced-match/index.js
optimacms-0.4.3 spec/dummy/node_modules/balanced-match/index.js
optimacms-0.4.2 spec/dummy/node_modules/balanced-match/index.js
locomotivecms-4.0.0.alpha1 app/javascript/node_modules/balanced-match/index.js
locomotivecms-3.4.0 app/javascript/node_modules/balanced-match/index.js
cortex-0.1.3 spec/dummy/node_modules/balanced-match/index.js
affiliator-0.2.1 node_modules/fsevents/node_modules/balanced-match/index.js
guard-sass-lint-0.1.2 node_modules/balanced-match/index.js
guard-sass-lint-0.1.1 node_modules/balanced-match/index.js
lanes-0.8.0 node_modules/balanced-match/index.js
lanes-0.8.0 node_modules/fsevents/node_modules/balanced-match/index.js
node-compiler-0.9.1 vendor/node/deps/npm/node_modules/glob/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
node-compiler-0.9.1 vendor/node/deps/npm/node_modules/fstream-npm/node_modules/fstream-ignore/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
node-compiler-0.9.1 vendor/node/deps/npm/node_modules/init-package-json/node_modules/glob/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
node-compiler-0.9.1 vendor/node/deps/npm/node_modules/node-gyp/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
node-compiler-0.9.1 vendor/node/deps/npm/node_modules/read-package-json/node_modules/glob/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js
node-compiler-0.9.0 vendor/node-v7.2.1/deps/npm/node_modules/fstream-npm/node_modules/fstream-ignore/node_modules/minimatch/node_modules/brace-expansion/node_modules/balanced-match/index.js