const isEqualTwoObjects = (firstObj, secondObj) => {
if (firstObj === secondObj) {
return true
}
const firstObjKeys = Object.getOwnPropertyNames(firstObj)
const secondObjKeys = Object.getOwnPropertyNames(secondObj)
if (firstObjKeys.length !== secondObjKeys.length) {
return false
}
const set = new Set(firstObjKeys)
const bool = secondObjKeys.every(item => set.has(item))
if (!bool) {
return false
}
const getType = value => {
const result = Object.prototype.toString.call(value).split(' ')
const str = result[1]
const len = str.length
const type = str.substring(0, len - 1).toLowerCase()
return type
}
for (let i = 0; i < firstObjKeys.length; i++) {
const item = firstObjKeys[i]
const firstObjValue = firstObj[item]
const secondObjValue = secondObj[item]
const firstObjValueType = getType(firstObjValue)
const secondObjValueType = getType(secondObjValue)
if (firstObjValueType !== secondObjValueType) {
return false
}
if (firstObjValueType === 'function' || firstObjValueType === 'array') {
if (firstObjValue.toString() !== secondObjValue.toString()) {
return false
}
} else if (firstObjValueType === 'object') {
if (!isEqualTwoObjects(firstObjValue, secondObjValue)) {
return false
}
} else if (firstObjValue !== secondObjValue) {
return false
}
}
return true
}