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

Fixes equality checking for Map objects. #52

Merged
merged 2 commits into from Dec 1, 2019

Conversation

@C-Saunders
Copy link

C-Saunders commented Sep 20, 2017

Currently:

// keys are not checked
const equal = require('./index.js')
const a = new Map([['a', 1]])
const b = new Map([['b', 1]])
equal(a, b) // true
// recursive checking for values doesn't work, either
const x = new Map([['a', [1, 2]]])
const y = new Map([['a', [2, 1]]])
equal(x, y) // true
// empty maps equal those with data b/c the key set is empty and the check falls all the way though the ifs
const z = new Map()
equal(a, z) // true

While Maps don't have good browser support at this time, they are useful in Node, and I've found that I want tape's deepEqual, deepLooseEqual, and friends to work properly with Maps when testing Node applications.

The implementation is a near duplication of the Object handling code, but deduplicating that didn't seem straightforward or all that beneficial to me at the time.

I also made a few small changes to make the style more consistent, and added a .gitignore, but tried to not overreach or get too out of scope. I can revert these changes upon request.

@C-Saunders

This comment has been minimized.

Copy link
Author

C-Saunders commented Sep 22, 2017

Updated description to include more details of the issue this addresses.

Updated the Travis config so I could get a nice green checkmark.

@Tyler-Murphy

This comment has been minimized.

Copy link

Tyler-Murphy commented Jun 15, 2018

I'd also like to see this feature. Can anyone review it?

@ljharb ljharb force-pushed the C-Saunders:map_value_equality branch from 20523d1 to 06a483c Jul 31, 2019
index.js Outdated
@@ -122,6 +127,24 @@ function objEquiv(a, b, opts) {
}
}

// Maps
if (typeof Map && a.constructor === Map && b.constructor === Map) {

This comment has been minimized.

Copy link
@ljharb

ljharb Jul 31, 2019

Member

This check is not robust across realms; it will need a brand check.

index.js Outdated
@@ -122,6 +127,24 @@ function objEquiv(a, b, opts) {
}
}

// Maps
if (typeof Map && a.constructor === Map && b.constructor === Map) {
ka = Array.from(a.keys());

This comment has been minimized.

Copy link
@ljharb

ljharb Jul 31, 2019

Member

Array.from is not necessarily available; we'll need to use a package or manually implement a while loop to call .next() on the iterator. However, not all Map implementations have keys either, so it's actually a pretty hard problem to robustly iterate Maps and Sets.

…ssert

Fixes #54. Fixes #46.
@ljharb ljharb force-pushed the inspect-js:master branch 2 times, most recently from 191fb2f to ecd15ae Dec 1, 2019
@ljharb ljharb force-pushed the C-Saunders:map_value_equality branch 2 times, most recently from 2a4c42d to 47eb288 Dec 1, 2019
@ljharb
ljharb approved these changes Dec 1, 2019
Copy link
Member

ljharb left a comment

updated this with a robust solution

@ljharb ljharb merged commit 20877c4 into inspect-js:master Dec 1, 2019
1 check passed
1 check passed
Travis CI - Pull Request Build Passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.