Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// deepSet.js
/*global adventurejs A*/

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

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

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

      if ("object" === typeof object[key]) {
        object[key] = A.mergeWorld.call(this, props[key], object[key]);
      } else {
        /**
         * AS OF 12/19/22 THIS PATH SHOULD NEVER BE CALLED...?
         *
         * 4/9/25 found that we do reach this path, at least with
         * game.settings.set() where settings.prop is a string
         * and incoming prop is an object
         */

        // is prop present on target? create prop if needed
        if ("undefined" === typeof object[key]) object[key] = props[key];

        // is target.prop not an object? make it an object
        if ("object" !== typeof object[key]) object[key] = props[key];

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

  return object;
};