1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
function getType(data) { return Object.prototype.toString.call(data).substring(8).split(/]/)[0] }
function comparisonObject(sourceObj, compareObj) { if (arguments.length < 2) throw "Incorrect number of parameters"; let sourceType = getType(sourceObj); if (sourceType !== getType(compareObj)) return false; if (sourceType !== "Array" && sourceType !== "Object" && sourceType !== "Set" && sourceType !== "Map") { if (sourceType === "Number" && sourceObj.toString() === "NaN") { return compareObj.toString() === "NaN" } if (sourceType === "Date" || sourceType === "RegExp") { return sourceObj.toString() === compareObj.toString() } return sourceObj === compareObj } else if (sourceType === "Array") { if (sourceObj.length !== compareObj.length) return false; if (sourceObj.length === 0) return true; for (let i = 0; i < sourceObj.length; i++) { if (!comparisonObject(sourceObj[i], compareObj[i])) return false; } } else if (sourceType === "Object") { let sourceKeyList = Reflect.ownKeys(sourceObj); let compareKeyList = Reflect.ownKeys(compareObj); let key; if (sourceKeyList.length !== compareKeyList.length) return false; for (let i = 0; i < sourceKeyList.length; i++) { key = sourceKeyList[i]; if (key !== compareKeyList[i]) return false; if (!comparisonObject(sourceObj[key], compareObj[key])) return false; } } else if (sourceType === "Set" || sourceType === "Map") { if (!comparisonObject(Array.from(sourceObj), Array.from(compareObj))) return false; } return true; }
|