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

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

  var p = adventurejs.Parser.prototype;

  /**
   * Normalize input addresses several common typos.
   * <ul>
   * <li>convert tabs to spaces</li>
   * <li>trim()</li>
   * <li>reduce multiple spaces</li>
   * <li>replace " ," with ","</li>
   * <li>replace " ." with "."</li>
   * </ul>
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#normalizeInput
   * @param {String} input
   * @returns {String}
   */
  p.normalizeInput = function Parser_normalizeInput(input) {
    this.game.log(
      "L1560",
      "log",
      "high",
      `[normalizeInput.js] normalizeInput() receive: ${input}`,
      "Parser"
    );

    // --------------------------------------------------
    // convert tabs to spaces
    // --------------------------------------------------
    input = input.replace(/\t/g, " ");

    // --------------------------------------------------
    // trim white spaces
    // --------------------------------------------------
    input = input.trim();

    // --------------------------------------------------
    // reduce multiple spaces to single
    // --------------------------------------------------
    input = input.replace(/\s+/g, " ");

    // --------------------------------------------------
    // reduce multiple periods to single
    // --------------------------------------------------
    input = input.replace(/\.+/g, ".");

    // --------------------------------------------------
    // replace " ," with ","
    // --------------------------------------------------
    input = input.replace(/\s+,/g, ",");

    // --------------------------------------------------
    // replace " ." with "."
    // --------------------------------------------------
    input = input.replace(/\s+\./g, ",");

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