'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function isObjOrArray(obj) {
  return obj instanceof Object;
}
function isArray(obj) {
  return Array.isArray(obj);
}
function toHump(str) {
  return str.replace(/_(\w)/g, function (a, b) {
    return b.toUpperCase();
  });
}
/**
 * 将对象中的key由下划线专为驼峰
 *
 * @param {object} obj - 对象
 * @returns {object} 转化后的对象
 *
 * @example
 * const obj = {
 *   a_a: 'a',
 *   b_b: [
 *     {
 *       bb_b: 'b',
 *     },
 *   ],
 *   c: {
 *     dd_d: 'd',
 *     e: {
 *       ee_e: 'e',
 *     },
 *   },
 * };
 *
 * toHumpObj(obj);
 * // { aA: 'a', bB: [ { bbB: 'b' } ], c: { ddD: 'd', e: { eeE: 'e' } } }
 */
function toHumpObj(obj) {
  var cache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new WeakMap();
  // 函数首次调用判断
  if (!isObjOrArray(obj)) return obj;
  if (cache.get(obj)) {
    return cache.get(obj);
  }
  var result = isArray(obj) ? [] : {};
  cache.set(obj, result);
  var keys = Object.keys(obj);
  keys.forEach(function (key) {
    var value;
    if (Array.isArray(obj)) {
      key = +key;
      value = obj[key];
    } else {
      value = obj[key];
    }
    var nKey = toHump("".concat(key));
    var temp = isObjOrArray(value) ? toHumpObj(value, cache) : value;
    if (Array.isArray(result)) {
      nKey = +nKey;
      result[nKey] = temp;
    } else {
      result[nKey] = temp;
    }
  });
  return result;
}
/**
 * 将属性混合到目标对象中
 * @param {object} to 目标对象
 * @param {object} from 原始对象
 * @returns 处理后的对象
 *
 * @example
 * const a = { name: 'lee' }
 * const b = { age: 3 }
 * extend(a, b)
 *
 * console.log(a)
 *
 * // => { name: 'lee', age: 3 }
 */
function extend(to, from) {
  // eslint-disable-next-line no-restricted-syntax, guard-for-in
  for (var key in from) {
    to[key] = from[key];
  }
  return to;
}

exports.extend = extend;
exports.toHumpObj = toHumpObj;
