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

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

  var p = adventurejs.Parser.prototype;

  /**
   * Convert some common compound prepositions into single words
   * to streamline preposition handling.
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#joinCompoundPrepositions
   * @param {String} input Player input.
   * @returns {String}
   */
  p.joinCompoundPrepositions = function Parser_joinCompoundPrepositions(
    parsed_input
  ) {
    this.game.log(
      "L1546",
      "log",
      "high",
      `[joinCompoundPrepositions.js] joinCompoundPrepositions() receive: ${parsed_input}`,
      "Parser"
    );
    let game = this.game;
    let compounds = this.game.dictionary.compound_prepositions;
    let record = function (match, replacement) {
      game.getInput().replacements[replacement.trim()] = {
        source: match.trim(),
        context: `[joinCompoundPrepositions.js] found ${match.trim()}`,
      };
      return replacement;
    };
    for (let i = 0; i < compounds.length; i++) {
      let regex = new RegExp(compounds[i][0], "g");
      parsed_input = parsed_input.replace(regex, (match) => {
        return record(match, compounds[i][1]);
      });
    }

    this.game.log(
      "L1257",
      "log",
      "high",
      `[joinCompoundPrepositions.js] joinCompoundPrepositions() return:\n${parsed_input}`,
      "Parser"
    );
    return parsed_input;
  };
})();