balanced | | Cell 1 | Search

The balanced function identifies and extracts a balanced substring within a given string, delimited by two specified markers, a and b. It handles potential nested delimiters and returns information about the balanced substring's position and content.

Run example

npm run import -- "balanced"

balanced

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

What the code could have been:

// Export the balanced function
module.exports = balanced;

/**
 * Finds the balanced part of a string based on two regular expressions or strings.
 *
 * @param {RegExp|string} a - The left part regular expression or string.
 * @param {RegExp|string} b - The right part regular expression or string.
 * @param {string} str - The input string.
 * @returns {object|null} An object containing start, end, pre, body, and post properties, or null if not found.
 */
function balanced(a, b, str) {
  if (a instanceof RegExp) a = maybeMatch(a, str);
  if (b instanceof RegExp) b = maybeMatch(b, str);

  const ranges = range(a, b, str);
  return ranges && {
    /**
     * The starting index of the matched text.
     * @type {number}
     */
    start: ranges[0],
    /**
     * The ending index of the matched text.
     * @type {number}
     */
    end: ranges[1],
    /**
     * The text before the matched text.
     * @type {string}
     */
    pre: str.slice(0, ranges[0]),
    /**
     * The matched text.
     * @type {string}
     */
    body: str.slice(ranges[0] + a.length, ranges[1]),
    /**
     * The text after the matched text.
     * @type {string}
     */
    post: str.slice(ranges[1] + b.length)
  };
}

/**
 * Tries to match a regular expression in a string.
 *
 * @param {RegExp} reg - The regular expression.
 * @param {string} str - The input string.
 * @returns {string|null} The matched text, or null if not found.
 */
function maybeMatch(reg, str) {
  return str.match(reg) && str.match(reg)[0] || null;
}

// Expose the range function
balanced.range = range;

/**
 * Finds all occurrences of two regular expressions or strings in a string.
 *
 * @param {RegExp|string} a - The left part regular expression or string.
 * @param {RegExp|string} b - The right part regular expression or string.
 * @param {string} str - The input string.
 * @returns {number[][]|null} An array of arrays containing the start and end indices of the matched text, or null if not found.
 */
function range(a, b, str) {
  if (typeof a!=='string') a = a.source;
  if (typeof b!=='string') b = b.source;

  const ai = str.indexOf(a);
  const bi = str.indexOf(b, ai + 1);
  let i = ai;

  if (ai >= 0 && bi > 0) {
    const begs = [];
    let left = str.length;

    while (i >= 0 &&!ranges) {
      if (i === ai) {
        begs.push(i);
        ai = str.indexOf(a, i + 1);
      } else if (begs.length === 1) {
        ranges = [ begs.pop(), bi ];
      } else {
        const beg = begs.pop();
        if (beg < left) {
          left = beg;
          const right = bi;
        }

        bi = str.indexOf(b, i + 1);
      }

      i = ai < bi && ai >= 0? ai : bi;
    }

    if (begs.length) {
      ranges = [ left, bi ];
    }
  }

  return ranges;
}

This code defines a function balanced that finds a balanced substring within a given string str based on two delimiters, a and b.

Here's a breakdown:

Let me know if you'd like a more detailed explanation of any specific part!