@@ -15,14 +15,24 @@ function isObject(obj: any): obj is Record<string, any> {
1515 return obj && typeof obj === 'object' && ! Array . isArray ( obj ) ;
1616}
1717
18- export function diffObjects < T extends Record < string , any > > ( obj1 : T , obj2 : T ) : Partial < DiffMap < T > > {
18+ function isPathExcluded ( path : string , excludedPaths : string [ ] ) : boolean {
19+ return excludedPaths . includes ( path ) ;
20+ }
21+
22+ export function diffObjects < T extends Record < string , any > > ( obj1 : T , obj2 : T , excludedPaths : string [ ] = [ ] , currentPath : string = "" ) : Partial < DiffMap < T > > {
1923 const result : Partial < DiffMap < T > > = { } ;
2024
2125 for ( const key in obj1 ) {
2226 if ( obj1 . hasOwnProperty ( key ) ) {
27+ const keyPath = currentPath ? `${ currentPath } .${ key } ` : key ;
28+
29+ if ( isPathExcluded ( keyPath , excludedPaths ) ) {
30+ continue ; // Skip excluded paths
31+ }
32+
2333 if ( obj2 . hasOwnProperty ( key ) ) {
2434 if ( isObject ( obj1 [ key ] ) && isObject ( obj2 [ key ] ) ) {
25- result [ key ] = diffObjects ( obj1 [ key ] , obj2 [ key ] ) ;
35+ result [ key ] = diffObjects ( obj1 [ key ] , obj2 [ key ] , excludedPaths , keyPath ) ;
2636 } else if ( obj1 [ key ] === obj2 [ key ] ) {
2737 // result[key] = obj1[key] as any;
2838 } else {
@@ -36,6 +46,12 @@ export function diffObjects<T extends Record<string, any>>(obj1: T, obj2: T): Pa
3646
3747 for ( const key in obj2 ) {
3848 if ( obj2 . hasOwnProperty ( key ) && ! obj1 . hasOwnProperty ( key ) ) {
49+ const keyPath = currentPath ? `${ currentPath } .${ key } ` : key ;
50+
51+ if ( isPathExcluded ( keyPath , excludedPaths ) ) {
52+ continue ; // Skip excluded paths
53+ }
54+
3955 result [ key ] = { pull : obj2 [ key ] } as any ;
4056 }
4157 }
0 commit comments