{"size": 1741, "ext": "js", "max_stars_repo_path": "simpleDeepAssign.js", "max_stars_repo_name": "ygoto3/simple-deep-assign", "max_stars_repo_stars_event_min_datetime": "2019-01-11T10:46:42.000Z", "max_stars_repo_stars_event_max_datetime": "2019-01-11T10:46:42.000Z", "max_issues_repo_path": "simpleDeepAssign.js", "max_issues_repo_name": "ygoto3/simple-deep-assign", "max_issues_repo_issues_event_min_datetime": "2021-09-04T12:21:21.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-04T12:21:21.000Z", "max_forks_repo_path": "simpleDeepAssign.js", "max_forks_repo_name": "ygoto3/simple-deep-assign", "max_forks_repo_forks_event_min_datetime": "2021-01-10T09:12:48.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-10T09:12:48.000Z"}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 26.3787878788, "max_line_length": 129, "alphanum_fraction": 0.5979322229}
// @flow

/**
 * Check if it is an array
 * @private
 * @param {*} item - an item to check
 * @return {boolean} array or not
 * @example
 * isArray([0, 1, 2]) // true
 */
function isArray(item/*: any*/)/*: boolean*/ {
  return {}.toString.call(item) === '[object Array]';
}

/**
 * Check if it is an object
 * @private
 * @param {*} item - an item to check
 * @return {boolean} object or not
 * @example
 * isObject({ a: 'a' }) // true
 */
function isObject(item/*: any*/)/*: boolean*/ {
  return typeof item === 'object' && item !== null && !isArray(item);
}

/**
 * Recursively copy the values of all enumerable own properties from a source object to a target object 
 * @private
 * @param {Object} target - a target object
 * @param {Object} source - a source object
 * @example
 * deepAssignObject({ a: 'a', b: 'b' }, { b: 'B', c: 'c' });
 * // { a: 'a', b: 'B', c: 'c' }
 */
function deepAssignObject(target/*: Object*/, source/*: Object*/)/*: void*/ {
  Object.keys(source).forEach(key => {
    if (isObject(target[key]) && isObject(source[key])) {
      deepAssignObject(target[key], source[key]);
      return;
    }
    target[key] = source[key];
  });
}

/**
 * Recursively copy the values of all enumerable own properties from a source item to a target item if the both items are objects
 * @private
 * @param {Object} target - a target object
 * @param {Object} source - a source object
 * @example
 * deepAssign({ a: 'a', b: 'b' }, { b: 'B', c: 'c' });
 * // { a: 'a', b: 'B', c: 'c' }
 */
function deepAssign(target/*: any*/, source/*: any*/)/*: void*/ {
  if (!isObject(target) || !isObject(source)) return;
  deepAssignObject(target, source);
}

module.exports = {
  isArray,
  isObject,
  deepAssignObject,
  deepAssign,
};
