@@ -43,7 +43,39 @@ const fieldCondition = (value, config) => {
4343 return config . notMatch ? ! isMatched : isMatched ;
4444} ;
4545
46- export const parseCondition = ( condition , values , field ) => {
46+ const allowedMappedAttributes = [ 'when' , 'is' ] ;
47+
48+ export const unpackMappedCondition = ( condition , conditionMapper ) => {
49+ if ( typeof condition . mappedAttributes !== 'object' ) {
50+ return condition ;
51+ }
52+
53+ const { mappedAttributes } = condition ;
54+
55+ const internalCondition = {
56+ ...condition ,
57+ mappedAttributes : undefined ,
58+ } ;
59+
60+ Object . entries ( mappedAttributes ) . forEach ( ( [ key , value ] ) => {
61+ if ( ! allowedMappedAttributes . includes ( key ) ) {
62+ console . error ( `Mapped condition attribute ${ key } is not allowed! Allowed attributes are: ${ allowedMappedAttributes . join ( ', ' ) } ` ) ;
63+ return ;
64+ }
65+
66+ if ( conditionMapper [ value ?. [ 0 ] ] ) {
67+ const [ fnName , ...args ] = value ;
68+ const fn = conditionMapper [ fnName ] ;
69+ internalCondition [ key ] = fn ( ...args ) ;
70+ } else {
71+ console . error ( `Missing conditionMapper entry for ${ value } !` ) ;
72+ }
73+ } ) ;
74+
75+ return internalCondition ;
76+ } ;
77+
78+ export const parseCondition = ( condition , values , field , conditionMapper = { } ) => {
4779 let positiveResult = {
4880 visible : true ,
4981 ...condition . then ,
@@ -62,14 +94,16 @@ export const parseCondition = (condition, values, field) => {
6294 : negativeResult ;
6395 }
6496
65- if ( condition . and ) {
66- return ! condition . and . map ( ( condition ) => parseCondition ( condition , values , field ) ) . some ( ( { result } ) => result === false )
97+ const conditionInternal = unpackMappedCondition ( condition , conditionMapper ) ;
98+
99+ if ( conditionInternal . and ) {
100+ return ! conditionInternal . and . map ( ( condition ) => parseCondition ( condition , values , field ) ) . some ( ( { result } ) => result === false )
67101 ? positiveResult
68102 : negativeResult ;
69103 }
70104
71- if ( condition . sequence ) {
72- return condition . sequence . reduce (
105+ if ( conditionInternal . sequence ) {
106+ return conditionInternal . sequence . reduce (
73107 ( acc , curr ) => {
74108 const result = parseCondition ( curr , values , field ) ;
75109
@@ -83,25 +117,25 @@ export const parseCondition = (condition, values, field) => {
83117 ) ;
84118 }
85119
86- if ( condition . or ) {
87- return condition . or . map ( ( condition ) => parseCondition ( condition , values , field ) ) . some ( ( { result } ) => result === true )
120+ if ( conditionInternal . or ) {
121+ return conditionInternal . or . map ( ( condition ) => parseCondition ( condition , values , field ) ) . some ( ( { result } ) => result === true )
88122 ? positiveResult
89123 : negativeResult ;
90124 }
91125
92- if ( condition . not ) {
93- return ! parseCondition ( condition . not , values , field ) . result ? positiveResult : negativeResult ;
126+ if ( conditionInternal . not ) {
127+ return ! parseCondition ( conditionInternal . not , values , field ) . result ? positiveResult : negativeResult ;
94128 }
95129
96- const finalWhen = typeof condition . when === 'function' ? condition . when ( field ) : condition . when ;
130+ const finalWhen = typeof conditionInternal . when === 'function' ? conditionInternal . when ( field ) : conditionInternal . when ;
97131
98132 if ( typeof finalWhen === 'string' ) {
99- return fieldCondition ( get ( values , finalWhen ) , condition ) ? positiveResult : negativeResult ;
133+ return fieldCondition ( get ( values , finalWhen ) , conditionInternal ) ? positiveResult : negativeResult ;
100134 }
101135
102136 if ( Array . isArray ( finalWhen ) ) {
103137 return finalWhen
104- . map ( ( fieldName ) => fieldCondition ( get ( values , typeof fieldName === 'function' ? fieldName ( field ) : fieldName ) , condition ) )
138+ . map ( ( fieldName ) => fieldCondition ( get ( values , typeof fieldName === 'function' ? fieldName ( field ) : fieldName ) , conditionInternal ) )
105139 . find ( ( condition ) => ! ! condition )
106140 ? positiveResult
107141 : negativeResult ;
0 commit comments