"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setPath = exports.isType = exports.hasPath = exports.getType = exports.deepClone = exports.clone = void 0;

/* eslint-disable no-restricted-syntax */
// 类型检测
var isType = function isType(value, type) {
  var _ref = {},
      toString = _ref.toString;
  return toString.call(value) === "[object ".concat(type, "]");
};

exports.isType = isType;

var clone = function clone(source) {
  if (!source) {
    return source;
  }

  var target = {}; // eslint-disable-next-line guard-for-in

  for (var k in source) {
    target[k] = source[k];
  }

  return target;
};

exports.clone = clone;

var getType = function getType(n) {
  return Object.prototype.toString.call(n).slice(8, -1);
};
/**
 * 深克隆
 * @param source 要深克隆的目标对象
 */


exports.getType = getType;

var deepClone = function deepClone(source) {
  if (!source) {
    return source;
  } // @ts-ignore


  var target = new source.constructor();

  for (var key in source) {
    if (source.hasOwnProperty(key)) {
      target[key] = getType(source[key]) === "Object" || getType(source[key]) === "Array" ? deepClone(source[key]) : source[key];
    }
  }

  return target;
};
/**
 * 存在时返回路径值，不存在时返回 undefined
 */


exports.deepClone = deepClone;

var hasPath = function hasPath(source, path) {
  var current = source;

  for (var i = 0; i < path.length; i += 1) {
    if (current && current[path[i]]) {
      current = current[path[i]];
    } else {
      current = undefined;
      break;
    }
  }

  return current;
};
/**
 * 内部指定 params ，不考虑复杂情况
 */


exports.hasPath = hasPath;

var setPath = function setPath(source, path, value) {
  if (!source) {
    return source;
  }

  var o = source;
  path.forEach(function (key, idx) {
    // 不是最后一个
    if (idx < path.length - 1) {
      o = o[key];
    } else {
      o[key] = value;
    }
  });
  return source;
};

exports.setPath = setPath;