Code Monkey home page Code Monkey logo

fifel's Introduction

fifel

Stands for "Functional If..Else".

Feature

A helper function gives a functional "if" statement. Gives a unified output for both truthy/falsy branches. "const over let" fans, you are welcome! ๐Ÿ˜Ž

Install

npm install fifel

Usage

Basic

if..else example

import { fif } from 'fifel';

const result = fif(
  Math.random() > 0.5,
  () => 'truthy',
  () => 'falsy'
);

if only example

else branch is optional. If you don't need it, you can skip it:

import { fif } from 'fifel';
const result = fif(
  Math.random() > 0.5,
  () => {
    // some logic
    return 'truthy';
  }
);

Advanced

This way, we use const instead of let for the result variable you expect to get. It is more helpful when statements in if and else branches include multiple steps before the result is assigned or even some after that. With fif it is better structured:

import { fif } from 'fifel';
function getSomeFuits(amount = 5) {
  const fruits = fif(
    isEnoughInStock(amount),
    () => {
      const result = getFruitsFromStock(amount);
      if (needToRefillStock()) {
        const amountToRefill = getAmountToRefill();
        stickANoteOnTheStock(`Please refill the stock (${amountToRefill} pcs)!`);
      }
      return {
        fromStock: amount,
        fromSupplier: 0,
        full: true
      };
    },
    () => {
      const amountOnStock = getAmountOnStock();
      const fromStock = getFruitsFromStock(amountOnStock);
      stickANoteOnTheStock(`Stock is empty! Please refill!`);
      const fromSupplier = getFruitsFromSupplier(amount - fromStock);
      
      return { 
        fromStock,
        fromSupplier,
        full: fromStock + fromSupplier === amount
      };
    }
  )
}

Within the same logic, using if..else and let result would be less readable and assignment to result could be lost in lines of code.

TypeScript

fif function is typed. You can use it with TypeScript as well. In previous examples, result types are inferred are perfectly inferred.

if..else

import { fif } from 'fifel';
// transport type is inferred as Car | Bike โœ…
const transport = fif(
  readyToGo(),
  () => {
    // some logic
    return getCarFromGarage(); // returns Car
  },
  () => {
    return buyBike(); // returns Bike
  }
);

if only

import { fif } from 'fifel';
// transport type is inferred as Car | undefined โœ…
const transport = fif(
 readyToGo(),
 () => {
   // some logic
   return getCarFromGarage(); // returns Car
 }
);

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.