DEV Community

Michael Di Prisco
Michael Di Prisco

Posted on • Updated on

My Journey in Open Source - conditional-fn

Link to the repo

What is this?

A higher-order function to provide a Feature Flag mechanism with multiple weights.

How do I install it?

npm install conditional-fn
Enter fullscreen mode Exit fullscreen mode

How can I use it?

Use-case #1 - Simple Feature Flag (Boolean)

import { conditional } from 'conditional-fn';

const shouldICallFnA = true;

const fnToCall = conditional(
  () => console.log('fnA'),
  () => console.log('fnB'),
  shouldICallFnA
)

fnToCall(); // logs 'fnA'
Enter fullscreen mode Exit fullscreen mode

Use-case #2 - Simple Feature Flag (Function)

import { conditional } from 'conditional-fn';

const shouldICallFnA = () => false;

const fnToCall = conditional(
  () => console.log('fnA'),
  () => console.log('fnB'),
  shouldICallFnA
)

fnToCall(); // logs 'fnB'
Enter fullscreen mode Exit fullscreen mode

API

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.

Tests

You can run the tests by using the following command:

npm test
Enter fullscreen mode Exit fullscreen mode

Top comments (0)