@@ -4,39 +4,45 @@ import defaults from 'lodash/defaults'
4
4
import map from 'lodash/map'
5
5
import get from 'lodash/get'
6
6
7
- function resolveFunctionKeys ( object ) {
8
- const getKey = ( key , defaultValue ) => get ( object , key , defaultValue )
9
-
10
- return Object . keys ( object ) . reduce ( ( resolved , key ) => {
11
- return {
12
- ...resolved ,
13
- [ key ] : isFunction ( object [ key ] ) ? object [ key ] ( getKey ) : object [ key ] ,
14
- }
15
- } , { } )
7
+ function resolveConfig ( configs ) {
8
+ return defaults (
9
+ {
10
+ theme : resolveFunctionKeys ( mergeExtensions ( defaults ( ...map ( configs , 'theme' ) ) ) ) ,
11
+ variants : defaults ( ...map ( configs , 'variants' ) ) ,
12
+ } ,
13
+ ...configs
14
+ )
16
15
}
17
16
18
17
function mergeExtensions ( { extend, ...theme } ) {
19
- return mergeWith ( { } , theme , extend , ( _ , extensions , key ) => {
20
- if ( isFunction ( theme [ key ] ) || ( extend && isFunction ( extend [ key ] ) ) ) {
21
- return mergedTheme => ( {
22
- ...( isFunction ( theme [ key ] ) ? theme [ key ] ( mergedTheme ) : theme [ key ] ) ,
23
- ...( extend && isFunction ( extend [ key ] ) ? extend [ key ] ( mergedTheme ) : extensions ) ,
24
- } )
25
- } else {
18
+ return mergeWith ( theme , extend , ( themeValue , extensions , key ) => {
19
+ if ( ! isFunction ( themeValue ) && ! ( isFunction ( extensions ) ) ) {
26
20
return {
27
- ...theme [ key ] ,
21
+ ...themeValue ,
28
22
...extensions ,
29
23
}
30
24
}
25
+
26
+ return resolveThemePath => ( {
27
+ ...value ( themeValue , resolveThemePath ) ,
28
+ ...value ( extensions , resolveThemePath ) ,
29
+ } )
31
30
} )
32
31
}
33
32
34
- export default function ( configs ) {
35
- return defaults (
36
- {
37
- theme : resolveFunctionKeys ( mergeExtensions ( defaults ( ...map ( configs , 'theme' ) ) ) ) ,
38
- variants : defaults ( ...map ( configs , 'variants' ) ) ,
39
- } ,
40
- ...configs
41
- )
33
+ function resolveFunctionKeys ( object ) {
34
+ const resolveObjectPath = ( key , defaultValue ) => get ( object , key , defaultValue )
35
+
36
+ return Object . keys ( object ) . reduce ( ( resolved , key ) => {
37
+ return {
38
+ ...resolved ,
39
+ [ key ] : isFunction ( object [ key ] ) ? object [ key ] ( resolveObjectPath ) : object [ key ] ,
40
+ }
41
+ } , { } )
42
+ }
43
+
44
+ function value ( valueToResolve , ...args ) {
45
+ return isFunction ( valueToResolve ) ? valueToResolve ( ...args ) : valueToResolve
42
46
}
47
+
48
+ export default resolveConfig
0 commit comments