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

(function () {
  /*global adventurejs A*/
  "use strict";

  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
  ) {
    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(
      "log",
      "high",
      "parseInput.js > joinCompoundPrepositions return: " + parsed_input,
      "Parser"
    );
    return parsed_input;
  };
})();