Skip to content

Commit

Permalink
[Fix] properly compare RegExp objects
Browse files Browse the repository at this point in the history
 - [Tests] add tests with `Object.create`
  • Loading branch information
vruivo authored and ljharb committed Jul 31, 2019
1 parent 6a5efc1 commit b8c179c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var objectKeys = require('object-keys');
var isArguments = require('is-arguments');
var is = require('object-is');
var isRegex = require('is-regex');
var flags = require('regexp.prototype.flags');

function deepEqual(actual, expected, options) {
var opts = options || {};
Expand All @@ -10,6 +12,15 @@ function deepEqual(actual, expected, options) {
return true;
}

var actualIsRegex = isRegex(actual);
var expectedIsRegex = isRegex(expected);
if (actualIsRegex || expectedIsRegex) {
if (actualIsRegex !== expectedIsRegex) {
return false;
}
return actual.source === expected.source && flags(actual) === flags(expected);
}

if (actual instanceof Date && expected instanceof Date) {
return actual.getTime() === expected.getTime();
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
},
"dependencies": {
"is-arguments": "^1.0.4",
"is-regex": "^1.0.4",
"object-is": "^1.0.1",
"object-keys": "^1.1.1"
"object-keys": "^1.1.1",
"regexp.prototype.flags": "^1.2.0"
},
"devDependencies": {
"@ljharb/eslint-config": "^13.1.1",
Expand Down
43 changes: 43 additions & 0 deletions test/cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,46 @@ test('zeroes', function (t) {

t.end();
});

test('test objects', { skip: !Object.create }, 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), 'two objects with the same [[Prototype]] but a different own property are not equal');
t.notOk(equal(c, b), 'two objects with the same [[Prototype]] but a different own property are not equal');

t.notOk(equal(b, c, { strict: true }), 'strict: two objects with the same [[Prototype]] but a different own property are not equal');
t.notOk(equal(c, b, { strict: true }), 'strict: two objects with the same [[Prototype]] but a different own property are not equal');

t.end();
});

test('regexes vs dates', function (t) {
var d = new Date(1387585278000);
var r = /abc/;

t.notOk(equal(d, r), 'date and regex are not equal');
t.notOk(equal(r, d), 'regex and date are not equal');

t.notOk(equal(d, r, { strict: true }), 'strict: date and regex are not equal');
t.notOk(equal(r, d, { strict: true }), 'strict: regex and date are not equal');

t.end();
});

test('regexen', function (t) {
t.notOk(equal(/abc/, /xyz/), 'two different regexes are not equal');
t.notOk(equal(/xyz/, /abc/), 'two different regexes are not equal');
t.ok(equal(/abc/, /abc/), 'two same regexes are equal');
t.ok(equal(/xyz/, /xyz/), 'two same regexes are equal');

t.notOk(equal(/abc/, /xyz/, { strict: true }), 'strict: two different regexes are not equal');
t.notOk(equal(/xyz/, /abc/, { strict: true }), 'strict: two different regexes are not equal');
t.ok(equal(/abc/, /abc/, { strict: true }), 'strict: two same regexes are not equal');
t.ok(equal(/xyz/, /xyz/, { strict: true }), 'strict: two same regexes are not equal');

t.end();
});

0 comments on commit b8c179c

Please sign in to comment.