Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed prototype comparison #40

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -42,10 +52,21 @@ function isBuffer (x) {

function objEquiv(a, b, opts) {
var i, key;

if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false;

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 (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)) {
Expand Down
19 changes: 18 additions & 1 deletion test/cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ 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]));

Expand Down Expand Up @@ -93,3 +93,20 @@ test('null == undefined', function (t) {
t.notOk(equal(null, undefined, { strict: true }))
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();
});