A higher-order function to provide a Feature Flag mechanism.
npm install conditional-fn
import { conditional } from 'conditional-fn';
const shouldICallFnA = true;
const fnToCall = conditional(
() => console.log('fnA'),
() => console.log('fnB'),
shouldICallFnA
)
fnToCall(); // logs 'fnA'
import { conditional } from 'conditional-fn';
const shouldICallFnA = (str) => str === 'fnB';
const fnToCall = conditional(
(str) => console.log('fnA'),
(str) => console.log('fnB'),
shouldICallFnA
)
fnToCall(); // logs 'fnB'
The conditional
function accepts three arguments: two functions and a boolean or a function that returns a boolean. The function will return one of the provided functions based on the boolean or the function that returns a boolean.
The function as the third parameter (The conditional one) will be called with the arguments that were passed to the returned function.
You can run the tests by using the following command:
npm test