Joe
Joe

Reputation: 4254

Ramda, pass value in pipe if condition pass

export const getValueFromPsetProperty = (pset: string, property: string) => R.pipe(
  R.prop('pSets'),
  R.find(R.propEq('name', pset)),
  R.propOr([], 'properties'),
  R.find(R.propEq('name', property)),
  R.propOr(null, 'value'),
  R.ifElse(isValueValid, null,R.identity),
);

The last pipe does not work. What I want to do is to pass the value if isValueValid is true. How should I do this?

Upvotes: 0

Views: 1320

Answers (1)

customcommander
customcommander

Reputation: 18941

It doesn't look like you're using R.ifElse correctly. All first three arguments should be functions:

  1. A predicate function
  2. The function to execute if the predicate is true
  3. The function to execute if the predicate is false

So from what you're describing, you want to return the value unchanged if isValueValid returns true. In this case your R.ifElse should be:

R.ifElse(isValueValid, R.identity, someOtherFunction)

You may want to consider R.unless which will run a function only if a predicate is false. If value is valid, then the R.unless returns it unchanged.

R.unless(isValueValid, someFunctionIfValueIsNotValid);

Upvotes: 4

Related Questions