[Ramda] Rewrite if..else with Ramda ifElse

From:

const onSeachClick = (searchTerm) => {
   if(searchTerm !== '') {
      searchForMovies(searchTerm)
   } else {
       console.log('a search term should be provided')
   }
}

 

To:

// Utils

const inNotEmpty = R.compose(
   R.not, 
   R.isEmpty
);

const onSearchClick = () => {
  R.ifElse(
     isNotEmpty,  // logic to check
     searchForMovices, // do it if true
     log('a search term should be provided') // do it if false
  )
}    

 

Example2:

/*
    Example2:
*/
 function processSearchResponse(response) {
     // check total_results prop from response,
     // it shuold greater than 0
     const searchHasResult = R.compose(
         R.lt(0),
         R.prop('total_results')
     );
     // get results props from response,
     // then createMoviesElements called 
     const createElementFromResults = R.compose(
         createMovicesElements,
         R.prop('results')
     );
     //always return empty 
     const createArrayWithNotFound = R.always([
         createMoviceNotFoundElement({})
     ]);

     const elements = R.ifElse(
         searchHasResult,
         createElementFromResults,
         createArrayWithNotFound
     )(response);
 }  

 

转载于:https://www.cnblogs.com/Answer1215/p/6464196.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值