Sha256: 5c3415fe87961cffc503e9a1d74fe2cd4c0c2ec57b7ea4fef0a4b663f53e52b4

Contents?: true

Size: 1.15 KB

Versions: 207

Compression:

Stored size: 1.15 KB

Contents

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

  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

207 entries across 183 versions & 24 rubygems

Version Path
boring_generators-0.15.0 tmp/templates/app_template/node_modules/balanced-match/index.js
opal-1.8.3.rc1 stdlib/nodejs/node_modules/balanced-match/index.js
boring_generators-0.14.0 tmp/templates/app_template/node_modules/balanced-match/index.js
optimacms-0.1.61 spec/dummy/node_modules/balanced-match/index.js
boring_generators-0.13.0 tmp/templates/app_template/node_modules/balanced-match/index.js
opal-1.8.2 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.8.1 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.8.0 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.8.0.beta1 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.7.4 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.8.0.alpha1 stdlib/nodejs/node_modules/balanced-match/index.js
boring_generators-0.12.0 tmp/templates/app_template/node_modules/balanced-match/index.js
opal-1.7.3 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.7.2 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.7.1 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.7.0 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.7.0.rc1 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.6.1 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.6.0 stdlib/nodejs/node_modules/balanced-match/index.js
opal-1.6.0.rc1 stdlib/nodejs/node_modules/balanced-match/index.js