繁体   English   中英

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

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

我有使用嵌套三元表达式的工作代码。 有什么方法可以使用ramda.js或其他功能助手来使其更清洁? cond将是一个不错的选择吗?

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

只是转换那疯狂的长行:

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

会比enougth还要多。

R.cond绝对是删除嵌套的一种选择。 也可以将其与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