Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// deepSet.js
/*global adventurejs A*/
"use strict";

/**
 * This is a deep copy method used for assets and verbs.
 * Returns the object being set for chaining.
 * @memberOf adventurejs
 * @method adventurejs#deepSet
 * @param {Object} props A generic object containing properties to copy to the classed object.
 * @param {Object} object A class instance to copy properties to.
 * @returns {Object} Returns the instance the method is called on (useful for chaining calls.)
 * @chainable
 */
adventurejs.deepSet = function Adventurejs_deepSet(props, object) {
  //console.warn( 'deepSet', props.name || object.name || object.id );
  if (props === null) return object;

  for (var prop in props) {
    if (
      typeof props[prop] === "function" ||
      Array.isArray(props[prop]) ||
      typeof props[prop] !== "object"
    ) {
      // copy simple types and arrays
      object[prop] = props[prop];
    } else if ("undefined" === typeof props[prop]) {
      var msg = "Error during Atom construction. props[prop] is undefined. ";
      this.game.log("error", "high", msg);
      return false;
    } else {
      // incoming prop is classed but this prop is not
      // look for id in prop & if not found use prop as id

      if (props && props[prop] && props[prop].class) {
        var id = props[prop].id;
        if ("undefined" === typeof id) {
          props[prop].id = prop;
          id = prop;
        }

        object[prop] = A.clone.call(this.game, props[prop], object[prop]);
      }

      if ("object" === typeof object[prop]) {
        object[prop] = A.mergeWorld.call(this, props[prop], object[prop]);
      } else {
        /**
         * AS OF 12/19/22 THIS PATH SHOULD NEVER BE CALLED...?
         */

        // create prop on this if needed
        if ("undefined" === typeof object[prop]) object[prop] = props[prop];

        // copy nested properties
        for (var nestedprop in props[prop]) {
          // ignore native prototype properties
          if (!Object.prototype.hasOwnProperty.call(object[prop], nestedprop))
            continue;
          object[prop][nestedprop] = props[prop][nestedprop];
        }
      }
    }
  }

  return object;
};