Skip to content

Commit 05659ac

Browse files
committed
[Breaking] boxed primitives are unwrapped for comparisons
1 parent ef7e7fb commit 05659ac

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var isRegex = require('is-regex');
55
var flags = require('regexp.prototype.flags');
66
var isArray = require('isarray');
77
var isDate = require('is-date-object');
8+
var isBoxedPrimitive = require('is-boxed-primitive');
9+
var toPrimitive = require('es-to-primitive/es2015'); // TODO: replace this with ES2020 once updated
810

911
var getTime = Date.prototype.getTime;
1012
var gPO = Object.getPrototypeOf;
@@ -18,6 +20,12 @@ function deepEqual(actual, expected, options) {
1820
return true;
1921
}
2022

23+
var actualBoxed = isBoxedPrimitive(actual);
24+
var expectedBoxed = isBoxedPrimitive(expected);
25+
if (actualBoxed || expectedBoxed) {
26+
return deepEqual(toPrimitive(actual), toPrimitive(expected), opts);
27+
}
28+
2129
// 7.3. Other pairs that do not both pass typeof value == 'object', equivalence is determined by ==.
2230
if (!actual || !expected || (typeof actual !== 'object' && typeof expected !== 'object')) {
2331
return opts.strict ? is(actual, expected) : actual == expected;
@@ -53,8 +61,9 @@ function isBuffer(x) {
5361
}
5462

5563
function objEquiv(a, b, opts) {
56-
/* eslint max-statements: [2, 70] */
64+
/* eslint max-statements: [2, 70], max-lines-per-function: [2, 80] */
5765
var i, key;
66+
5867
if (typeof a !== typeof b) { return false; }
5968
if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) { return false; }
6069

@@ -121,6 +130,7 @@ function objEquiv(a, b, opts) {
121130
for (i = ka.length - 1; i >= 0; i--) {
122131
if (ka[i] != kb[i]) { return false; }
123132
}
133+
124134
// equivalent values for every corresponding key, and ~~~possibly expensive deep test
125135
for (i = ka.length - 1; i >= 0; i--) {
126136
key = ka[i];

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"test": "npm run tests-only"
1616
},
1717
"dependencies": {
18+
"es-to-primitive": "^1.2.0",
1819
"is-arguments": "^1.0.4",
20+
"is-boxed-primitive": "^1.0.0",
1921
"is-date-object": "^1.0.1",
2022
"is-regex": "^1.0.4",
2123
"isarray": "^2.0.5",

test/cmp.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,27 @@ test('toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (t) {
400400

401401
t.end();
402402
});
403+
404+
test('boxed primitives', function (t) {
405+
t.deepEqualTest(Object(false), false, 'boxed and primitive `false`', true, true);
406+
t.deepEqualTest(Object(true), true, 'boxed and primitive `true`', true, true);
407+
t.deepEqualTest(Object(3), 3, 'boxed and primitive `3`', true, true);
408+
t.deepEqualTest(Object(NaN), NaN, 'boxed and primitive `NaN`', false, true);
409+
t.deepEqualTest(Object(''), '', 'boxed and primitive `""`', true, true);
410+
t.deepEqualTest(Object('str'), 'str', 'boxed and primitive `"str"`', true, true);
411+
412+
t.test('symbol', { skip: !hasSymbols }, function (st) {
413+
var s = Symbol('');
414+
st.deepEqualTest(Object(s), s, 'boxed and primitive `Symbol()`', true, true);
415+
st.end();
416+
});
417+
418+
/* globals BigInt: false */
419+
t.test('bigint', { skip: typeof BigInt !== 'function' }, function (st) {
420+
var hhgtg = BigInt(42);
421+
st.deepEqualTest(Object(hhgtg), hhgtg, 'boxed and primitive `BigInt(42)`', true, true);
422+
st.end();
423+
});
424+
425+
t.end();
426+
});

0 commit comments

Comments
 (0)