11type searchObj = any | object | Array < any > ;
22type objPair = { obj1 :searchObj , obj2 : searchObj } ;
3- function testValue ( value1 , value2 ) {
3+ type Primitive = symbol | boolean | string | number | null | undefined ;
4+ function testValue ( value1 :Primitive , value2 :Primitive ) :boolean {
45 if ( ( typeof value1 ) !== ( typeof value2 ) )
56 return true ;
6- if ( Number . isNaN ( value1 ) !== Number . isNaN ( value2 ) )
7- return true ;
7+ if ( Number . isNaN ( value1 ) || Number . isNaN ( value2 ) )
8+ return Number . isNaN ( value1 ) !== Number . isNaN ( value2 ) ;
89 if ( ( value1 !== value2 ) )
910 return true ;
11+ return false ;
1012}
1113// Returns true if obj1 differs in any way from obj2.
1214export function testDiff ( obj1 :searchObj , obj2 :searchObj , deep :boolean = true ) :boolean {
13- if ( obj1 === null )
14- return obj1 !== obj2 ;
15- // Cheap comparisons first
16- if ( ( ( typeof obj1 ) !== "object" ) && testValue ( obj1 , obj2 ) )
17- return true ;
15+ if ( ( obj1 === null ) || ( obj2 === null ) || ( ( typeof obj1 ) !== "object" ) || ( ( typeof obj2 ) !== "object" ) )
16+ return testValue ( obj1 , obj2 ) ;
1817 const stack :Array < objPair > = [ { obj1 : obj1 , obj2 : obj2 } ] ;
1918 const seen :Map < searchObj , objPair > = new Map ( ) ;
2019 seen . set ( obj1 , stack [ 0 ] ) ;
@@ -38,7 +37,7 @@ export function testDiff(obj1:searchObj, obj2:searchObj, deep:boolean = true):bo
3837 const value2 :any = objects . obj2 [ prop ] ;
3938 if ( ( typeof value1 ) !== ( typeof value2 ) )
4039 return true ;
41- if ( value1 === null || ( typeof value1 ) !== "object" ) {
40+ if ( value1 === null || value2 === null || ( ( typeof value1 ) !== "object" ) || ( ( typeof value2 ) !== "object" ) ) {
4241 if ( testValue ( value1 , value2 ) )
4342 return true ;
4443 continue _props;
0 commit comments