Skip to content

Commit

Permalink
Implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 13, 2014
1 parent a52f527 commit 2669d7e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"use strict";

var toString = Object.prototype.toString;
var has = Object.prototype.hasOwnProperty;

var dateType = '[object Date]';
var regexType = '[object RegExp]';
var arrayType = '[object Array]';
var funcType = '[object Function]';
var objType = '[object Object]';

module.exports = function isEqual(value, other) {
if (value === other) { return true; }

var type = toString.call(value);
if (type !== toString.call(other)) { return false; }

if (type === dateType) { return value.getTime() === other.getTime(); }

if (type === regexType) { return String(value) === String(other); }

if (type === arrayType) {
if (value.length !== other.length) { return false; }

var index = value.length;
do {
--index;
} while (index > 0 && index in value && index in other && isEqual(value[index], other[index]));
return index <= 0;
}

if (type === funcType) {
return isEqual(value.name, other.name) && isEqual(String(value), String(other));
}

if (type === objType) {
if (Object.getPrototypeOf(value) !== Object.getPrototypeOf(other)) { return false; }
var key;
for (key in value) {
if (has.call(value, key)) {
if (!(key in other)) { return false; }
if (!isEqual(value[key], other[key])) { return false; }
}
}
for (key in other) {
if (has.call(other, key)) {
if (!(key in value)) { return false; }
if (!isEqual(other[key], value[key])) { return false; }
}
}
return true;
}

return false;
};

0 comments on commit 2669d7e

Please sign in to comment.