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

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

  var p = adventurejs.Parser.prototype;

  /**
   * List of methods to be called during parse that perform
   * regex operations on input in order to join phrasal verbs.
   * @var adventurejs.Parser#phrasal_patterns
   * @type {Array}
   */
  p.phrasal_patterns = [
    "joinPhrasalVerbPrepNounPrepNounPrepNouns",
    "joinPhrasalVerbNounPrepPrepNouns",
    "joinPhrasalVerbNounPrepNounPrepNouns",
    "joinPhrasalVerbPrepNounPrepNouns",
    "joinPhrasalVerbPrepPrepPrepNouns",
    "joinPhrasalVerbPrepPrepNouns",
    "joinPhrasalVerbNounPrepNouns",
    "joinPhrasalVerbNounPreps",
    "joinPhrasalVerbPrepNouns",
  ];

  /**
   * <code>joinPhrasalVerbs</code> identifies phrasal or compound verb
   * patterns such as "swing from tree to cliff on vine". This feature
   * is mostly retired but for use in a small few instances such as
   * aiding in identifying "plug in computer" vs "plug drain". An earlier
   * version of the parser used this extensively to join phrasal verbs
   * into distinct verbs such as "swing_from_to_on tree cliff vine",
   * which worked well but ultimately proved to be inflexible.
   * <br><br>
   * The listed order of regex operations is important. We're calling them
   * in order of longest to shortest, to ensure we don't accidentally
   * catch partial phrases, like only finding "swing_from" out of
   * "swing from tree to cliff on vine", and then not finding
   * "swing_from_to_on", which is a distinct pattern.
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#joinPhrasalVerbs
   * @param {String} input Player input.
   * @returns {String}
   */
  p.joinPhrasalVerbs = function Parser_joinPhrasalVerbs(input) {
    // join phrasal verbs
    if (1 < input.split(" ").length) {
      for (let i = 0; i < this.phrasal_patterns.length; i++) {
        let phrasal_input = this[this.phrasal_patterns[i]](input);

        // this version returns the original input
        // if (phrasal_input !== input) return input;

        // this version returns the modified input
        if (phrasal_input !== input) return phrasal_input;
      }
    }

    return input;
  };
})();