@@ -15,14 +15,24 @@ function isObject(obj: any): obj is Record<string, any> {
15
15
return obj && typeof obj === 'object' && ! Array . isArray ( obj ) ;
16
16
}
17
17
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 > > {
19
23
const result : Partial < DiffMap < T > > = { } ;
20
24
21
25
for ( const key in obj1 ) {
22
26
if ( obj1 . hasOwnProperty ( key ) ) {
27
+ const keyPath = currentPath ? `${ currentPath } .${ key } ` : key ;
28
+
29
+ if ( isPathExcluded ( keyPath , excludedPaths ) ) {
30
+ continue ; // Skip excluded paths
31
+ }
32
+
23
33
if ( obj2 . hasOwnProperty ( key ) ) {
24
34
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 ) ;
26
36
} else if ( obj1 [ key ] === obj2 [ key ] ) {
27
37
// result[key] = obj1[key] as any;
28
38
} else {
@@ -36,6 +46,12 @@ export function diffObjects<T extends Record<string, any>>(obj1: T, obj2: T): Pa
36
46
37
47
for ( const key in obj2 ) {
38
48
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
+
39
55
result [ key ] = { pull : obj2 [ key ] } as any ;
40
56
}
41
57
}
0 commit comments