@@ -2,54 +2,29 @@ import { GlobalMountOptions } from './types'
2
2
3
3
const isString = ( val : unknown ) : val is string => typeof val === 'string'
4
4
5
- function deepMerge ( ...objects : object [ ] ) {
6
- const isObject = ( obj : any ) => obj && typeof obj === 'object'
7
-
8
- function deepMergeInner ( target : object , source : object ) {
9
- Object . keys ( source ) . forEach ( ( key : string ) => {
10
- const targetValue = target [ key ]
11
- const sourceValue = source [ key ]
12
-
13
- if ( Array . isArray ( targetValue ) && Array . isArray ( sourceValue ) ) {
14
- target [ key ] = targetValue . concat ( sourceValue )
15
- } else if ( isObject ( targetValue ) && isObject ( sourceValue ) ) {
16
- target [ key ] = deepMergeInner (
17
- Object . assign ( { } , targetValue ) ,
18
- sourceValue
19
- )
20
- } else {
21
- target [ key ] = sourceValue
5
+ // Deep merge function, adapted from from https://gist.github.com/ahtcx/0cd94e62691f539160b32ecda18af3d6
6
+ // Merge a `source` object to a `target` recursively
7
+ const merge = ( target : object , source : object ) => {
8
+ // Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties
9
+ for ( const key of Object . keys ( source ) ) {
10
+ if ( ! target [ key ] ) {
11
+ target [ key ] = source [ key ]
12
+ } else {
13
+ if ( source [ key ] instanceof Object ) {
14
+ Object . assign ( source [ key ] , merge ( target [ key ] , source [ key ] ) )
22
15
}
23
- } )
24
-
25
- return target
26
- }
27
-
28
- if ( objects . length < 2 ) {
29
- throw new Error (
30
- 'deepMerge: this function expects at least 2 objects to be provided'
31
- )
32
- }
33
-
34
- if ( objects . some ( ( object ) => ! isObject ( object ) ) ) {
35
- throw new Error ( 'deepMerge: all values should be of type "object"' )
36
- }
37
-
38
- const target = objects . shift ( )
39
- let source : object
40
-
41
- while ( ( source = objects . shift ( ) ) ) {
42
- deepMergeInner ( target , source )
16
+ }
43
17
}
44
18
45
- return target
19
+ Object . assign ( target || { } , source )
46
20
}
47
21
48
22
function mergeGlobalProperties (
49
23
configGlobal : GlobalMountOptions = { } ,
50
24
mountGlobal : GlobalMountOptions = { }
51
25
) : GlobalMountOptions {
52
- return deepMerge ( configGlobal , mountGlobal )
26
+ merge ( configGlobal , mountGlobal )
27
+ return configGlobal
53
28
}
54
29
55
30
export { isString , mergeGlobalProperties }
0 commit comments