Skip to content

Commit a159a3a

Browse files
committed
[Breaking] boxed primitives are unwrapped for comparisons
1 parent 45431b6 commit a159a3a

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
@@ -419,3 +419,27 @@ test('toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (t) {
419419

420420
t.end();
421421
});
422+
423+
test('boxed primitives', function (t) {
424+
t.deepEqualTest(Object(false), false, 'boxed and primitive `false`', true, true);
425+
t.deepEqualTest(Object(true), true, 'boxed and primitive `true`', true, true);
426+
t.deepEqualTest(Object(3), 3, 'boxed and primitive `3`', true, true);
427+
t.deepEqualTest(Object(NaN), NaN, 'boxed and primitive `NaN`', false, true);
428+
t.deepEqualTest(Object(''), '', 'boxed and primitive `""`', true, true);
429+
t.deepEqualTest(Object('str'), 'str', 'boxed and primitive `"str"`', true, true);
430+
431+
t.test('symbol', { skip: !hasSymbols }, function (st) {
432+
var s = Symbol('');
433+
st.deepEqualTest(Object(s), s, 'boxed and primitive `Symbol()`', true, true);
434+
st.end();
435+
});
436+
437+
/* globals BigInt: false */
438+
t.test('bigint', { skip: typeof BigInt !== 'function' }, function (st) {
439+
var hhgtg = BigInt(42);
440+
st.deepEqualTest(Object(hhgtg), hhgtg, 'boxed and primitive `BigInt(42)`', true, true);
441+
st.end();
442+
});
443+
444+
t.end();
445+
});

0 commit comments

Comments
 (0)