Skip to content

Commit

Permalink
fix: catch fake collections throwing (#100) (#101)
Browse files Browse the repository at this point in the history
Fixes #99
  • Loading branch information
ljharb authored Jun 5, 2024
1 parent 0e417c7 commit 377458d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,18 @@ function regexpEqual(leftHandOperand, rightHandOperand) {
*/

function entriesEqual(leftHandOperand, rightHandOperand, options) {
// IE11 doesn't support Set#entries or Set#@@iterator, so we need manually populate using Set#forEach
if (leftHandOperand.size !== rightHandOperand.size) {
try {
// IE11 doesn't support Set#entries or Set#@@iterator, so we need manually populate using Set#forEach
if (leftHandOperand.size !== rightHandOperand.size) {
return false;
}
if (leftHandOperand.size === 0) {
return true;
}
} catch (sizeError) {
// things that aren't actual Maps or Sets will throw here
return false;
}
if (leftHandOperand.size === 0) {
return true;
}
var leftHandItems = [];
var rightHandItems = [];
leftHandOperand.forEach(function gatherEntries(key, value) {
Expand Down
16 changes: 16 additions & 0 deletions test/new-ecmascript-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ describe('ES2015 Specific', function () {
assert(eql(mapA, mapB), 'eql(Map { a => 1, b => 2, c => 3 }, Map { a => 1, b => 2, c => 3 })');
});

(setExists ? it : it.skip)('returns false for fake Maps', function () {
var maplikeSet = new Set();
Object.defineProperty(maplikeSet, 'constructor', { enumerable: false, value: Map });
Object.setPrototypeOf(maplikeSet, Map.prototype);

assert(eql(maplikeSet, new Map()) === false, 'eql(Set pretending to be a Map, Map { })');
});

});

describeIf(symbolAndMapExist && typeof Map.prototype[Symbol.iterator] === 'function')('map iterator', function () {
Expand Down Expand Up @@ -240,6 +248,14 @@ describe('ES2015 Specific', function () {
assert(eql(setA, setB) === true, 'eql(Set { -> }, Set { <- }) === true');
});

(mapExists ? it : it.skip)('returns false for fake Sets', function () {
var setlikeMap = new Map();
Object.defineProperty(setlikeMap, 'constructor', { enumerable: false, value: Set });
Object.setPrototypeOf(setlikeMap, Set.prototype);

assert(eql(setlikeMap, new Set()) === false, 'eql(Map pretending to be a Set, Set { })');
});

});

describeIf(symbolAndSetExist && typeof Set.prototype[Symbol.iterator] === 'function')('set iterator', function () {
Expand Down

0 comments on commit 377458d

Please sign in to comment.