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

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

  var p = adventurejs.Parser.prototype;

  /**
   * <p>
   * Search input for compound object names,
   * by comparing the input string against entries in
   * this.game.world lookup table,
   * which was populated during the creation of Game
   * objects. If we find multiple words that match an entry,
   * we compress the words into a single string
   * that is a noun id.
   * </p>
   * <p>
   * For example:<br>
   * <code class="property">"turn on brass lantern"</code>
   * becomes
   * <code class="property">"turn on brass_lantern"</code>.
   * </p>
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#joinCompoundNames
   * @param {String} input Player input.
   * @returns {String}
   */
  p.joinCompoundNames = function Parser_joinCompoundNames(input) {
    this.game.log(
      "L1544",
      "log",
      "high",
      `[joinCompoundNames.js] joinCompoundNames() receive: ${input}`,
      "Parser"
    );

    for (var prop in this.game.world) {
      var name = this.game.world[prop].name;
      if (!name) continue;
      var id = this.game.world[prop].id;
      var search = "\\b" + name + "\\b";
      var regex = new RegExp(search, "g");
      input = input.replace(regex, id);
    }

    this.game.log(
      "L1545",
      "log",
      "high",
      `[joinCompoundNames.js] joinCompoundNames() return: ${input}`,
      "Parser"
    );
    return input;
  };
})();