The iif
decorator allows you to execute given Pipeline.StepFunction conditionally or acts as a pass-through if the provided condition is not satisfied. You can explore the source code here.
Here the manageOnlyStringsPipeline
will be executed since the first argument of iif
evaluates to true.
const { iif, pipe, step } = require('@axa/bautajs-core')
const stepReturnString = step(() => 'I am so random!');
const stepManagesStrings = step(() => 'I can manage only strings');
const pipeline = pipe(
stepReturnString,
iif(prev => typeof prev === 'string', stepManagesStrings)
);
// => 'I can manage only strings'
Here iif
works as a pass-through, since the first argument will evaluate to false.
const { iif, pipe } = require('@axa/bautajs-core')
const pipeline = pipe(
() => 'Plastic is not fantastic!',
iif(
prev => prev.includes('Plastic is fantastic!'),
() => 'Plastic is fantastic!'
)
);
// => 'Plastic is not fantastic!'
Here the else pipeline will be executed since the first argument of iif
evaluates to false.
const { iif, pipe } = require('@axa/bautajs-core')
const pipeline = pipe(
() => 'Plastic is not fantastic!',
iif(
prev => prev.includes('Plastic is fantastic!'),
() => 'Plastic is fantastic!',
() => 'Else: Pink plastic partially problematic'
)
);
// => 'Else: Pink plastic partially problematic!'