-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Share more logic between object equality and arrays #11061
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6d3075e
Share more logic between object equality and arrays
dubzzz e63452b
FIX Eslint warnings for no-sparse-arrays
dubzzz 204f779
Merge remote-tracking branch 'upstream/master' into try-share-more-ar…
dubzzz 12c9dbb
Update snapshots following merge of better sparse print
dubzzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,30 +147,19 @@ function eq( | |
// Add the first object to the stack of traversed objects. | ||
aStack.push(a); | ||
bStack.push(b); | ||
var size = 0; | ||
// Recursively compare objects and arrays. | ||
// Compare array lengths to determine if a deep comparison is necessary. | ||
if (className == '[object Array]') { | ||
size = a.length; | ||
if (size !== b.length) { | ||
return false; | ||
} | ||
|
||
while (size--) { | ||
result = eq(a[size], b[size], aStack, bStack, customTesters, hasKey); | ||
if (!result) { | ||
return false; | ||
} | ||
} | ||
if (className == '[object Array]' && a.length !== b.length) { | ||
return false; | ||
} | ||
|
||
// Deep compare objects. | ||
var aKeys = keys(a, className == '[object Array]', hasKey), | ||
var aKeys = keys(a, hasKey), | ||
key; | ||
size = aKeys.length; | ||
var size = aKeys.length; | ||
|
||
// Ensure that both objects contain the same number of properties before comparing deep equality. | ||
if (keys(b, className == '[object Array]', hasKey).length !== size) { | ||
if (keys(b, hasKey).length !== size) { | ||
return false; | ||
} | ||
|
||
|
@@ -193,47 +182,20 @@ function eq( | |
return result; | ||
} | ||
|
||
function keys( | ||
obj: object, | ||
isArray: boolean, | ||
hasKey: (obj: object, key: string) => boolean, | ||
) { | ||
var allKeys = (function(o) { | ||
var keys = []; | ||
for (var key in o) { | ||
if (hasKey(o, key)) { | ||
keys.push(key); | ||
} | ||
function keys(obj: object, hasKey: (obj: object, key: string) => boolean) { | ||
var keys = []; | ||
for (var key in obj) { | ||
if (hasKey(obj, key)) { | ||
keys.push(key); | ||
} | ||
return keys.concat( | ||
(Object.getOwnPropertySymbols(o) as Array<any>).filter( | ||
symbol => | ||
(Object.getOwnPropertyDescriptor(o, symbol) as PropertyDescriptor) | ||
.enumerable, | ||
), | ||
); | ||
})(obj); | ||
|
||
if (!isArray) { | ||
return allKeys; | ||
} | ||
|
||
var extraKeys = []; | ||
if (allKeys.length === 0) { | ||
return allKeys; | ||
} | ||
|
||
for (var x = 0; x < allKeys.length; x++) { | ||
if ( | ||
typeof allKeys[x] === 'symbol' || | ||
!allKeys[x].match(/^[0-9]+$/) || | ||
Number(allKeys[x]) >= 4294967295 | ||
) { | ||
extraKeys.push(allKeys[x]); | ||
} | ||
} | ||
|
||
return extraKeys; | ||
return keys.concat( | ||
(Object.getOwnPropertySymbols(obj) as Array<any>).filter( | ||
symbol => | ||
(Object.getOwnPropertyDescriptor(obj, symbol) as PropertyDescriptor) | ||
.enumerable, | ||
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. thanks prettier 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 blame the people who put enumerable symbol-keyed properties on objects in the first place ^^ |
||
), | ||
); | ||
} | ||
|
||
function hasDefinedKey(obj: any, key: string) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The Expected / Received seems to be wrong there 🤔
Nonetheless it does not seem related to my change as I got the same snapshot before my dev.
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.
wrong how? they reflect the
+
and-
in the diff below, no?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 looks that the diff should be:
(clearly not sure of me on that one)
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.
ah, I see what you mean. You misunderstand (I think 😛) - the heading is a counter of the number of
-
and+
lines, it's not an extracted part of the diff itself