ridermansb
ridermansb

Reputation: 11059

Check value in when inside an pipe function

if R.cond resolve an not undefined value, should execute a pipe function and pass the value to the pipe function...

const publishClient = (resp, cb) => {
  console.log('Publishing ', resp);
  cb()
}

// Applying same process
const format = (letter) => ({'letter': letter});
const concact =  R.curry((num, letter) => num + '=' + letter);

// my method publish
const publish = partialRight(publishClient, [function() { console.log('Callback called!') }]);

// conditions to check
const condToRes = R.cond([
  [equals(1), concact('A')],
  [equals(2), concact('B')],
  [equals(3), concact('C')],
  [equals(4), concact('D')],
  [equals(5), concact('E')],
]);

// Publish only if condition resolves
// const resolveCond = when(condToRes, pipe(format, publish)) // NOT WORK,concact FN is ignored!
const resolveCond = pipe( // TRYING
  condToRes,
  when(HOW TO CHECK condToRes IS NOT NIL, pipe(format, publish))
);


// Call
resolveCond(1)

// SHOULD DISPLAY ..
Publishing 
{"letter":"A=1"}
Callback called!

Upvotes: 0

Views: 360

Answers (1)

Scott Christopher
Scott Christopher

Reputation: 6516

To test whether a value is null or undefined you can make use of R.isNil. When combined with the R.complement combinator, this will produce a function that evaluates to true when applied to a value that is not null/undefined.

This can be used to update your example to:

const resolveCond = pipe(
  condToRes,
  when(complement(isNil), pipe(format, publish))
);

Alternatively, R.unless can be used instead of R.when, which removes the need to wrap R.isNil with R.complement.

const resolveCond = pipe(
  condToRes,
  unless(isNil, pipe(format, publish))
);

Both approaches are equivalent, though R.unless perhaps reads a little better.

Upvotes: 2

Related Questions