summaryrefslogtreecommitdiff
path: root/value-equal/modules/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'value-equal/modules/index.js')
-rw-r--r--value-equal/modules/index.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/value-equal/modules/index.js b/value-equal/modules/index.js
new file mode 100644
index 0000000..94d02c1
--- /dev/null
+++ b/value-equal/modules/index.js
@@ -0,0 +1,36 @@
+function valueOf(obj) {
+ return obj.valueOf ? obj.valueOf() : Object.prototype.valueOf.call(obj);
+}
+
+function valueEqual(a, b) {
+ // Test for strict equality first.
+ if (a === b) return true;
+
+ // Otherwise, if either of them == null they are not equal.
+ if (a == null || b == null) return false;
+
+ if (Array.isArray(a)) {
+ return (
+ Array.isArray(b) &&
+ a.length === b.length &&
+ a.every(function(item, index) {
+ return valueEqual(item, b[index]);
+ })
+ );
+ }
+
+ if (typeof a === 'object' || typeof b === 'object') {
+ var aValue = valueOf(a);
+ var bValue = valueOf(b);
+
+ if (aValue !== a || bValue !== b) return valueEqual(aValue, bValue);
+
+ return Object.keys(Object.assign({}, a, b)).every(function(key) {
+ return valueEqual(a[key], b[key]);
+ });
+ }
+
+ return false;
+}
+
+export default valueEqual;