1
- import isString from 'lodash/isString'
2
1
import mergeWith from 'lodash/mergeWith'
3
2
4
3
import { GlobalMountOptions } from './types'
5
4
5
+ const isString = ( val : unknown ) : val is string => typeof val === 'string'
6
+
7
+ function deepMerge ( ...objects : object [ ] ) {
8
+ const isObject = ( obj : any ) => obj && typeof obj === 'object'
9
+
10
+ function deepMergeInner ( target : object , source : object ) {
11
+ Object . keys ( source ) . forEach ( ( key : string ) => {
12
+ const targetValue = target [ key ]
13
+ const sourceValue = source [ key ]
14
+
15
+ if ( Array . isArray ( targetValue ) && Array . isArray ( sourceValue ) ) {
16
+ target [ key ] = targetValue . concat ( sourceValue )
17
+ } else if ( isObject ( targetValue ) && isObject ( sourceValue ) ) {
18
+ target [ key ] = deepMergeInner (
19
+ Object . assign ( { } , targetValue ) ,
20
+ sourceValue
21
+ )
22
+ } else {
23
+ target [ key ] = sourceValue
24
+ }
25
+ } )
26
+
27
+ return target
28
+ }
29
+
30
+ if ( objects . length < 2 ) {
31
+ throw new Error (
32
+ 'deepMerge: this function expects at least 2 objects to be provided'
33
+ )
34
+ }
35
+
36
+ if ( objects . some ( ( object ) => ! isObject ( object ) ) ) {
37
+ throw new Error ( 'deepMerge: all values should be of type "object"' )
38
+ }
39
+
40
+ const target = objects . shift ( )
41
+ let source : object
42
+
43
+ while ( ( source = objects . shift ( ) ) ) {
44
+ deepMergeInner ( target , source )
45
+ }
46
+
47
+ return target
48
+ }
49
+
6
50
function mergeGlobalProperties (
7
51
configGlobal : GlobalMountOptions = { } ,
8
52
mountGlobal : GlobalMountOptions = { }
9
53
) : GlobalMountOptions {
54
+ // const merged: GlobalMountOptions = deepMerge(configGlobal, mountGlobal)
55
+ // merged.components = {
56
+ // ...mountGlobal.components,
57
+ // ...configGlobal.components
58
+ // }
59
+ // console.log(merged)
60
+ // return merged
10
61
return mergeWith (
11
62
{ } ,
12
63
configGlobal ,
@@ -24,6 +75,7 @@ function mergeGlobalProperties(
24
75
}
25
76
}
26
77
)
78
+ // return mountGlobal
27
79
}
28
80
29
81
export { isString , mergeGlobalProperties }
0 commit comments