function isArray(obj) {
  return Object.prototype.toString.call(obj) === '[object Array]'
}

function copy(target, ...sources) {
  if (!target) return target;
  sources.forEach(data => {
    data && Object.keys(data).map(key => {
      const temp = data[key];
      const isObj = typeof temp === 'object';
      if (!temp || !isObj || isArray(temp) || !Object.keys(temp).length) {
        target[key] = temp;
        return;
      }
      target[key] = copy(target[key], temp);
    });
  });
  return target;
}

export default {
  /**
   * 深层拷贝
   */
  deepCopy(target, ...sources) {
    return copy(target, ...sources);
  },

  /**
   * 多拷贝一层
   */
  copy2th(target, source) {
    if (!target || !source) return;
    for (let i in source) {
      const temp = source[i];
      if (typeof temp === 'object' && !isArray(temp)) {
        // TODO: 非枚举 extents class 继承创建的class的属性都是非枚举的
        target[i] = Object.assign(target[i] || {}, temp);
      } else {
        target[i] = temp;
      }
    }
    return target;
  }
}