Skip to content

Commit

Permalink
[Breaking] boxed primitives are not silently unboxed
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 30, 2019
1 parent 43b1eef commit a42223e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
15 changes: 5 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ var isRegex = require('is-regex');
var flags = require('regexp.prototype.flags');
var isArray = require('isarray');
var isDate = require('is-date-object');
var isBoxedPrimitive = require('is-boxed-primitive');
var unboxPrimitive = require('unbox-primitive');
var whichBoxedPrimitive = require('which-boxed-primitive');
var callBound = require('es-abstract/helpers/callBound');

var $getTime = callBound('Date.prototype.getTime');
Expand All @@ -21,14 +20,10 @@ function deepEqual(actual, expected, options) {
return true;
}

var actualBoxed = isBoxedPrimitive(actual);
var expectedBoxed = isBoxedPrimitive(expected);
if (actualBoxed || expectedBoxed) {
return deepEqual(
actualBoxed ? unboxPrimitive(actual) : actual,
expectedBoxed ? unboxPrimitive(expected) : expected,
opts
);
var actualBoxed = whichBoxedPrimitive(actual);
var expectedBoxed = whichBoxedPrimitive(expected);
if (actualBoxed !== expectedBoxed) {
return false;
}

// 7.3. Other pairs that do not both pass typeof value == 'object', equivalence is determined by ==.
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
"dependencies": {
"es-abstract": "^1.16.2",
"is-arguments": "^1.0.4",
"is-boxed-primitive": "^1.0.0",
"is-date-object": "^1.0.1",
"is-regex": "^1.0.4",
"isarray": "^2.0.5",
"object-is": "^1.0.1",
"object-keys": "^1.1.1",
"regexp.prototype.flags": "^1.2.0",
"unbox-primitive": "^1.0.0"
"which-boxed-primitive": "^1.0.1"
},
"devDependencies": {
"@ljharb/eslint-config": "^15.0.2",
Expand Down
20 changes: 10 additions & 10 deletions test/cmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,32 +461,32 @@ test('toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (t) {
});

test('boxed primitives', function (t) {
t.deepEqualTest(Object(false), false, 'boxed and primitive `false`', true, true);
t.deepEqualTest(Object(true), true, 'boxed and primitive `true`', true, true);
t.deepEqualTest(Object(3), 3, 'boxed and primitive `3`', true, true);
t.deepEqualTest(Object(NaN), NaN, 'boxed and primitive `NaN`', false, true);
t.deepEqualTest(Object(''), '', 'boxed and primitive `""`', true, true);
t.deepEqualTest(Object('str'), 'str', 'boxed and primitive `"str"`', true, true);
t.deepEqualTest(Object(false), false, 'boxed and primitive `false`', false, false);
t.deepEqualTest(Object(true), true, 'boxed and primitive `true`', false, false);
t.deepEqualTest(Object(3), 3, 'boxed and primitive `3`', false, false);
t.deepEqualTest(Object(NaN), NaN, 'boxed and primitive `NaN`', false, false);
t.deepEqualTest(Object(''), '', 'boxed and primitive `""`', false, false);
t.deepEqualTest(Object('str'), 'str', 'boxed and primitive `"str"`', false, false);

t.test('symbol', { skip: !hasSymbols }, function (st) {
var s = Symbol('');
st.deepEqualTest(Object(s), s, 'boxed and primitive `Symbol()`', true, true);
st.deepEqualTest(Object(s), s, 'boxed and primitive `Symbol()`', false, false);
st.end();
});

t.test('bigint', { skip: typeof BigInt !== 'function' }, function (st) {
var hhgtg = BigInt(42);
st.deepEqualTest(Object(hhgtg), hhgtg, 'boxed and primitive `BigInt(42)`', true, true);
st.deepEqualTest(Object(hhgtg), hhgtg, 'boxed and primitive `BigInt(42)`', false, false);
st.end();
});

t.test('`valueOf` is not called for boxed primitives', function (st) {
t.test('`valueOf` is called for boxed primitives', function (st) {
var a = Object(5);
a.valueOf = function () { throw new Error('failed'); };
var b = Object(5);
b.valueOf = function () { throw new Error('failed'); };

st.deepEqualTest(a, b, 'two boxed numbers with a thrower valueOf', true, true);
st.deepEqualTest(a, b, 'two boxed numbers with a thrower valueOf', false, false);

st.end();
});
Expand Down

0 comments on commit a42223e

Please sign in to comment.