@@ -4,39 +4,45 @@ import defaults from 'lodash/defaults'
44import map from 'lodash/map'
55import get from 'lodash/get'
66
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+ )
1615}
1716
1817function 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 ) ) ) {
2620 return {
27- ...theme [ key ] ,
21+ ...themeValue ,
2822 ...extensions ,
2923 }
3024 }
25+
26+ return resolveThemePath => ( {
27+ ...value ( themeValue , resolveThemePath ) ,
28+ ...value ( extensions , resolveThemePath ) ,
29+ } )
3130 } )
3231}
3332
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
4246}
47+
48+ export default resolveConfig
0 commit comments