Skip to content

Commit 28fde4a

Browse files
grahambinnsljharb
authored andcommitted
[Fix] use object-is to support NaN and -0 properly
1 parent 7000403 commit 28fde4a

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
var objectKeys = require('object-keys');
22
var isArguments = require('is-arguments');
3+
var is = require('object-is');
34

45
function deepEqual(actual, expected, options) {
56
var opts = options || {};
7+
68
// 7.1. All identical values are equivalent, as determined by ===.
7-
if (actual === expected) {
9+
if (opts.strict ? is(actual, expected) : actual === expected) {
810
return true;
11+
}
912

10-
} else if (actual instanceof Date && expected instanceof Date) {
13+
if (actual instanceof Date && expected instanceof Date) {
1114
return actual.getTime() === expected.getTime();
15+
}
1216

1317
// 7.3. Other pairs that do not both pass typeof value == 'object', equivalence is determined by ==.
14-
} else if (!actual || !expected || (typeof actual != 'object' && typeof expected != 'object')) {
15-
return opts.strict ? actual === expected : actual == expected;
18+
if (!actual || !expected || (typeof actual != 'object' && typeof expected != 'object')) {
19+
return opts.strict ? is(actual, expected) : actual == expected;
1620
}
1721

1822
/*

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"dependencies": {
1818
"is-arguments": "^1.0.4",
19+
"object-is": "^1.0.1",
1920
"object-keys": "^1.1.1"
2021
},
2122
"devDependencies": {

test/cmp.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,40 @@ test('null == undefined', function (t) {
191191
t.notOk(equal(null, undefined, { strict: true }));
192192
t.end();
193193
});
194+
195+
test('null == undefined', function (t) {
196+
t.ok(equal(null, undefined), 'null == undefined');
197+
t.ok(equal(undefined, null), 'undefined == null');
198+
t.notOk(equal(null, undefined, { strict: true }), 'null !== undefined');
199+
t.notOk(equal(undefined, null, { strict: true }), 'undefined !== null');
200+
t.end();
201+
});
202+
203+
test('NaNs', function (t) {
204+
t.notOk(equal(NaN, NaN), 'NaN is not NaN');
205+
t.ok(equal(NaN, NaN, { strict: true }), 'strict: NaN is NaN');
206+
207+
t.notOk(equal({ a: NaN }, { a: NaN }), 'two equiv objects with a NaN value are not equiv');
208+
t.ok(equal({ a: NaN }, { a: NaN }, { strict: true }), 'strict: two equiv objects with a NaN value are equiv');
209+
210+
t.notOk(equal(NaN, 1), 'NaN !== 1');
211+
t.notOk(equal(NaN, 1, { strict: true }), 'strict: NaN !== 1');
212+
213+
t.end();
214+
});
215+
216+
test('zeroes', function (t) {
217+
t.ok(equal(0, -0), '0 is -0');
218+
t.ok(equal(-0, 0), '-0 is 0');
219+
220+
t.notOk(equal(0, -0, { strict: true }), 'strict: 0 is -0');
221+
t.notOk(equal(-0, 0, { strict: true }), 'strict: -0 is 0');
222+
223+
t.ok(equal({ a: 0 }, { a: -0 }), 'two objects with a same-keyed 0/-0 value are equal');
224+
t.ok(equal({ a: -0 }, { a: 0 }), 'two objects with a same-keyed -0/0 value are equal');
225+
226+
t.notOk(equal({ a: 0 }, { a: -0 }, { strict: true }), 'strict: two objects with a same-keyed 0/-0 value are equal');
227+
t.notOk(equal({ a: -0 }, { a: 0 }, { strict: true }), 'strict: two objects with a same-keyed -0/0 value are equal');
228+
229+
t.end();
230+
});

0 commit comments

Comments
 (0)