2017-09-18 1 views
0

Ich habe Arbeitscode mit verschachtelten ternären Ausdrücken. Gibt es eine Möglichkeit, es mit Hilfe von ramda.js oder einem anderen funktionalen Helfer sauberer zu machen? Wird cond eine gute Wahl sein?Swap verschachtelte ternäre Ausdrücke zu cond mit ramda.js

Ich bin neu zu ramda und ich weiß nicht genau, wie dieses Stück Code zu ramda Weg konvertieren.

const enhance: React$HOC<*, InitialProps> = compose(
     withProps(props => ({ 
     iconColor: props.isPriority ? (props.isCompleted ? variables.color.lightpurple : variables.color.purple) : variables.color.gray3, 
     iconName: props.isPriority ? 'star-full' : 'star-empty', 
     })) 
    ) 

Umwandlung genau die wahnsinnig lange Linie:

iconColor: props.isPriority ? (props.isCompleted ? variables.color.lightpurple : variables.color.purple) : variables.color.gray3, 

würde als enougth mehr sein.

Antwort

0

R.cond ist definitiv eine Option zum Entfernen der Verschachtelung. Dies könnte auch mit R.applySpec kombiniert werden, um ein Objekt zu erzeugen, dessen Werte alle von demselben Argument abgeleitet sind (props in Ihrer Instanz).

const enhance: React$HOC<*, InitialProps> = compose(withProps(R.applySpec({ 
    iconColor: R.cond([ 
     [({ isPriority }) => !isPriority,() => variables.color.gray3], 
     [({ isCompleted }) => isCompleted,() => variables.color.lightpurple], 
     [() => true,() => variables.color.purple] 
    ]), 
    iconName: ({ isPriority }) => isPriority ? 'star-full' : 'star-empty' 
}))) 
Verwandte Themen