Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch fake collections throwing #100

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading