Skip to content

Commit b8c179c

Browse files
vruivoljharb
authored andcommitted
[Fix] properly compare RegExp objects
- [Tests] add tests with `Object.create`
1 parent 6a5efc1 commit b8c179c

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var objectKeys = require('object-keys');
22
var isArguments = require('is-arguments');
33
var is = require('object-is');
4+
var isRegex = require('is-regex');
5+
var flags = require('regexp.prototype.flags');
46

57
function deepEqual(actual, expected, options) {
68
var opts = options || {};
@@ -10,6 +12,15 @@ function deepEqual(actual, expected, options) {
1012
return true;
1113
}
1214

15+
var actualIsRegex = isRegex(actual);
16+
var expectedIsRegex = isRegex(expected);
17+
if (actualIsRegex || expectedIsRegex) {
18+
if (actualIsRegex !== expectedIsRegex) {
19+
return false;
20+
}
21+
return actual.source === expected.source && flags(actual) === flags(expected);
22+
}
23+
1324
if (actual instanceof Date && expected instanceof Date) {
1425
return actual.getTime() === expected.getTime();
1526
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
},
1717
"dependencies": {
1818
"is-arguments": "^1.0.4",
19+
"is-regex": "^1.0.4",
1920
"object-is": "^1.0.1",
20-
"object-keys": "^1.1.1"
21+
"object-keys": "^1.1.1",
22+
"regexp.prototype.flags": "^1.2.0"
2123
},
2224
"devDependencies": {
2325
"@ljharb/eslint-config": "^13.1.1",

test/cmp.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,46 @@ test('zeroes', function (t) {
228228

229229
t.end();
230230
});
231+
232+
test('test objects', { skip: !Object.create }, function (t) {
233+
var a = { a: 'A' };
234+
var b = Object.create(a);
235+
b.b = 'B';
236+
var c = Object.create(a);
237+
c.b = 'C';
238+
239+
t.notOk(equal(b, c), 'two objects with the same [[Prototype]] but a different own property are not equal');
240+
t.notOk(equal(c, b), 'two objects with the same [[Prototype]] but a different own property are not equal');
241+
242+
t.notOk(equal(b, c, { strict: true }), 'strict: two objects with the same [[Prototype]] but a different own property are not equal');
243+
t.notOk(equal(c, b, { strict: true }), 'strict: two objects with the same [[Prototype]] but a different own property are not equal');
244+
245+
t.end();
246+
});
247+
248+
test('regexes vs dates', function (t) {
249+
var d = new Date(1387585278000);
250+
var r = /abc/;
251+
252+
t.notOk(equal(d, r), 'date and regex are not equal');
253+
t.notOk(equal(r, d), 'regex and date are not equal');
254+
255+
t.notOk(equal(d, r, { strict: true }), 'strict: date and regex are not equal');
256+
t.notOk(equal(r, d, { strict: true }), 'strict: regex and date are not equal');
257+
258+
t.end();
259+
});
260+
261+
test('regexen', function (t) {
262+
t.notOk(equal(/abc/, /xyz/), 'two different regexes are not equal');
263+
t.notOk(equal(/xyz/, /abc/), 'two different regexes are not equal');
264+
t.ok(equal(/abc/, /abc/), 'two same regexes are equal');
265+
t.ok(equal(/xyz/, /xyz/), 'two same regexes are equal');
266+
267+
t.notOk(equal(/abc/, /xyz/, { strict: true }), 'strict: two different regexes are not equal');
268+
t.notOk(equal(/xyz/, /abc/, { strict: true }), 'strict: two different regexes are not equal');
269+
t.ok(equal(/abc/, /abc/, { strict: true }), 'strict: two same regexes are not equal');
270+
t.ok(equal(/xyz/, /xyz/, { strict: true }), 'strict: two same regexes are not equal');
271+
272+
t.end();
273+
});

0 commit comments

Comments
 (0)