@@ -497,6 +497,15 @@ function partiallyCompareSets(actual, expected, comparedObjects) {
497
497
return true ;
498
498
}
499
499
500
+ // Helper function to get a unique key for 0, -0 to avoid collisions
501
+ function getZeroKey ( item ) {
502
+ if ( item === 0 ) {
503
+ if ( ObjectIs ( item , - 0 ) ) return '-0' ;
504
+ return '0' ;
505
+ }
506
+ return item ;
507
+ }
508
+
500
509
function partiallyCompareArrays ( actual , expected , comparedObjects ) {
501
510
if ( expected . length > actual . length ) {
502
511
return false ;
@@ -507,38 +516,63 @@ function partiallyCompareArrays(actual, expected, comparedObjects) {
507
516
// Create a map to count occurrences of each element in the expected array
508
517
const expectedCounts = new SafeMap ( ) ;
509
518
for ( const expectedItem of expected ) {
510
- let found = false ;
511
- for ( const { 0 : key , 1 : count } of expectedCounts ) {
512
- if ( isDeepStrictEqual ( key , expectedItem ) ) {
513
- expectedCounts . set ( key , count + 1 ) ;
514
- found = true ;
515
- break ;
519
+ // Check if the item is a zero or a -0, as these need to be handled separately
520
+ if ( expectedItem === 0 ) {
521
+ const zeroKey = getZeroKey ( expectedItem ) ;
522
+ expectedCounts . set ( zeroKey , {
523
+ count : ( expectedCounts . get ( zeroKey ) ?. count || 0 ) + 1 ,
524
+ expectedType : typeof expectedItem ,
525
+ } ) ;
526
+ } else {
527
+ let found = false ;
528
+ for ( const { 0 : key , 1 : { count, expectedType } } of expectedCounts ) {
529
+ // eslint-disable-next-line valid-typeof
530
+ if ( isDeepStrictEqual ( key , expectedItem ) && expectedType === typeof expectedItem ) {
531
+ expectedCounts . set ( key , { count : count + 1 , expectedType } ) ;
532
+ found = true ;
533
+ break ;
534
+ }
535
+ }
536
+ if ( ! found ) {
537
+ expectedCounts . set ( expectedItem , { count : 1 , expectedType : typeof expectedItem } ) ;
516
538
}
517
- }
518
- if ( ! found ) {
519
- expectedCounts . set ( expectedItem , 1 ) ;
520
539
}
521
540
}
522
541
523
542
const safeActual = new SafeArrayIterator ( actual ) ;
524
543
525
- // Create a map to count occurrences of relevant elements in the actual array
526
544
for ( const actualItem of safeActual ) {
527
- for ( const { 0 : key , 1 : count } of expectedCounts ) {
528
- if ( isDeepStrictEqual ( key , actualItem ) ) {
529
- if ( count === 1 ) {
530
- expectedCounts . delete ( key ) ;
531
- } else {
532
- expectedCounts . set ( key , count - 1 ) ;
545
+ // Check if the item is a zero or a -0, as these need to be handled separately
546
+ if ( actualItem === 0 ) {
547
+ const zeroKey = getZeroKey ( actualItem ) ;
548
+
549
+ if ( expectedCounts . has ( zeroKey ) ) {
550
+ const { count, expectedType } = expectedCounts . get ( zeroKey ) ;
551
+ // eslint-disable-next-line valid-typeof
552
+ if ( expectedType === typeof actualItem ) {
553
+ if ( count === 1 ) {
554
+ expectedCounts . delete ( zeroKey ) ;
555
+ } else {
556
+ expectedCounts . set ( zeroKey , { count : count - 1 , expectedType } ) ;
557
+ }
558
+ }
559
+ }
560
+ } else {
561
+ for ( const { 0 : expectedItem , 1 : { count, expectedType } } of expectedCounts ) {
562
+ // eslint-disable-next-line valid-typeof
563
+ if ( isDeepStrictEqual ( expectedItem , actualItem ) && expectedType === typeof actualItem ) {
564
+ if ( count === 1 ) {
565
+ expectedCounts . delete ( expectedItem ) ;
566
+ } else {
567
+ expectedCounts . set ( expectedItem , { count : count - 1 , expectedType } ) ;
568
+ }
569
+ break ;
533
570
}
534
- break ;
535
571
}
536
572
}
537
573
}
538
574
539
- const { size } = expectedCounts ;
540
- expectedCounts . clear ( ) ;
541
- return size === 0 ;
575
+ return expectedCounts . size === 0 ;
542
576
}
543
577
544
578
/**
0 commit comments