Created
May 8, 2017 04:22
-
-
Save bravelincy/0255d9a521ef831a34e042e3f51b68fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isArray = Array.isArray; | |
const toString = Object.prototype.toString; | |
function isDate(value) { | |
return toString.call(value) === '[object Date]'; | |
} | |
function isRegExp(value) { | |
return toString.call(value) === '[object RegExp]'; | |
} | |
function equals(v1, v2) { | |
if (v1 === v2) return true; | |
if (v1 !== v1 && v2 !== v2) return true; // NaN | |
let t1 = typeof v1, t2 = typeof v2; | |
if (t1 === t2 && t1 === 'object') { | |
if (isArray(v1)) { | |
if (!isArray(v2)) return false; | |
if (v1.length === v2.length) { | |
return v1.every((item, index) => { | |
return equals(item, v2[index]); | |
}); | |
} | |
} else if(isDate(v1)) { | |
if (!isDate(v2)) return false; | |
return +v1 === +v2; | |
} else if(isRegExp(v1)) { | |
if (!isRegExp(v2)) return false; | |
return v1.toString() === v2.toString(); | |
} else { | |
for (let k in v1) { | |
let vv1 = v1[k], vv2 = v2[k]; | |
if (!equals(vv1, vv2)) return false; | |
} | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment