简体   繁体   中英

Ramda - allPass returns true, even if one of the conditions is false

I am using Ramda, to clean up my code. I have some complex Checks I need to do. And I decided instead of && and || to use the allPass and anyPass from ramda.

But I have an issue. AllPass, will return true even if one of the conditions is false, while a regular && returns false.

The first check is false, and the second true. Yet, this weird thing is happening.

// Regulr &&, returns false
  const emptyView =
    R.contains(itemType, [IItemType.ONE, IItemType.TWO]) &&
    R.equals(mode, IViewMode.VIEW);

// Ramda allPass returns true
  const emptyView = R.allPass([
    R.contains(itemType, [IItemType.JSON, IItemType.AVRO]),
    R.equals(mode, IViewMode.VIEW),
  ]);

Can someone explain to me why? I tried both variations of Ramda. A plain Array, and also calling the function, with th necessary info, but no luck. What am I doing wrong?

While the first emptyView is actually of type boolean, the second one is just a function (which in javascript always evaluates to true).

I think that the following is what you really need, a function that can tell you whether the itemType and the mode are correct.

 const IItemType = { JSON: 1, AVRO: 2 }; const IViewMode = { VIEW: 'view', }; const fn = R.useWith(R.and, [ R.contains(R.__, [IItemType.JSON, IItemType.AVRO]), R.equals(IViewMode.VIEW), ]); console.log( 'it should be true =>', fn(1, 'view'), ); console.log( 'it should be false =>', fn(1, 'something-but-view'), );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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