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

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

  var p = adventurejs.Dictionary.prototype;

  /**
   * Takes two verb names and checks to see if they're synonyms.
   * @memberOf adventurejs.Dictionary
   * @method adventurejs.Dictionary#testVerbSynonyms
   * @kind function
   * @param  {String} word1 An unclassed verb object.
   * @returns {Verb} The constructed verb
   */
  p.testVerbSynonyms = function Dictionary_testVerbSynonyms(word1, word2) {
    var prop, testword;

    // this is a private function so input is probably trustworthy
    if (word1 === word2) {
      return true;
    }

    // check that both are strings
    if ("string" !== typeof word1 || "string" !== typeof word2) {
      // warn because input is bad
      console.warn(
        "synonyms function takes two strings, but received:",
        word1,
        word2
      );
      return false;
    }

    // check if both verbs are undefined
    if (
      "undefined" === typeof this.verb_lookup[word1] &&
      "undefined" === typeof this.verb_lookup[word2]
    ) {
      // warn because input is bad
      console.warn(
        "Neither " + word1 + " nor " + word2 + " were found in the dictionary."
      );
      return false;
    }

    // check that both aren't unique verbs
    if (
      "undefined" !== typeof this.verb_lookup[word1] &&
      "undefined" !== typeof this.verb_lookup[word2]
    ) {
      return false;
    }

    if ("undefined" === typeof this.verb_lookup[word1]) {
      testword = word1;
      prop = word2;
    } else {
      testword = word2;
      prop = word1;
    }

    //console.log( "prop, testword", prop, testword);
    var synonyms = this.verb_lookup[prop].synonyms;

    // error check
    if ("undefined" === typeof synonyms) return false;

    for (var i = 0; i < synonyms.length; i++) {
      if (testword === synonyms[i]) {
        // found a match, return prop testword of synonym
        return prop;
      }
    }
    return false;
  };
})();