@@ -4,12 +4,7 @@ export type UserIntegrationsFunction = (integrations: Integration[]) => Integrat
4
4
export type UserIntegrations = Integration [ ] | UserIntegrationsFunction ;
5
5
6
6
type ForcedIntegrationOptions = {
7
- [ integrationName : string ] :
8
- | {
9
- keyPath : string ;
10
- value : unknown ;
11
- }
12
- | undefined ;
7
+ [ keyPath : string ] : unknown ;
13
8
} ;
14
9
15
10
/**
@@ -30,7 +25,8 @@ function setNestedKey(obj: Record<string, any>, keyPath: string, value: unknown)
30
25
obj [ keyPath ] = value ;
31
26
} else {
32
27
// `match[1]` is the initial segment of the path, and `match[2]` is the remainder of the path
33
- setNestedKey ( obj [ match [ 1 ] ] , match [ 2 ] , value ) ;
28
+ const innerObj = obj [ match [ 1 ] ] ;
29
+ setNestedKey ( innerObj , match [ 2 ] , value ) ;
34
30
}
35
31
}
36
32
@@ -52,34 +48,26 @@ export function addOrUpdateIntegration(
52
48
userIntegrations : UserIntegrations ,
53
49
forcedOptions : ForcedIntegrationOptions = { } ,
54
50
) : UserIntegrations {
55
- if ( Array . isArray ( userIntegrations ) ) {
56
- return addOrUpdateIntegrationInArray ( defaultIntegrationInstance , userIntegrations , forcedOptions ) ;
57
- } else {
58
- return addOrUpdateIntegrationInFunction ( defaultIntegrationInstance , userIntegrations , forcedOptions ) ;
59
- }
51
+ return Array . isArray ( userIntegrations )
52
+ ? addOrUpdateIntegrationInArray ( defaultIntegrationInstance , userIntegrations , forcedOptions )
53
+ : addOrUpdateIntegrationInFunction ( defaultIntegrationInstance , userIntegrations , forcedOptions ) ;
60
54
}
61
55
62
56
function addOrUpdateIntegrationInArray (
63
57
defaultIntegrationInstance : Integration ,
64
58
userIntegrations : Integration [ ] ,
65
59
forcedOptions : ForcedIntegrationOptions ,
66
60
) : Integration [ ] {
67
- let includesName = false ;
68
- // eslint-disable-next-line @typescript-eslint/prefer-for-of
69
- for ( let x = 0 ; x < userIntegrations . length ; x ++ ) {
70
- if ( userIntegrations [ x ] . name === defaultIntegrationInstance . name ) {
71
- includesName = true ;
72
- }
61
+ const userInstance = userIntegrations . find ( integration => integration . name === defaultIntegrationInstance . name ) ;
73
62
74
- const optionToSet = forcedOptions [ userIntegrations [ x ] . name ] ;
75
- if ( optionToSet ) {
76
- setNestedKey ( userIntegrations [ x ] , optionToSet . keyPath , optionToSet . value ) ;
63
+ if ( userInstance ) {
64
+ for ( const [ keyPath , value ] of Object . entries ( forcedOptions ) ) {
65
+ setNestedKey ( userInstance , keyPath , value ) ;
77
66
}
78
- }
79
67
80
- if ( includesName ) {
81
68
return userIntegrations ;
82
69
}
70
+
83
71
return [ ...userIntegrations , defaultIntegrationInstance ] ;
84
72
}
85
73
0 commit comments