23
23
const {
24
24
ArrayBufferIsView,
25
25
ArrayBufferPrototypeGetByteLength,
26
- ArrayFrom,
27
26
ArrayIsArray,
28
27
ArrayPrototypeIndexOf,
29
28
ArrayPrototypeJoin,
@@ -395,12 +394,11 @@ function partiallyCompareMaps(actual, expected, comparedObjects) {
395
394
const expectedIterator = FunctionPrototypeCall ( SafeMap . prototype [ SymbolIterator ] , expected ) ;
396
395
397
396
for ( const { 0 : key , 1 : expectedValue } of expectedIterator ) {
398
- if ( ! MapPrototypeHas ( actual , key ) ) {
397
+ const actualValue = MapPrototypeGet ( actual , key ) ;
398
+ if ( actualValue === undefined && ! MapPrototypeHas ( actual , key ) ) {
399
399
return false ;
400
400
}
401
401
402
- const actualValue = MapPrototypeGet ( actual , key ) ;
403
-
404
402
if ( ! compareBranch ( actualValue , expectedValue , comparedObjects ) ) {
405
403
return false ;
406
404
}
@@ -481,18 +479,32 @@ function partiallyCompareSets(actual, expected, comparedObjects) {
481
479
482
480
if ( isDeepEqual === undefined ) lazyLoadComparison ( ) ;
483
481
484
- const actualArray = ArrayFrom ( FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ) ;
482
+ const actualSet = new SafeSet ( FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , actual ) ) ;
485
483
const expectedIterator = FunctionPrototypeCall ( SafeSet . prototype [ SymbolIterator ] , expected ) ;
486
- const usedIndices = new SafeSet ( ) ;
487
484
488
- expectedIteration: for ( const expectedItem of expectedIterator ) {
489
- for ( let actualIdx = 0 ; actualIdx < actualArray . length ; actualIdx ++ ) {
490
- if ( ! usedIndices . has ( actualIdx ) && isDeepStrictEqual ( actualArray [ actualIdx ] , expectedItem ) ) {
491
- usedIndices . add ( actualIdx ) ;
492
- continue expectedIteration;
485
+ for ( const expectedItem of expectedIterator ) {
486
+ let foundMatch = false ;
487
+
488
+ // Check for primitives first to avoid useless loops
489
+ if ( isPrimitive ( expectedItem ) ) {
490
+ if ( actualSet . has ( expectedItem ) ) {
491
+ actualSet . delete ( expectedItem ) ;
492
+ foundMatch = true ;
493
+ }
494
+ } else {
495
+ // Check for non-primitives
496
+ for ( const actualItem of actualSet ) {
497
+ if ( isDeepStrictEqual ( actualItem , expectedItem ) ) {
498
+ actualSet . delete ( actualItem ) ;
499
+ foundMatch = true ;
500
+ break ;
501
+ }
493
502
}
494
503
}
495
- return false ;
504
+
505
+ if ( ! foundMatch ) {
506
+ return false ;
507
+ }
496
508
}
497
509
498
510
return true ;
@@ -518,13 +530,11 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
518
530
519
531
// Create a map to count occurrences of each element in the expected array
520
532
const expectedCounts = new SafeMap ( ) ;
521
- const safeExpected = new SafeArrayIterator ( expected ) ;
522
533
523
- for ( const expectedItem of safeExpected ) {
524
- // Check if the item is a zero or a -0, as these need to be handled separately
534
+ for ( const expectedItem of new SafeArrayIterator ( expected ) ) {
525
535
if ( expectedItem === 0 ) {
526
536
const zeroKey = getZeroKey ( expectedItem ) ;
527
- expectedCounts . set ( zeroKey , ( expectedCounts . get ( zeroKey ) ?. count || 0 ) + 1 ) ;
537
+ expectedCounts . set ( zeroKey , ( expectedCounts . get ( zeroKey ) ?? 0 ) + 1 ) ;
528
538
} else {
529
539
let found = false ;
530
540
for ( const { 0 : key , 1 : count } of expectedCounts ) {
@@ -540,10 +550,7 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
540
550
}
541
551
}
542
552
543
- const safeActual = new SafeArrayIterator ( actual ) ;
544
-
545
- for ( const actualItem of safeActual ) {
546
- // Check if the item is a zero or a -0, as these need to be handled separately
553
+ for ( const actualItem of new SafeArrayIterator ( actual ) ) {
547
554
if ( actualItem === 0 ) {
548
555
const zeroKey = getZeroKey ( actualItem ) ;
549
556
@@ -723,6 +730,10 @@ function compareExceptionKey(actual, expected, key, message, keys, fn) {
723
730
}
724
731
}
725
732
733
+ function isPrimitive ( value ) {
734
+ return typeof value !== 'object' || value === null ;
735
+ }
736
+
726
737
function expectedException ( actual , expected , message , fn ) {
727
738
let generatedMessage = false ;
728
739
let throwError = false ;
@@ -741,7 +752,7 @@ function expectedException(actual, expected, message, fn) {
741
752
}
742
753
throwError = true ;
743
754
// Handle primitives properly.
744
- } else if ( typeof actual !== 'object' || actual === null ) {
755
+ } else if ( isPrimitive ( actual ) ) {
745
756
const err = new AssertionError ( {
746
757
actual,
747
758
expected,
0 commit comments