Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// findQuotedSubstrings.js

(function () {
  /* global adventurejs A */

  var p = adventurejs.Parser.prototype;

  /**
   * Find quoted substrings, as in 'type "foo" on keyboard'.
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#findQuotedSubstrings
   * @param {String} input
   * @returns {Array} [array, int, boolean]
   */
  p.findQuotedSubstrings = function Parser_findQuotedSubstrings(input) {
    this.game.log(
      "L1603",
      "log",
      "low",
      `[findQuotedSubstrings.js] findQuotedSubstrings() receive: ${input}`,
      "Parser"
    );

    let quoted = false;
    let quoted_ranges = [];
    let blocks = -1;
    let success = true;

    for (let i = 0; i < input.length; i++) {
      const char = input[i];
      if (char === '"') {
        quoted = !quoted; // Toggle the flag when encountering quotes
        if (quoted) {
          blocks++;
          quoted_ranges.push([]);
        }
        quoted_ranges[blocks].push(i);
      }
    }
    for (let block in quoted_ranges) {
      let pair = quoted_ranges[block];
      if (pair.length !== 2) {
        let msg = `There appears to be an unclosed double quote in that input. Please try again. `;
        this.game.log(
          "L1595",
          "warn",
          "high",
          `[findQuotedSubstrings.js] found uneven quotes in ${input}`,
          "Parser"
        );
        this.game.display.printInput(input);
        this.game.debug(
          `D1632`,
          `findQuotedSubstrings.js `,
          `found uneven quotes in ${input}`
        );
        this.game.print(msg);
        success = false;
      }
    }

    this.game.log(
      "L1604",
      "log",
      "low",
      `[findQuotedSubstrings.js] findQuotedSubstrings() return: ${String(quoted_ranges)}`,
      "Parser"
    );
    return [quoted_ranges, quoted_ranges.length > 0, success];
  };
})();