Skip to content

Commit

Permalink
fix: catch fake collections throwing (#100)
Browse files Browse the repository at this point in the history
Fixes #99
  • Loading branch information
ljharb authored Jun 3, 2024
1 parent 4e914be commit 8c14040
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,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
14 changes: 14 additions & 0 deletions test/new-ecmascript-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ 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 });

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 @@ -238,6 +245,13 @@ 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 });

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 8c14040

Please sign in to comment.