-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
assert: add support for Set and Map #2315
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,6 +185,36 @@ function isArguments(object) { | |
return Object.prototype.toString.call(object) == '[object Arguments]'; | ||
} | ||
|
||
function compareMaps(a, b, strict) { | ||
// check size of the maps | ||
if (a.size !== b.size) | ||
return false; | ||
|
||
// iterate over all keys and check if they exist in the other | ||
for (let kvs of a) { | ||
const key = kvs[0]; | ||
const val = kvs[1]; | ||
if (!b.has(key)) { | ||
return false; | ||
} | ||
|
||
if (!_deepEqual(val, b.get(key), strict)) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function compareSets(a, b) { | ||
if (a.size !== b.size) | ||
return false; | ||
|
||
for (let item of a) { | ||
if (!b.has(item)) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
function objEquiv(a, b, strict) { | ||
if (a === null || a === undefined || b === null || b === undefined) | ||
return false; | ||
|
@@ -202,6 +232,13 @@ function objEquiv(a, b, strict) { | |
b = pSlice.call(b); | ||
return _deepEqual(a, b, strict); | ||
} | ||
const aStr = Object.prototype.toString.call(a); | ||
const bStr = Object.prototype.toString.call(b); | ||
if (aStr === '[object Map]' && bStr === '[object Map]') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We does not support WeakMap and WeakSet ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, not yet. Was going to knock that out next. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do plan on supporting them ? They do not expose iterators. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we cannot measure the equality of WeakMap and WeakSet... |
||
return compareMaps(a, b, strict); | ||
} else if (aStr === '[object Set]' && bStr === '[object Set]') { | ||
return compareSets(a, b); | ||
} | ||
var ka = Object.keys(a), | ||
kb = Object.keys(b), | ||
key, i; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let
is not faster thanvar
, if this code is hotcode, we would be better to usevar
instead oflet
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I wouldn't consider this to be hot code, but probably should stick with var for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is not faster, but performs equally in this kind of situation: http://jsperf.com/let-vs-var-iteration
I believe the optimization arrived with V8 4.4.