'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var isArr = _interopDefault(require('./isArr'));
var isObj = _interopDefault(require('./isObj'));

/**
 * 判断两个对象或数组内容是否相等
 * @param {*} obj1
 * @param {*} obj2
 * @returns
 *
 * isDeepEqual([1,2], [1,2]); // => true;
 * isDeepEqual({name: 'Tom', age: 18}, {name: 'Tom', age: 20});  // => false
 */
function isDeepEqual(obj1, obj2) {
  if (typeof obj1 !== typeof obj2) {
    return false;
  }

  if (isArr(obj1) && isArr(obj2)) {
    if (obj1.length !== obj2.length) return false;

    if (obj1.length) {
      return obj1.every(function (item, i) {
        return isDeepEqual(item, obj2[i]);
      });
    }

    return true;
  }

  if (isObj(obj1) && isObj(obj2)) {
    var keys1 = Object.keys(obj1);
    var keys2 = Object.keys(obj2);
    if (keys1.length !== keys2.length) return false;

    if (keys1.length) {
      return keys1.every(function (item, i) {
        if (keys2[i] !== item) return false;
        return isDeepEqual(obj1[item], obj2[item]);
      });
    }

    return true;
  }

  return obj1 === obj2;
}

module.exports = isDeepEqual;
