From c03535d6dd670aef8d9f7e692c1ebe721a310b47 Mon Sep 17 00:00:00 2001 From: vruivo Date: Tue, 10 May 2016 01:16:18 +0100 Subject: [PATCH 1/4] fixed prototype comparison added RegExp comparison --- index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0772f8c..4b05c6a 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,16 @@ var deepEqual = module.exports = function (actual, expected, opts) { } else if (actual instanceof Date && expected instanceof Date) { return actual.getTime() === expected.getTime(); + // 7.3 If the expected value is a RegExp object, the actual value is + // equivalent if it is also a RegExp object with the same source and + // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). + } else if (actual instanceof RegExp && expected instanceof RegExp) { + return actual.source === expected.source && + actual.global === expected.global && + actual.multiline === expected.multiline && + actual.lastIndex === expected.lastIndex && + actual.ignoreCase === expected.ignoreCase; + // 7.3. Other pairs that do not both pass typeof value == 'object', // equivalence is determined by ==. } else if (!actual || !expected || typeof actual != 'object' && typeof expected != 'object') { @@ -42,10 +52,11 @@ function isBuffer (x) { function objEquiv(a, b, opts) { var i, key; + if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) return false; // an identical 'prototype' property. - if (a.prototype !== b.prototype) return false; + if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false; //~~~I've managed to break Object.keys through screwy arguments passing. // Converting to array solves the problem. if (isArguments(a)) { From a1fa20f60abc5dcd9f25a7884d6af69346055414 Mon Sep 17 00:00:00 2001 From: vruivo Date: Tue, 10 May 2016 01:18:12 +0100 Subject: [PATCH 2/4] added some tests for objects & RegExp --- test/cmp.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/test/cmp.js b/test/cmp.js index 2aab5f9..bc07ae9 100644 --- a/test/cmp.js +++ b/test/cmp.js @@ -59,10 +59,10 @@ test('arguments class', function (t) { test('test the arguments shim', function (t) { t.ok(isArguments.supported((function(){return arguments})())); t.notOk(isArguments.supported([1,2,3])); - + t.ok(isArguments.unsupported((function(){return arguments})())); t.notOk(isArguments.unsupported([1,2,3])); - + t.end(); }); @@ -93,3 +93,31 @@ test('null == undefined', function (t) { t.notOk(equal(null, undefined, { strict: true })) t.end() }) + +test('test objects', function (t) { + var a = { a: "A" }; + var b = Object.create(a); + b.b = "B"; + var c = Object.create(a); + c.b = "C"; + + t.notOk(equal(b, c)); + t.end(); +}); + +test('test objects', function (t) { + var d = new Date(1387585278000); + var e = new RegExp("abc"); + t.notOk(equal(d, e)); + t.end(); +}); + +test('test RegExp !=', function (t) { + t.notOk(equal(/abc/, /xyz/)); + t.end(); +}); + +test('test RegExp ==', function (t) { + t.ok(equal(/abc/, /abc/)); + t.end(); +}); From 37ceddeb7a2fedcf764af2a0ae28be5bee698cf8 Mon Sep 17 00:00:00 2001 From: vruivo Date: Tue, 10 May 2016 21:39:35 +0100 Subject: [PATCH 3/4] added a polyfill for Object.getPrototypeOf removed test with Object.create --- index.js | 4 ++++ test/cmp.js | 13 +------------ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 4b05c6a..57a7f43 100644 --- a/index.js +++ b/index.js @@ -55,6 +55,10 @@ function objEquiv(a, b, opts) { if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) return false; + + Object.getPrototypeOf = Object.getPrototypeOf || function(obj) { + return obj.__proto__; + } // an identical 'prototype' property. if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false; //~~~I've managed to break Object.keys through screwy arguments passing. diff --git a/test/cmp.js b/test/cmp.js index bc07ae9..1cb6b9a 100644 --- a/test/cmp.js +++ b/test/cmp.js @@ -62,7 +62,7 @@ test('test the arguments shim', function (t) { t.ok(isArguments.unsupported((function(){return arguments})())); t.notOk(isArguments.unsupported([1,2,3])); - + t.end(); }); @@ -94,17 +94,6 @@ test('null == undefined', function (t) { t.end() }) -test('test objects', function (t) { - var a = { a: "A" }; - var b = Object.create(a); - b.b = "B"; - var c = Object.create(a); - c.b = "C"; - - t.notOk(equal(b, c)); - t.end(); -}); - test('test objects', function (t) { var d = new Date(1387585278000); var e = new RegExp("abc"); From 742ab9ac96347f827fa24d23ff92f88330533d7b Mon Sep 17 00:00:00 2001 From: vruivo Date: Wed, 11 May 2016 20:50:11 +0100 Subject: [PATCH 4/4] redefine Object.getPrototypeOf if needed --- index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 57a7f43..3f7948d 100644 --- a/index.js +++ b/index.js @@ -56,9 +56,15 @@ function objEquiv(a, b, opts) { if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) return false; - Object.getPrototypeOf = Object.getPrototypeOf || function(obj) { - return obj.__proto__; + var abc = 5; + try { + Object.getPrototypeOf(abc); // all ok ES6 + } catch (e) { // error ES5 + Object.getPrototypeOf = function(obj) { + return obj.__proto__; + } } + // an identical 'prototype' property. if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false; //~~~I've managed to break Object.keys through screwy arguments passing.