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