简体   繁体   English

交换嵌套三元表达式以使用ramda.js进行转换

[英]Swap nested ternary expressions to cond using ramda.js

I have working code using nested ternary expressions. 我有使用嵌套三元表达式的工作代码。 Is there any way to make it cleaner using ramda.js or another functional helper? 有什么方法可以使用ramda.js或其他功能助手来使其更清洁? Will cond be a good choice? cond将是一个不错的选择吗?

I'm new to ramda and I don't know exactly how to convert that piece of code to ramda way. 我是ramda的新手,我不知道如何将这段代码转换为ramda方式。

  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',
      }))
    )

converting just that insane long line: 只是转换那疯狂的长行:

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

would be more than enougth. 会比enougth还要多。

R.cond is definitely one option for removing the nesting. R.cond绝对是删除嵌套的一种选择。 This could also be combined with R.applySpec to produce an object whose values are all derived from the same argument ( props in your instance). 也可以将其与R.applySpec组合以生成一个对象,该对象的值均从同一参数(实例中的props )派生。

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'
})))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM